Question

In: Computer Science

***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...

***Convert the C++ to Python***

#include <iostream>

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 << "Enter an expression: ";
cin >> str;
strcnt=0;
nextChar = str[strcnt++];
lex();
cout << "Call lex /* returns " << nextToken << " */\n";
/*****************************************************************************
     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\n\n";
    else
      cout << "PARSE SUCCESSFUL\n\n";
}
else
{
    cout << "PARSE FAILED\n\n";
}
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\n";
}
}

/* 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 <expr>\n";

term();
while ((nextToken == PLUS_CODE) ||
         (nextToken == MINUS_CODE))
{
    lex();
    cout << "Call lex /* returns " << nextToken << " */\n";
    term();
}

cout << "Exit <expr>\n";
}

void term()
{
cout << "Enter <term>\n";

factor();
while ((nextToken == AST_CODE) ||
         (nextToken == SLASH_CODE))
{
    lex();
    cout << "Call lex /* returns " << nextToken << " */\n";
    factor();
}

cout << "Exit <term>\n";
}

void factor()
{
cout << "Enter <factor>\n";

if (nextToken == ID_CODE)
{
    lex();
    cout << "Call lex /* returns " << nextToken << " */\n";
}
else if (nextToken == LEFT_PAREN_CODE)
{
    lex();
    cout << "Call lex /* returns " << nextToken << " */\n";
    expr();
    if (nextToken == RIGHT_PAREN_CODE)
    {
      lex();
      cout << "Call lex /* returns " << nextToken << " */\n";
    }
    else
    {
      error = 1;
    }
}
else
{
    error = 1;
}

cout << "Exit <factor>\n";
}
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 << "????";
}

Solutions

Expert Solution

The given C++ code, converted to python is given below. I have commented the code as and where any explanations were needed.

charClass = 0
lexeme = ""
str1 = "" #str is a method in python
nextChar = ""

#Variables cannot be declared constants in python, to emulate that
#we define them as values returned by methods in a class
#thus they can't be modified

class Constant:
def letter(self):
return 0
def digit(self):
return 1
def unknown():
return -1
def oparen():
return 2
def cparen();
return 3
def plus():
return 4
def minus():
return 5
def mul():
return 6
def div():
return 7
def id_code():
return 100
def plus_code():
return 101
def minus_code():
return 102
def ast_code():
return 103
def slash_code():
return 104
def left_paren_code():
return 105
def right_paren_code():
return 106
def num_code():
return 107


lexLen = 0
nextToken = 0
strcnt = 0
error = False

const = Constant()

def addChar():
#declare the scope of global variables
global lexLen
global lexeme
  
if lexLen <= 99:
lexeme.append(nextChar)
lexLen = lexLen + 1
else:
print("Error - lexeme is too long\n")

def getChar():
#declare the scope of global variables
global nextChar
global const
global strcnt
  
if nextChar.isalpha():
charClass = const.letter()
elif nextChar.isdigit():
charClass = const.digit()
elif nextChar == "(":
charClass = const.oparen()
elif nextChar == ")":
charClass = const.cparen()
elif nextChar == "+":
charClass = const.plus()
elif nextChar == "-":
charClass = const.minus()
elif nextChar == "*":
charClass = const.mul()
elif nextChar == "/":
charClass = const.div()
else:
charClass = const.unknown()
nextChar = str1[strcnt]
strcnt = strcnt + 1

def getNonBlank():
#declare the scope of global variables
global nextChar
  
while nextChar.isspace():
getChar()

def lex():
#declare the scope of global variables
global charClass
global nextToken
global const
global lexLen
  
retval = 0
lexLen = 0
first = True
if first == True:
getChar()
first = False
getNonBlank()
if charClass == const.letter():
addChar()
getChar()
while charClass == const.letter() or charClass = const.digit():
addChar()
getChar()
retval = const.id_code()
elif charClass == const.digit():
addChar()
getChar()
while charClass == const.digit():
addChar()
getChar()
retval = const.num_code()
elif charClass == const.plus():
getChar()
retval = const.plus_code()
elif charClass == const.minus():
getChar()
retval = const.minus_code()
elif charClass == const.mul():
getChar()
retval = const.ast_code()
elif charClass = const.div():
getChar()
retval = const.slash_code()
elif charClass = const.oparen():
getChar()
retval = const.left_paren_code()
elif charClass = const.cparen():
getChar()
retval = const.right_paren_code()
else:
retval = const.unknown()
nextToken = retval

def expr():
#declare the scope of global variables
global nextToken
global const
  
print("Enter <expr>\n")
term()
if nextToken == const.plus_code() or nextToken == const.minus_code():
lex()
print("call lex /* returns {} */\n".format(nextToken))
term()
print("Exit <expr>\n")
  
def term():
#declare the scope of global variables
global nextToken
global const
  
print("Enter <term>\n")
factor()
while nextToken == const.ast_code() or nextToken == const.slash_code():
lex()
print("call lex /* returns {} */\n".format(nextToken))
factor()
print("Exit <term>\n")

def factor():
#declare the scope of global variables
global nextToken
global const
global error
  
print("Enter <factor>/n")
if nextToken == const.id_code():
lex():
print("call lex /* returns {} */\n".format(nextToken))
elif nextToken == const.left_paren_code():
lex():
print("call lex /* returns {} */\n".format(nextToken))
expr()
if nextToken == const.right_paren_code():
lex():
print("call lex /* returns {} */\n".format(nextToken))
else:
error = True
else:
error = True
print("Exit <factor>\n")

def ovalue():
#declare the scope of global variables
global nextToken
global const
global lexeme
  
if nextToken == const.id_code():
print(lexeme),
elif nextToken == const.num_code():
print("Number"),
elif nextToken == const.plus_code():
print("+"),
elif nextToken == const.minus_code():
print("-"),
elif nextToken == const.ast_code():
print("*"),
elif nextToken == const.slash_code():
print("/"),
elif nextToken == const.left_paren_code():
print("("),
elif nextToken == const.right_paren_code():
print(")"),
else:
print("????")

if __name__ == "__main__": #main method
global str1
global strcnt
global nextChar
global error
test = 0
str1 = str(raw_input("Enter an expression: "))
strcnt = 0
nextChar = str1[strcnt]
strcnt = strcnt + 1
lex()
'''
/*******************************************************************
THIS SEGMENT OF CODE HAS BEEN COMMENTED OUT ITS PURPOSE IS TO
CONTINUALLY CALL LEX UNTIL THE INPUT HAS BEEN EXHAUSTED
while strcnt != len(str1):
test = lex()
print(test)
END COMMENTED OUT SEGMENT OF CODE
********************************************************************/
'''
if strcnt == len(str1):
if error == True:
print("PARSE FAILED\n\n")
else:
print("PARSE SUCCESSFUL\n\n")
else:
print("PARSE FAILED\n\n")
  

Indentation Screenshots:


Related Solutions

#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left;...
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left; using std::right; using std::setw; using std::string; // The IntVariableTable constructor dynamically allocates the fixed size array of integer variables. IntVariableTable::IntVariableTable() { int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES]; } // The IntVariableTable destructor deallocates the integer variable array. IntVariableTable::~IntVariableTable() { delete[] int_variable_table; } // Returns the number of variables added to the integer variable table. int IntVariableTable::numVariables() const { return num_int_variables; } // Returns the index of...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
Flow this Code to answer question below #include <iostream> using std::cout; struct Node {     int...
Flow this Code to answer question below #include <iostream> using std::cout; struct Node {     int data;     Node *link;                  Node(int data=0, Node *p = nullptr) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs         this->data = data;                                   link = p;     } }; class linked_list { private:     Node *head,*current; public:      //constructor     linked_list() {         head = nullptr;//the head pointer         current = nullptr;//acts as...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i<NUM; i++) cout << arr[i] <<" "; cout <<endl; return 0; } void prepareArr(int a[]) { //randomly generate NUM integers in the range [0,99] and save them in...
unsigned u =10; int i = -42; cout << i+i <<endl; cout << u+i<<endl;//if int take...
unsigned u =10; int i = -42; cout << i+i <<endl; cout << u+i<<endl;//if int take 32 bit, output 4294967264 I know when unsigned and singed operate together, they need to be converted, but why is the answer 4294967264? What does it have to do with int .…… 32 bit
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT