










































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











































Code explanation:
removeComments: prgm) representing the input C++ program and removes the comments from it.s_cmt (single-line comment flag) and m_cmt (multi-line comment flag) are used to keep track of comment blocks.//), the s_cmt flag is set to true, and the loop skips until a newline character is found to indicate the end of the comment./*), the m_cmt flag is set to true, and the loop continues until */ is found to indicate the end of the comment block.res).main: prgm to store the input program.removeComments function is called with the input program, and the modified program (without comments) is printed to the console. Code: #include Output:
Code explanation: Sure! This C++ program examines an input string and classifies the substrings into categories like keywords, integers, real numbers, operators, and valid identifiers. Here's a simpler summary of the code:
isDelimiter(char ch): isOperator(char ch): validIdentifier(char* str): isKeyword(char* str): isInteger(char* str): isRealNumber(char* str): subString(char* str, int left, int right): left to index right.parse(char* str): main(): cin.getline().parse function to examine the input string and sort substrings.|| !strcmp(str, "short") || !strcmp(str, "typedef") || !strcmp(str, "switch") || !strcmp(str, "unsigned") || !strcmp(str, "void") || !strcmp(str, "static") || !strcmp(str, "struct") || !strcmp(str, "goto")) return (true); return (false); } bool isInteger(char* str) { int i, len = strlen(str); if (len == 0) return (false); for (i = 0; i < len; i++) { if (str[i] != '0' && str[i] != '1' && str[i] != '2' && str[i] != '3' && str[i] != '4' && str[i] != '5' && str[i] != '6' && str[i] != '7' && str[i] != '8' && str[i] != '9' || (str[i] == '-' && i > 0)) return (false); } return (true); } bool isRealNumber(char* str) { int i, len = strlen(str); bool hasDecimal = false; if (len == 0) return (false); for (i = 0; i < len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2' && str[i] != '3' && str[i] != '4' && str[i] != '5' && str[i] != '6' && str[i] != '7' && str[i] != '8' && str[i] != '9' && str[i] != '.' || (str[i] == '-' && i > 0)) return (false); if (str[i] == '.') hasDecimal = true; } return (hasDecimal); } char* subString(char* str, int left, int right) { int i; char* subStr = (char)malloc( sizeof(char) * (right - left + 2)); for (i = left; i <= right; i++) subStr[i - left] = str[i]; subStr[right - left + 1] = '\0'; return (subStr); } void parse(char str) { int left = 0, right = 0; int len = strlen(str); while (right <= len && left <= right) { if (isDelimiter(str[right]) == false) right++;
int main() { char str[100]; cin.getline(str,20); cout<<endl; parse(str); return (0); } Input: Output:
Code explanation:
<bits/stdc++.h> header, a GCC-specific header that encompasses all standard C++ libraries, offering a handy method to include them all simultaneously.bool isValidString(const char* str): true if these conditions are met, otherwise false.int main():main function starts by creating a character array inputString of length 100.scanf.isValidString function with this input string.isValidString function, it displays "Accepted!". If not, it displays "Not accepted!". Code: #include <bits/stdc++.h> bool isValidString(const char* str) { int aCount = 0, bCount = 0, cCount = 0; int length = strlen(str); for (int i = 0; i < length; i++) { if (str[i] == 'a') { aCount++; if (bCount > 0 || cCount > 0) { return false; } } else if (str[i] == 'b') { bCount++; if (cCount > 0) { return false; } } else if (str[i] == 'c') { cCount++; } else {State for the finite automaton states.q0, q1, q2, q3, q4.bool isBaba(string str, State currentState):str (the substring being checked), currentState (automaton's current state).true if state is q4 (found "baba").false if string is empty.currentChar) and moves to next state based on character and current state.int main():str for user input.cin.q0.isBaba function with the input and starting state.isBaba. Code: #include <bits/stdc++.h> using namespace std; enum State { q0, q1, q2, q3, q }; bool isBaba(string str, State currentState) {if (currentState == q4) { return true; } if (str.length() == 0) { return false; } char currentChar = str[0]; switch (currentState) { case q0: if (currentChar == 'b') { return isBaba(str.substr(1), q1); } else { return isBaba(str.substr(1), q0); } case q1: if (currentChar == 'a') { return isBaba(str.substr(1), q2); } else { return isBaba(str.substr(1), q0); } case q2: if (currentChar == 'b') { return isBaba(str.substr(1), q3); } else { return isBaba(str.substr(1), q0); } case q3: if (currentChar == 'a') { return isBaba(str.substr(1), q4); } else { return isBaba(str.substr(1), q0);
-1).true (DFA). If not, it returns false.int main():n (state count) and s (symbol count) are declared.a[MAX_STATE][MAX_SYMBOL] is declared to store the transition table data.DFA is called with the relevant transition table, state count, and symbol count parameters.DFA function. Code: #include if (a[i][j] == a[i][k]) { return false; } } } } return true; } int main() { int n, s; cout << "Enter the number of states: "; cin >> n; cout << "Enter the number of symbols: "; cin >> s; int a[MAX_STATE][MAX_SYMBOL]; cout << "Enter the transition table:\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < s; j++) { cin >> a[i][j]; } } if (DFA(a, n, s)) {
non_terminal - > beta non_terminal'non_terminal - > sym alpha non_terminal' | epsilonmain() function:t).solve() for each test to process grammar rules and remove left recursion.solve() function tackles one production rule at a time. Code: #include <bits/stdc++.h> using namespace std; void solve() { string pro; while (cin >> pro) { char non_terminal, beta, alpha, sym; int index = 3; cout << "GRAMMAR IS : " << pro<< endl; non_terminal = pro[0]; if (non_terminal == pro[index]) { sym = pro[index + 1]; alpha = pro[index + 2]; while (pro[index] != '\0' && pro[index] != '|') { index++; } if (pro[index] != 0) {beta = pro[index + 1]; cout<< "Grammar without left recursion:"<<endl; cout<<non_terminal<<"->"<<beta<<non_terminal<<"'"<<endl; cout<<non_terminal<<"->"<<sym<< alpha<< non_terminal<<"'"<<"|#"; } else cout<<"can't be reduced"<<endl; } else cout<<"not recursive."<<endl; index = 3; } } int main() { long long t; cin>>t; while(t--) { solve(); } } Input: Output: