Recursive descent parser
In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent) where each such procedure implements one of the nonterminals of the grammar.[1] Thus the structure of the resulting program closely mirrors that of the grammar it recognizes.[src] A predictive parser is a recursive descent parser that does not require backtracking.[1] Predictive parsing is possible only for the class of LL(k) grammars, which are the context-free grammars for which there exists some positive integer k that allows a recursive descent parser to decide which production to use by examining only the next k tokens of input.[1] The LL(k) grammars therefore exclude all ambiguous grammars, as well as all grammars that contain left recursion.[src] 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(k) grammar.[src] A predictive parser runs in linear time.[src] Recursive descent with backtracking is a technique that determines which production to use by trying each production in turn.[1] Recursive descent with backtracking is not limited to LL(k) grammars, but is not guaranteed to terminate unless the grammar is LL(k).[1] Even when they terminate, parsers that use recursive descent with backtracking may require exponential time.…[1]
In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent) where each such procedure implements one of the nonterminals of the grammar.[1] Thus the structure of the resulting program closely mirrors that of the grammar it recognizes.[src] A predictive parser is a recursive descent parser that does not require backtracking.[1] Predictive parsing is possible only for the class of LL(k) grammars, which are the context-free grammars for which there exists some positive integer k that allows a recursive descent parser to decide which production to use by examining only the next k tokens of input.[1] The LL(k) grammars therefore exclude all ambiguous grammars, as well as all grammars that contain left recursion.[src] 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(k) grammar.[src] A predictive parser runs in linear time.[src] Recursive descent with backtracking is a technique that determines which production to use by trying each production in turn.[1] Recursive descent with backtracking is not limited to LL(k) grammars, but is not guaranteed to terminate unless the grammar is LL(k).[1] Even when they terminate, parsers that use recursive descent with backtracking may require exponential time.[1] 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(k) language or using an alternative parser, such as LALR or LR.[1] This is particularly the case if a grammar is not in LL(k) form, as transforming the grammar to LL to make it suitable for predictive parsing is involved.[src] Predictive parsers can also be automatically generated, using tools like ANTLR.[src] 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 (terminals and non-terminals) of the right side of the production rule.[src] ==Example parser== The following EBNF-like grammar (for Niklaus Wirth's PL/0 programming language, from Algorithms + Data Structures = Programs) is in LL(1) form: program = block "." .[src] block = ["const" ident "=" number {"," ident "=" number} ";"] ["var" ident {"," ident} ";"] {"procedure" ident ";" block ";"} statement .[src] statement = ident ":=" expression | "call" ident | "begin" statement {";" statement } "end" | "if" condition "then" statement | "while" condition "do" statement .[src] condition = "odd" expression | expression ("="|"#"|" "|">=") expression .[src] expression = ["+"|"-"] term {("+"|"-") term} .[src] term = factor {("*"|"/") factor} .[src] factor = ident | number | "(" expression ")" .[src] Terminals are expressed in quotes.[src] Each nonterminal is defined by a rule in the grammar, except for ident and number, which are assumed to be implicitly defined.[src] ===C implementation=== What follows is an implementation of a recursive descent parser for the above language in C.[1] 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.[src] Notice how closely the predictive parser below mirrors the grammar above.[src] There is a procedure for each nonterminal in the grammar.[src] Parsing descends in a top-down manner until the final nonterminal has been processed.[src] The program fragment depends the functions peeksym , which peeks at the current symbol; consumesym , which consumes the symbol to move to the next; and error , which displays an error message.[src] These functions are assumed to be provided by the lexer.[src] extern void error(const char msg[]); extern void consumesym(); typedef enum Symbol { ident, number, lparen, rparen, times, slash, plus, minus, eql, neq, lss, leq, gtr, geq, callsym, beginsym, semicolon, endsym, ifsym, whilesym, becomes, thensym, dosym, constsym, comma, varsym, procsym, period, oddsym } Symbol; extern Symbol peeksym(); bool accept(Symbol s) { if (peeksym() == s) { consumesym(); return true; } return false; } bool expect(Symbol s) { if (accept(s)) { return true; } error("expect: unexpected symbol"); return false; } void factor() { if (accept(ident) || accept(number)) { return; } if (accept(lparen)) { expression(); expect(rparen); } else { error("factor: syntax error"); consumesym(); } } void term() { factor(); while (peeksym() == times || peeksym() == slash) { consumesym(); factor(); } } void expression() { if (peeksym() == plus || peeksym() == minus) { consumesym(); } term(); while (peeksym() == plus || peeksym() == minus) { consumesym(); term(); } } void condition() { if (accept(oddsym)) { expression(); return; } expression(); if (peeksym() == eql || peeksym() == neq || peeksym() == lss || peeksym() == leq || peeksym() == gtr || peeksym() == geq) { consumesym(); expression(); } else { error("condition: invalid operator"); consumesym(); } } void statement() { if (accept(ident)) { expect(becomes); expression(); } else if (accept(callsym)) { expect(ident); } else if (accept(beginsym)) { do { statement(); } while (accept(semicolon)); expect(endsym); } else if (accept(ifsym)) { condition(); expect(thensym); statement(); } else if (accept(whilesym)) { condition(); expect(dosym); statement(); } else { error("statement: syntax error"); consumesym(); } } void block() { if (accept(constsym)) { do { expect(ident); expect(eql); expect(number); } while (accept(comma)); expect(semicolon); } if (accept(varsym)) { do { expect(ident); } while (accept(comma)); expect(semicolon); } while (accept(procsym)) { expect(ident); expect(semicolon); block(); expect(semicolon); } statement(); } void program() { block(); expect(period); } ==See also== Parser combinator – a higher-order function used in combinatory parsing, a method of factoring recursive descent parser designs Parsing expression grammar – another form representing recursive descent grammar Recursive ascent parser Tail recursive parser – a variant of the recursive descent parser ==References== ===General references=== Compilers: Principles, Techniques, and Tools, first edition, Alfred V Aho, Ravi Sethi, and Jeffrey D Ullman, in particular Section 4.4.[3] Modern Compiler Implementation in Java, Second Edition, Andrew Appel, 2002, .[src] Recursive Programming Techniques, W.H.[2] Burge, 1975, Crafting a Compiler with C, Charles N Fischer and Richard J LeBlanc, Jr, 1991, .[2] Compiling with C# and Java, Pat Terry, 2005, , 624 Algorithms + Data Structures = Programs, Niklaus Wirth, 1975, Compiler Construction, Niklaus Wirth, 1996, ==External links== Jack W.[5] Crenshaw: Let's Build A Compiler (1988-1995), in Pascal, with assembly language output, using a "keep it simple" approach[5]
46 sentences · 46 cited inline · 5 references stored
References
- relicensing · retrieved 2026-11-17 · rev 1342274285
Cite this reference
MLA "relicensing." This article is based on material taken from Recursive+descent+parser at the Free On-line Dictionary of Computing prior. 2026-11-17. https://foldoc.org/Recursive+descent+parser. Accessed 25 November 2026.
Chicago "relicensing." This article is based on material taken from Recursive+descent+parser at the Free On-line Dictionary of Computing prior. 2026-11-17. https://foldoc.org/Recursive+descent+parser.
APA This article is based on material taken from Recursive+descent+parser at the Free On-line Dictionary of Computing prior. (2026). relicensing. Retrieved November 25, 2026, from https://foldoc.org/Recursive+descent+parser
- Burge, W.H. (1975). Recursive Programming Techniques . Addison-Wesley Publishing Company. ISBN 0-201-14450-6 . Wikipedia revision 1342274285, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1342274285
Cite this reference
MLA "Burge, W.H. (1975). Recursive Programming Techniques . Addison-Wesley Publishing Company. ISBN 0-201-14450-6 . Wikipedia revision 1342274285, retrieved 2026-11-18." Burge, W. 2026-11-17. https://archive.org/details/recursiveprogram0000burg. Accessed 25 November 2026.
Chicago "Burge, W.H. (1975). Recursive Programming Techniques . Addison-Wesley Publishing Company. ISBN 0-201-14450-6 . Wikipedia revision 1342274285, retrieved 2026-11-18." Burge, W. 2026-11-17. https://archive.org/details/recursiveprogram0000burg.
APA Burge, W. (2026). Burge, W.H. (1975). Recursive Programming Techniques . Addison-Wesley Publishing Company. ISBN 0-201-14450-6 . Wikipedia revision 1342274285, retrieved 2026-11-18. Retrieved November 25, 2026, from https://archive.org/details/recursiveprogram0000burg
- Aho, Alfred V. ; Sethi, Ravi; Ullman, Jeffrey (1986). Compilers: Principles, Techniques and Tools (first ed.). Addison Wesley. p. 183 . Wikipedia revision 1342274285, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1342274285
Cite this reference
MLA "Aho, Alfred V. ; Sethi, Ravi; Ullman, Jeffrey (1986). Compilers: Principles, Techniques and Tools (first ed.). Addison Wesley. p. 183 . Wikipedia revision 1342274285, retrieved 2026-11-18." Aho, Alfred V. 2026-11-17. https://en.wikipedia.org/wiki/Alfred_V._Aho. Accessed 25 November 2026.
Chicago "Aho, Alfred V. ; Sethi, Ravi; Ullman, Jeffrey (1986). Compilers: Principles, Techniques and Tools (first ed.). Addison Wesley. p. 183 . Wikipedia revision 1342274285, retrieved 2026-11-18." Aho, Alfred V. 2026-11-17. https://en.wikipedia.org/wiki/Alfred_V._Aho.
APA Aho, Alfred V. (2026). Aho, Alfred V. ; Sethi, Ravi; Ullman, Jeffrey (1986). Compilers: Principles, Techniques and Tools (first ed.). Addison Wesley. p. 183 . Wikipedia revision 1342274285, retrieved 2026-11-18. Retrieved November 25, 2026, from https://en.wikipedia.org/wiki/Alfred_V._Aho
- How Clang handles the type / variable name ambiguity of C/C++ https://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc/ Wikipedia revision 1342274285, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1342274285
Cite this reference
MLA "How Clang handles the type / variable name ambiguity of C/C++ https://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc/ Wikipedia revision 1342274285, retrieved 2026-11-18." How Clang handles the type / variable name ambiguity of C/C++ https://eli. 2026-11-17. https://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc/. Accessed 25 November 2026.
Chicago "How Clang handles the type / variable name ambiguity of C/C++ https://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc/ Wikipedia revision 1342274285, retrieved 2026-11-18." How Clang handles the type / variable name ambiguity of C/C++ https://eli. 2026-11-17. https://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc/.
APA How Clang handles the type / variable name ambiguity of C/C++ https://eli. (2026). How Clang handles the type / variable name ambiguity of C/C++ https://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc/ Wikipedia revision 1342274285, retrieved 2026-11-18. Retrieved November 25, 2026, from https://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc/
- Watson, Des (22 March 2017). A Practical Approach to Compiler Construction . Springer. ISBN 978-3-319-52789-5 . Wikipedia revision 1342274285, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1342274285
Cite this reference
MLA "Watson, Des (22 March 2017). A Practical Approach to Compiler Construction . Springer. ISBN 978-3-319-52789-5 . Wikipedia revision 1342274285, retrieved 2026-11-18." Watson, Des (22 March 2017). 2026-11-17. https://books.google.com/books?id=05B0DgAAQBAJ&q=%22predictive+parser%22. Accessed 25 November 2026.
Chicago "Watson, Des (22 March 2017). A Practical Approach to Compiler Construction . Springer. ISBN 978-3-319-52789-5 . Wikipedia revision 1342274285, retrieved 2026-11-18." Watson, Des (22 March 2017). 2026-11-17. https://books.google.com/books?id=05B0DgAAQBAJ&q=%22predictive+parser%22.
APA Watson, Des (22 March 2017). (2026). Watson, Des (22 March 2017). A Practical Approach to Compiler Construction . Springer. ISBN 978-3-319-52789-5 . Wikipedia revision 1342274285, retrieved 2026-11-18. Retrieved November 25, 2026, from https://books.google.com/books?id=05B0DgAAQBAJ&q=%22predictive+parser%22
Cite this article
MLA
"Recursive descent parser." encyclopegia.org, 2026, https://encyclopegia.org/wiki/recursive-descent-parser. Accessed 25 November 2026.
Chicago
"Recursive descent parser." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/recursive-descent-parser.
APA
encyclopegia.org. (2026). Recursive descent parser. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/recursive-descent-parser
Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON