In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures where each such procedure implements one of the nonterminals of the grammar. Thus the structure of the resulting program closely mirrors that of the grammar it recognizes. A predictive parser is a recursive descent parser that does not require backtracking. Predictive parsing is possible only for the class of LL grammars, which are the context-free grammars for which there exists some positive integerk that allows a recursive descent parser to decide which production to use by examining only the next k tokens of input. The LL grammars therefore exclude all ambiguous grammars, as well as all grammars that contain left recursion. Any context-free grammar can be transformed into an equivalent grammar that has no left recursion, but removal of left recursion does not always yield an LL grammar. A predictive parser runs in linear time. Recursive descent with backtracking is a technique that determines which production to use by trying each production in turn. Recursive descent with backtracking is not limited to LL grammars, but is not guaranteed to terminate unless the grammar is LL. Even when they terminate, parsers that use recursive descent with backtracking may require exponential time. Although predictive parsers are widely used, and are frequently chosen if writing a parser by hand, programmers often prefer to use a table-based parser produced by a parser generator, either for an LL language or using an alternative parser, such as LALR or LR. This is particularly the case if a grammar is not in LL form, as transforming the grammar to LL to make it suitable for predictive parsing is involved. Predictive parsers can also be automatically generated, using tools like ANTLR. Predictive parsers can be depicted using transition diagrams for each non-terminal symbol where the edges between the initial and the final states are labelled by the symbols of the right side of the production rule.
Example parser
The following EBNF-like grammar is in LL form: program = block ".". block =
What follows is an implementation of a recursive descent parser for the above language in C. The parser reads in source code, and exits with an error message if the code fails to parse, exiting silently if the code parses correctly. Notice how closely the predictive parser below mirrors the grammar above. There is a procedure for each nonterminal in the grammar. Parsing descends in a top-down manner, until the final nonterminal has been processed. The programfragment depends on a global variable, sym, which contains the current symbol from the input, and the function nextsym, which updates sym when called. The implementations of the functions nextsym and error are omitted for simplicity. typedef enum Symbol; Symbol sym; void nextsym; void error; int accept int expect void factor void term void expression void condition void statement void block void program
Examples
Some recursive descent parser generators:
TMG – an early compiler-compiler used in the 1960s and early 1970s
JavaCC
Coco/R
ANTLR
Spirit Parser Framework – a C++ recursive descent parser generator framework requiring no pre-compile step
parboiled – a recursive descent PEG parsing library for Java