Question: i am having issues correcting an error in my program here is my code: #include // define libraries #include using namespace std; // Variable definitions:

i am having issues correcting an error in my program

here is my code:

#include // define libraries

#include

using namespace std;

// Variable definitions:

ifstream infp; // file handler

enum Tokens {INT_LIT, IDENT, ASSIGN_OP, ADD_OP, SUB_OP, MUL_OP, DIV_OP, LEFT_PAREN, RIGHT_PAREN, LETTER, DIGIT, UNKNOWN,ENDFILE};

Tokens nextToken; // nextToken read from the file.

int charClass; // char class

char lexeme [100]; // number of characters per line

char nextChar; // next char read from the file

int lexLen; // number of characters in lexeme so far.

// function to get the next character from the file

void getChar()

{

if ((nextChar = infp.get()) != EOF)

{

if (isalpha(nextChar)) charClass = LETTER;

else if (isdigit(nextChar)) charClass = DIGIT;

else charClass = UNKNOWN;

}

else charClass = ENDFILE;

}

// function to skip all white spaces

void skipWhiteSpace()

{

while (isspace(nextChar))

getChar();

}

// function add the character read from the file into the arraylexeme

void addChar()

{

if (lexLen < 99)

{

lexeme[lexLen++] = nextChar;

lexeme[lexLen] = 0;

}

else cout << "Error - lexeme is too long ";

}

// function to determine the token

int lookup(char ch)

{

switch (ch)

{

case '(': addChar(); nextToken = LEFT_PAREN; break;

case ')': addChar(); nextToken = RIGHT_PAREN; break;

case '+': addChar(); nextToken = ADD_OP; break;

case '-': addChar(); nextToken = SUB_OP; break;

case '*': addChar(); nextToken = MUL_OP; break;

case '/': addChar(); nextToken = DIV_OP; break;

case '=': addChar(); nextToken = ASSIGN_OP; break;

default: addChar(); nextToken = ENDFILE; break;

}

return nextToken;

}

void print(Tokens t)

{ // print out the token category.

switch (t)

{

case INT_LIT: cout << ""; break;

case IDENT: cout << ""; break;

case ASSIGN_OP: cout << ""; break;

case ADD_OP: cout << ""; break;

case SUB_OP: cout << ""; break;

case MUL_OP: cout << ""; break;

case DIV_OP: cout << ""; break;

case LEFT_PAREN: cout << ""; break;

case RIGHT_PAREN: cout << ""; break;

case LETTER: cout << ""; break;

case DIGIT: cout << ""; break;

case UNKNOWN: cout << ""; break;

case ENDFILE: cout << ""; break;

} /* End of switch */

} /* End of function print */

int lex()

{ //function lex() returns a token for each lexeme read from the file

lexLen = 0; skipWhiteSpace();

switch (charClass)

{

case LETTER:

while (charClass == LETTER || charClass == DIGIT)

{

addChar();

getChar();

}

nextToken = IDENT;

break;

case DIGIT:

while (charClass == DIGIT)

{

addChar();

getChar();

}

nextToken = INT_LIT;

break;

case UNKNOWN: lookup(nextChar);

nextChar = ' ';

break;

case ENDFILE:

nextToken=ENDFILE;

lexeme[0]='E';

lexeme[1]='O';

lexeme[2] = 'F';

lexeme[3] = 0;

break;

} /* End of switch */

cout << "Next token is: "; print(nextToken);

cout <<" Next lexeme is " << lexeme << endl;

return nextToken;

} /* End of function lex */

int main() {

infp.open("front.in");

if (!infp) cout << "ERROR - cannot open front.in ";

else {

nextChar = ' ';

lex();

do {

expr();

} while (nextToken != EOF);

}

return 0;

}

/* Function expr

Parses strings in the language generated by the rule:

{(+ | -) }

equivalent to:

| | -

*/

void expr() {

printf("Enter ");

term(); // Parse the first term

/* As long as the next token is + or -, call lex to get the next token

and parse the next term */

while (nextToken == ADD_OP || nextToken == SUB_OP){

lex();

term();

}

printf("Exit ");

}

/* Function: term(): Parses strings in the language generated by the rule:

-> {(* | /) } or equivalent to:

-> | * | /

*/

void term() {

printf("Enter ");

factor(); // Parse the first factor

/* As long as the next token is * or /, next token and parse the next

factor */

while (nextToken == MUL_OP || nextToken == DIV_OP) {

lex();

factor();

}

printf("Exit ");

} /* End of function term */

/* Function factor: Parses strings generated by the rule:

-> id | int_constant | () */

void factor() {

printf("Enter ");

/* Determine which RHS */

if (nextToken) == IDENT || nextToken == INT_LIT)

lex(); /* For the RHS id, just call lex */

else if (nextToken == LEFT_PAREN) {

lex();

expr();

if (nextToken == RIGHT_PAREN)

lex();

else

cout << "Expected ), found " << nextChar << endl;

} /* End of else if (nextToken == ... */

else cout << "Error: Expected (, found " << nextChar << endl;

printf("Exit ");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!