Professional Documents
Culture Documents
1, Recursive Descent: Program Program STMT List STMT List STMT STMT List
1, Recursive Descent: Program Program STMT List STMT List STMT STMT List
Recursive descent parsing is a top down parsing technique that contructs the parse tree from
the top and the input is read from left to right. It uses procedures for every terminal and non
terminal entity. It recursively parse the input to make the parse tree, which may or may not
required backtracking. However, the grammar associated with it can’t avoid backtracking.
Handwritten recursive descent parsers are most often used when the language to be parsed is relatively
simple, or when a parser-generator tool is not available. There are exceptions, however. In particular,
recursive descent appears in recent versions of the GNU compiler collection ( gcc). Earlier versions used
bison to create a bottom-up parser automatically. The change was made in part for performance reasons
and in part to enable the generation of higher quality syntax error messages.
It has a subroutine for every nonterminal in the grammar. It also has a mechanism input token to inspect the
next token available from the scanner and a subroutine (match) to consume and update this token, and in
the process verify that it is the one that was expected (as specified by an argument). If match or any of the
other subroutines sees an unexpected token, then a syntax error has occurred. For the time being let us
assume that the parse error subroutine simply prints a message and terminates the parse
The top-down parsing start at the root of the tree (id list) and expand to id id_list_tail. It then matches the id against a token obtained
from the scanner. The parser moves down into the first nonterminal child and predicts that id_list_tail will expand to id, id_list_tail. To
predict this, it needs
to peek at the upcoming token, in this case is the comma, which allows it to choose between the two posible expansions for
id_list_tail. Then it maches the comma and the id, after that it moves down into the next id_list_tail.
!!!What is backtracking!!!
To make things more easier to explain, let’s look at this example:
S → rXd | rZd
X → oa | ea
Z → ai
In recursive descent parser, each arm of a case statement corresponds to a production, and contains parsing
routine and match calls corresponding to the symbols on the right-hand side of the prodcution. At any given point
in the parse if we consider the calls beyond the program counter (the ones that have yet to occur) in the
parsing routine invocations currently in the call stack, we obtain a list of the symbols that the parser expects
to see between here and the end of the program. A table-driven top-down parser maintains an explicit stack
containing this same list of symbols.