CSCI 400
Recursive Descent Exercise #1
Consider the recursive descent code from the textbook, repeated below:
/* Function factor
Parses strings in the language generated by the rule:
<factor> -> id | (<ex)
*/
void factor() {
// Determine which RHS */
if (nextToken) == ID_CODE)
lex(); // For the RHS id, just call lex
/* If the RHS is ( – call lex to pass over the left parenthesis,
call expr, then check for the right parenthesis */
else if (nextToken == LEFT_PAREN_CODE)
{
lex();
expr();
if (nextToken == RIGHT_PAREN_CODE)
lex();
else
error();
} /* End of else if (nextToken == ... */
else error(); /* Neither RHS matches */
}
/* Function term
Parses strings in the language generated by the rule:
<term> -> <factor> {(* | /) <factor> }
*/
void term() {
/* Parse the first factor */
factor();
while (nextToken == AST_CODE || nextToken == SLASH_CODE)
{
lex();
factor();
}
}
/* Function expr
Parses strings in the language generated by the rule:
<expr> → <term> { (+ | -) <term> }
*/
void 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 == PLUS_CODE || nextToken == MINUS_CODE)
{
lex();
term();
}
}
Assume the symbols a, b and c will return ID_CODE. Use the above
functions to do a top-down parse of the statement:
a + b * c
Do a trace similar to slide #36 in Chapter 4 ppt.