Question: Show a trace of the recursive descent parser given as follows for the string (b-c)/a. In your answer you need to indicate enter a method,

Show a trace of the recursive descent parser given as follows for the string (b-c)/a.

In your answer you need to indicate enter a method, exit a method, and what is the next token when lex() is called.

/* Function expr

Parses strings in the language

generated by the rule:

{(+ | -) }

*/

void expr() {

printf("Enter < expr > ");

/* Parse the first term */

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 < 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 /,

next token and parse the next factor */

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

lex();

factor();

}

printf("Exit ");

} /* End of function term */

/* Function factor

Parses strings in the language

generated by the rule:

-> id | () */

void factor() {

printf("Enter < factor > ");

/* Determine which RHS */

if (nextToken) == ID_CODE || nextToken == INT_CODE)

/* For the RHS id, just call lex */

lex();

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

call expr, and check for the right parenthesis */

else if (nextToken == LP_CODE) {

lex();

expr();

if (nextToken == RP_CODE)

lex();

else

error();

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

else error(); /* Neither RHS matches */

printf("Exit < 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!