Concepts of Programming Languages

(Sean Pound) #1

186 Chapter 4 Lexical and Syntax Analysis


One more example grammar rule and parsing function should help solidify
the reader’s understanding of recursive-descent parsing. Following is a gram-
matical description of the Java if statement:

<ifstmt> → if (<boolexpr>) <statement> [else <statement>]

The recursive-descent subprogram for this rule follows:

/* Function ifstmt
Parses strings in the language generated by the rule:
<ifstmt> -> if (<boolexpr>) <statement>
[else <statement>]
*/
void ifstmt() {
/* Be sure the first token is 'if' */
if (nextToken != IF_CODE)
error();
else {
/* Call lex to get to the next token */
lex();
/* Check for the left parenthesis */
if (nextToken != LEFT_PAREN)
error();
else {
/* Call boolexpr to parse the Boolean expression */
boolexpr();
/* Check for the right parenthesis */
if (nextToken != RIGHT_PAREN)
error();
else {
/* Call statement to parse the then clause */
statement();
/* If an else is next, parse the else clause */
if (nextToken == ELSE_CODE) {
/* Call lex to get over the else */
lex();
statement();
} /* end of if (nextToken == ELSE_CODE ... */
} /* end of else of if (nextToken != RIGHT ... */
} /* end of else of if (nextToken != LEFT ... */
} /* end of else of if (nextToken != IF_CODE ... */
} /* end of ifstmt */

Notice that this function uses parser functions for statements and Boolean
expressions, which are not given in this section.
The objective of these examples is to convince you that a recursive-descent
parser can be easily written if an appropriate grammar is available for the
Free download pdf