Question
**Convert the C++ to Python*** #include using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0;
#include
using 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 cin >> str; strcnt=0; nextChar = str[strcnt++]; lex(); cout /***************************************************************************** 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 }
END COMMENTED OUT SEGMENT OF CODE *****************************************************************************/ expr(); /* begin recursive decent parsing */ if (nextChar == '\\0') { if (error) cout else cout } else { cout } return 0; }
/* addChar - a function to add nextChar to lexeme */ void addChar() { if (lexLen => { lexeme[lexLen++] = nextChar; } else { cout } }
/* 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 \";
term(); while ((nextToken == PLUS_CODE) || (nextToken == MINUS_CODE)) { lex(); cout term(); }
cout \"; }
void term() { cout \";
factor(); while ((nextToken == AST_CODE) || (nextToken == SLASH_CODE)) { lex(); cout factor(); }
cout \"; }
void factor() { cout \";
if (nextToken == ID_CODE) { lex(); cout } else if (nextToken == LEFT_PAREN_CODE) { lex(); cout expr(); if (nextToken == RIGHT_PAREN_CODE) { lex(); cout } else { error = 1; } } else { error = 1; }
cout \"; } void ovalue() { if (nextToken == ID_CODE) cout else if (nextToken == NUM_CODE) cout 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