Question: /* expr Parses strings in the language generated by the rule: -> {(+ | -) } */ void expr() { printf(Enter ); /* Parse the

/* expr

Parses strings in the language generated by the rule:

-> {(+ | -) }

*/

void expr() {

printf("Enter ");

/* Parse the first term */

term();

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

the next token and parse the next term */

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

lex();

term();

}

printf("Exit ");

} /* End of function expr */

/* term

Parses strings in the language generated by the rule:

-> {(* | /) )

*/

void term() {

printf("Enter ");

/* Parse the first factor */

factor();

/* As long as the next token is * or /, get the

next token and parse the next factor */

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

lex();

factor();

}

printf("Exit ");

} /* End of function term */

/* factor

Parses strings in the language generated by the rule:

-> id | int_constant | (

*/

void factor() {

printf("Enter ");

/* Determine which RHS */

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

/* Get the next token */

lex();

/* If the RHS is ( ), call lex to pass over the

left parenthesis, call expr, and check for the right

parenthesis */

else {

if (nextToken == LEFT_PAREN) {

lex();

expr();

if (nextToken == RIGHT_PAREN)

lex();

else

error();

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

/* It was not an id, an integer literal, or a left

parenthesis */

else

error();

} /* End of else */

printf("Exit ");;

} /* End of function factor */

***GIVEN THE CODE ABOVE FOR A LEXICAL ANALYZER in c++ Implement the Parser found below thats in C, to c++, with Input, EXP***

//expression(EXP) example:

// (sum + 47) /total

/*

the parse begins by calling lex and the start

symbol routine, in this case, expr .

Next token is: 25 Next lexeme is (

Enter

Enter

Enter

Next token is: 11 Next lexeme is sum

Enter

Enter

Enter

Next token is: 21 Next lexeme is +

Exit

Exit

Next token is: 10 Next lexeme is 47

Enter

Enter

Next token is: 26 Next lexeme is )

Exit

Exit

Exit

Next token is: 24 Next lexeme is /

Exit

Next token is: 11 Next lexeme is total

Enter

Next token is: -1 Next lexeme is EOF

Exit

Exit

Exit

*/

/* expr

Parses strings in the language generated by the rule:

-> {(+ | -) }

*/

void expr() {

printf("Enter ");

/* Parse the first term */

term();

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

the next token and parse the next term */

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

lex();

term();

}

printf("Exit ");

} /* End of function expr */

/* term

Parses strings in the language generated by the rule:

-> {(* | /) )

*/

void term() {

printf("Enter ");

/* Parse the first factor */

factor();

/* As long as the next token is * or /, get the

next token and parse the next factor */

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

lex();

factor();

}

printf("Exit ");

} /* End of function term */

/* factor

Parses strings in the language generated by the rule:

-> id | int_constant | (

*/

void factor() {

printf("Enter ");

/* Determine which RHS */

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

/* Get the next token */

lex();

/* If the RHS is ( ), call lex to pass over the

left parenthesis, call expr, and check for the right

parenthesis */

else {

if (nextToken == LEFT_PAREN) {

lex();

expr();

if (nextToken == RIGHT_PAREN)

lex();

else

error();

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

/* It was not an id, an integer literal, or a left

parenthesis */

else

error();

} /* End of else */

printf("Exit ");;

} /* End of function factor */

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!