LearnViz CompSci
Home / Learn / Languages & Compilers / Recursive Descent Parsing

Intermediate Topic 23 of 50 Languages & Compilers

Recursive Descent Parsing

Hand-written top-down parsing with mutually recursive functions.

How it works

Recursive descent parsing is a top-down technique in which each grammar nonterminal becomes a function that consumes tokens and calls functions for the nonterminals on its right-hand side. With one token of lookahead it handles LL(1) grammars directly, and precedence climbing or Pratt parsing handles expressions with operator precedence neatly. Hand-written recursive descent parsers are simple, flexible and give excellent error messages.

Step-by-step walkthrough

  1. Each grammar rule becomes a function.
  2. parseExpr calls parseTerm.
  3. parseTerm calls parseFactor.
  4. parseFactor consumes a number or parenthesized expression.
  5. Functions return as rules match, so it is easy to hand-write.

Where it is used

Many production compilers and tools, including GCC's C and C++ front ends, Clang, Go, Rust and many JSON and configuration parsers, use hand-written recursive descent.

Common pitfall

Left-recursive rules cause infinite recursion and must be rewritten as loops. Grammars needing more than limited lookahead require backtracking, which can become exponential without memoization, as in packrat parsing. Deep nesting can overflow the stack. Handle error recovery by synchronizing on tokens such as semicolons, and write tests for each grammar rule.

Where it fits in the course

Recursive Descent Parsing is topic 23 of 50 in Languages & Compilers, rated Intermediate. It follows Compiler Phases. The next topic is LL and LR Parsing.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Recursive Descent Parsing?
Hand-written top-down parsing with mutually recursive functions. It belongs to Languages & Compilers and is an intermediate-level topic.
Where do people use Recursive Descent Parsing?
Many production compilers and tools, including GCC's C and C++ front ends, Clang, Go, Rust and many JSON and configuration parsers, use hand-written recursive descent.
What is the most common mistake with Recursive Descent Parsing?
Left-recursive rules cause infinite recursion and must be rewritten as loops.
What should I learn before this topic?
Start with Compiler Phases, the topic just before it in Languages & Compilers.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Languages & Compilers quiz.
Is Recursive Descent Parsing hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Recursive Descent Parsing?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Recursive Descent Parsing?
Compiler Phases, LL and LR Parsing, Generics and Templates are closely connected. The full list is in the related topics section of this page.
What should I study after Recursive Descent Parsing?
Continue with LL and LR Parsing, the next topic in Languages & Compilers.

Related topics