Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For this problem you must: -Write a Python program -Test, debug, and execute the program -Submit a copy of your commented source code on-line prior
For this problem you must:
-Write a Python program
-Test, debug, and execute the program
-Submit a copy of your commented source code on-line prior to class
Convert the following C++ code to Python.
/*********************************************************** Add header comments here ***********************************************************/ #includeusing std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE = 102; const int AST_CODE = 103; const int SLASH_CODE = 104; const int LEFT_PAREN_CODE = 105; const int RIGHT_PAREN_CODE = 106; const int NUM_CODE = 107; int lexLen; int nextToken; int strcnt; int error = 0; void addChar(); void getChar(); void getNonBlank(); void lex(); void expr(); void term(); void factor(); void ovalue(); int main() { int test; cout << "Enter an expression: "; cin >> str; strcnt=0; nextChar = str[strcnt++]; lex(); cout << "Call lex /* returns " << nextToken << " */ "; /***************************************************************************** THIS SEGMENT OF CODE HAS BEEN COMMENTED OUT ITS PURPOSE IS TO CONTINUALLY CALL LEX UNTIL THE INPUT HAS BEEN EXHAUSTED while (nextChar != '\0') { test = lex(); cout << test << endl; } END COMMENTED OUT SEGMENT OF CODE *****************************************************************************/ expr(); /* begin recursive decent parsing */ if (nextChar == '\0') { if (error) cout << "PARSE FAILED "; else cout << "PARSE SUCCESSFUL "; } else { cout << "PARSE FAILED "; } return 0; } /* addChar - a function to add nextChar to lexeme */ void addChar() { if (lexLen <= 99) { lexeme[lexLen++] = nextChar; } else { cout << "Error - lexeme is too long "; } } /* getChar - a function to get the next character of input and determine its character class */ void getChar() { /* do whatever is required to get the next character from input and put it in nextChar */ if (isalpha(nextChar)) { charClass = LETTER; } else if (isdigit(nextChar)) { charClass = DIGIT; } else if (nextChar == '(') { charClass = OPAREN; } else if (nextChar == ')') { charClass = CPAREN; } else if (nextChar == '+') { charClass = PLUS; } else if (nextChar == '-') { charClass = MINUS; } else if (nextChar == '*') { charClass = MUL; } else if (nextChar == '/') { charClass = DIV; } else { charClass = UNKNOWN; } nextChar = str[strcnt++]; } /* getNonBlank - a function that calles getChar until it returns a non-whitespace character */ void getNonBlank() { while (isspace(nextChar)) { getChar(); } } /* lex - a simple lexical analyzer */ void lex() { int retval; lexLen = 0; static int first = 1; /* If it is the first call to lex, initialize by calling getChar */ if (first) { getChar(); first = 0; } getNonBlank(); /* process identifiers */ if (charClass == LETTER) { addChar(); getChar(); while ((charClass == LETTER) || (charClass == DIGIT)) { addChar(); getChar(); } retval = ID_CODE; } else if (charClass == DIGIT) { addChar(); getChar(); while (charClass == DIGIT) { addChar(); getChar(); } retval = NUM_CODE; } else if (charClass == PLUS) { getChar(); retval = PLUS_CODE; } else if (charClass == MINUS) { getChar(); retval = MINUS_CODE; } else if (charClass == MUL) { getChar(); retval = AST_CODE; } else if (charClass == DIV) { getChar(); retval = SLASH_CODE; } else if (charClass == OPAREN) { getChar(); retval = LEFT_PAREN_CODE; } else if (charClass == CPAREN) { getChar(); retval = RIGHT_PAREN_CODE; } else { retval = UNKNOWN; } nextToken = retval; } void expr() { cout << "Enter "; term(); while ((nextToken == PLUS_CODE) || (nextToken == MINUS_CODE)) { lex(); cout << "Call lex /* returns " << nextToken << " */ "; term(); } cout << "Exit "; } void term() { cout << "Enter "; factor(); while ((nextToken == AST_CODE) || (nextToken == SLASH_CODE)) { lex(); cout << "Call lex /* returns " << nextToken << " */ "; factor(); } cout << "Exit "; } void factor() { cout << "Enter "; if (nextToken == ID_CODE) { lex(); cout << "Call lex /* returns " << nextToken << " */ "; } else if (nextToken == LEFT_PAREN_CODE) { lex(); cout << "Call lex /* returns " << nextToken << " */ "; expr(); if (nextToken == RIGHT_PAREN_CODE) { lex(); cout << "Call lex /* returns " << nextToken << " */ "; } else { error = 1; } } else { error = 1; } cout << "Exit "; } void ovalue() { if (nextToken == ID_CODE) cout << lexeme; else if (nextToken == NUM_CODE) cout << "Number"; else if (nextToken == PLUS_CODE) cout << "+"; else if (nextToken == MINUS_CODE) cout << "-"; else if (nextToken == AST_CODE) cout << "*"; else if (nextToken == SLASH_CODE) cout << "/"; else if (nextToken == LEFT_PAREN_CODE) cout << "("; else if (nextToken == RIGHT_PAREN_CODE) cout << ")"; else if (nextToken == UNKNOWN) cout << "????"; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started