# 递归下降解析器

递归下降解析器是一种自顶向下解析器，它为文法中的每个非终结符各建一个过程：要匹配一个终结符，过程就把它与下一个输入词法单元比较并消耗掉；要匹配一个非终结符，过程就调用相应的过程，而该过程可能递归调用，这项技术因此得名。<sup>[1](https://www.cs.rochester.edu/u/nelson/courses/csc_173/grammars/parsing.html)</sup> 由于解析器从根向下构建解析树，它属于自顶向下（LL）解析器家族。<sup>[2](https://www.cs.wustl.edu/~cytron/cacweb/Chapters/5/)</sup> 诸如 `<statement> ::= if <expression> then <statement>` 的文法规则会变成一个 `parseStatement()` 例程，它调用 `parseExpression()`，并递归调用 `parseStatement()`。<sup>[3](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup>

| 关键事实 | 细节 |
|---|---|
| 解析器家族 | 自顶向下（LL）；执行路径沿解析树做从左到右的深度优先遍历<sup>[2](https://www.cs.wustl.edu/~cytron/cacweb/Chapters/5/)</sup><sup> • </sup><sup>[4](https://www.sciencedirect.com/topics/computer-science/parsing)</sup> |
| 文法要求 | 预测式（非回溯）解析器要求 LL(k) 文法：接下来的 k 个词法单元必须能决定应用哪条产生式；必须消除左递归和公共前缀<sup>[5](https://devacomputerscience.in/toc/syntax%20analysis.pdf)</sup><sup> • </sup><sup>[3](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup> |
| 时间复杂度 | 预测式解析时为 O(n)，n 为输入长度；回溯变体不保证终止，最坏情况下可能耗时指数级<sup>[6](https://desertthunder.github.io/garden/programming/algorithms/recursive-descent/)</sup><sup> • </sup><sup>[7](https://www.baeldung.com/cs/ll-vs-lr-parsing)</sup> |
| 左递归 | 直接或间接左递归规则会导致不消耗输入就立即自调用，因此无法为这种文法编写递归下降解析器<sup>[8](https://www.gm.th-koeln.de/~ehses/compiler/parsdemo-1.1/recintro.html)</sup> |
| 实际应用 | 手写递归下降用于 GCC 和 Clang 的 C/C++ 前端、Java 编译器的解析器、V8 和 Roslyn<sup>[9](https://github.com/PetarKirov/sparkles/blob/main/docs/research/parsing/theory/top-down.md)</sup><sup> • </sup><sup>[10](https://github.com/munificent/craftinginterpreters/blob/master/book/parsing-expressions.md)</sup> |
| 生成器输出 | 许多 LL(1) 解析器生成器（包括 Coco/R 和 JavaCC 系列工具）生成递归下降代码，而不是解析表<sup>[11](https://citeseerx.ist.psu.edu/document?doi=fa60fc44c0a6431439ea7950c1320d22e828d1ff&repid=rep1&type=pdf)</sup><sup> • </sup><sup>[12](https://web.ist.utl.pt/antonio.menezes.leitao/Rosetta/FinalReport/dissertations/HugoCorreiaThesis.pdf)</sup> |
| 扩展思路 | 普拉特（Pratt）解析用绑定力表处理运算符优先级，而不是为每个优先级各写一个函数；PEG 和 packrat 解析器是带有序选择和记忆化的递归下降<sup>[13](https://ori-lang.com/docs/compiler-design/04-parser/)</sup><sup> • </sup><sup>[14](https://peps.python.org/pep-0617/)</sup> |

## 工作原理

解析器是文法规则到命令式代码的字面翻译。<sup>[10](https://github.com/munificent/craftinginterpreters/blob/master/book/parsing-expressions.md)</sup> 每个非终结符的过程都尝试匹配其某条产生式的右部。`match` 子程序在当前词法单元等于预期符号时消耗它，否则报告语法错误；这些调用形成的执行路径沿解析树做从左到右的深度优先遍历。<sup>[4](https://www.sciencedirect.com/topics/computer-science/parsing)</sup> 照这样写，这种解析器只判定输入是否属于该语言；在解析过程中逐步构建解析树则是很直接的补充。<sup>[1](https://www.cs.rochester.edu/u/nelson/courses/csc_173/grammars/parsing.html)</sup>

**产生式的选择。** 当一个非终结符有多条产生式时，过程必须选择一条。在预测式解析器中，这一选择由固定的前瞻决定：能够开始某条产生式右部的终结符集合就是它的 FIRST 集；如果某个右部能推导出空串，则决策还要使用左部非终结符的 FOLLOW 集，即在一次解析中可能紧跟在它后面的终结符。<sup>[1](https://www.cs.rochester.edu/u/nelson/courses/csc_173/grammars/parsing.html)</sup> 递归下降可以使用回溯，并不局限于 LL(1) 文法，但朴素回溯对任意上下文无关文法不保证终止。对于不使用回溯、只向前看一个词法单元的预测式解析，共享同一左部的产生式必须具有互不相交的预测集；满足这一条件的文法是 LL(1) 文法。<sup>[1](https://www.cs.rochester.edu/u/nelson/courses/csc_173/grammars/parsing.html)</sup>

另一种做法是回溯：尝试一条产生式，如果失败，就恢复输入位置并尝试另一条。回溯式递归下降可以处理 LL(k) 集合之外的文法，而表驱动预测式解析器完全无法处理这些文法；但对非 LL 文法不保证终止，即使解析成功，最坏情况下也可能耗时指数级。<sup>[7](https://www.baeldung.com/cs/ll-vs-lr-parsing)</sup> packrat 解析器通过记忆化每个输入位置上已经匹配过的规则来消除这一代价，往往只需相当直接的结构改动，就能把回溯式递归下降解析器转为线性时间，代价是更大的内存占用。<sup>[15](https://arxiv.org/abs/cs/0603077)</sup><sup> • </sup><sup>[14](https://peps.python.org/pep-0617/)</sup>

## 文法要求与变换

文法的两个性质会阻碍确定性递归下降。<sup>[1](https://www.cs.rochester.edu/u/nelson/courses/csc_173/grammars/parsing.html)</sup>

**左递归。** 如果某个非终结符能推导出以自身开头的句型，该文法就是左递归的，例如 `<expr> ::= <expr> + <term>`。选中这样一条规则后，解析过程的第一个动作就是不消耗任何输入而调用自身，因此无法为包含直接或间接左递归规则的文法编写递归下降解析器，这样的文法也不可能是 LL(1)。<sup>[8](https://www.gm.th-koeln.de/~ehses/compiler/parsdemo-1.1/recintro.html)</sup> 标准补救办法是变换：`<expr> ::= term ('+' term)*` 接受相同的字符串，但解析树被压平了，必须进行后处理才能恢复自然的左结合分组。<sup>[14](https://peps.python.org/pep-0617/)</sup> 改写后的文法保持语言不变，却扭曲了解析树，往往产生向右倾斜的树，除非手工重建，否则 `-` 和 `/` 的左结合性会丢失。<sup>[9](https://github.com/PetarKirov/sparkles/blob/main/docs/research/parsing/theory/top-down.md)</sup>

**公共前缀。** 同一非终结符的两条产生式若以相同符号开头，就无法用一个词法单元的前瞻来区分。左因子化把决策推迟到共享前缀之后：表达式文法 `<E> → <T> + <E> | <T> − <E> | <T>` 变为 `<E> → <T> <E*>`，其中 `<E*> → + <T> <E*> | − <T> <E*> | ε`。<sup>[1](https://www.cs.rochester.edu/u/nelson/courses/csc_173/grammars/parsing.html)</sup>

任何上下文无关文法都可以变换为不含左递归的等价文法，但消除左递归并不总能得到 LL(k) 文法。<sup>[5](https://devacomputerscience.in/toc/syntax%20analysis.pdf)</sup> 正因有这些限制，编写和修改 LL(1) 文法在编译器教材中才被视为一项独立的技能。<sup>[16](https://www.sciencedirect.com/topics/computer-science/recursive-descent)</sup>

## 变体与扩展

**普拉特解析。** 当表达式涉及大量优先级层次时，普拉特（Pratt）解析提供了一种专门的递归下降形式：一个循环检查绑定力表，以决定某个运算符是接管当前表达式，还是让位给优先级更高的调用者。编译器可以在声明、语句和其他构造上仍使用递归下降，只在表达式上切换到普拉特循环。<sup>[13](https://ori-lang.com/docs/compiler-design/04-parser/)</sup>

**PEG 与 packrat 解析。** 解析表达式文法（Parsing Expression Grammar，PEG）通过指定一个识别该语言的递归下降解析器来定义语言，而 PEG 解析器通常正是这样构造的：每条文法规则一个函数。<sup>[17](https://arxiv.org/abs/1509.02439)</sup><sup> • </sup><sup>[14](https://peps.python.org/pep-0617/)</sup> 与上下文无关文法解析器不同，PEG 的选择运算符是有序的：备选项按书写顺序尝试，能解析的字符串恰好只有一棵有效解析树，因此 PEG 解析器不会产生歧义。CPython 在 Python 3.9（PEP 617）中用 PEG 解析器替换了基于 LL(1) 的解析器，以解除那些曾迫使文法采用别扭变通办法的 LL(1) 限制。<sup>[14](https://peps.python.org/pep-0617/)</sup>

**左递归扩展。** 由于朴素递归下降无法处理左递归，研究者构建了能够处理的变体。Pika 解析（Pika parsing）把 packrat 解析重新表述为一种逆向解析输入的动态规划算法，可处理直接和间接左递归，并能从语法错误中实现最优恢复，这一特性对集成开发环境（IDE）和编译器很有意义。<sup>[18](https://arxiv.org/abs/2005.06444)</sup>

## 实际地位

递归下降解析器被描述为最容易手工编写的解析器之一，并且为检测和修复语法错误提供了很好的机会；构造良好的递归下降解析器甚至可能比表驱动的 LR(1) 解析器更快。它们还让编译器作者能够处理会困扰 LR(1) 解析器的歧义，例如可用作标识符的关键字名称。<sup>[19](https://booksite.elsevier.com/samplechapters/9780120884780/Chapter_3.pdf)</sup> 尽管 YACC 等 LR/LALR 工具具有解析能力，程序员仍常因灵活性、错误处理和易调试性而选择手写递归下降；ANTLR 生成器的设计目标就是把这种手写灵活性与生成器的便利结合起来，产生带谓词的 LL(k) 解析器，其谓词利用语义和语法上下文指导解析。<sup>[20](https://onlinelibrary.wiley.com/doi/10.1002/spe.4380250705)</sup>

生成的递归下降代码有具体优势：语义动作很容易嵌入各例程，宿主语言的参数机制无需额外工作即可使用，非回溯解析器往往比表驱动解析器更高效，动态冲突解决器也容易实现。主要缺点是体积，因为递归下降解析器通常比表驱动解析器（连同其解析表）更大。<sup>[11](https://citeseerx.ist.psu.edu/document?doi=fa60fc44c0a6431439ea7950c1320d22e828d1ff&repid=rep1&type=pdf)</sup>

**使用场景。** 手写递归下降是许多实际投入使用的语言的参考解析器背后的技术：GCC 和 Clang 的 C/C++ 前端、Java 编译器手工构建的解析器（在 ALL(*) 工作中被用作基准）、V8 和 Roslyn。<sup>[9](https://github.com/PetarKirov/sparkles/blob/main/docs/research/parsing/theory/top-down.md)</sup><sup> • </sup><sup>[10](https://github.com/munificent/craftinginterpreters/blob/master/book/parsing-expressions.md)</sup> 2022 年一项关于解析器生成的综述指出，几乎所有现代语言都使用手写的递归下降解析器，而这一过程漫长且容易出错，因为自动生成被认为对真实编程语言不切实际。<sup>[21](https://arxiv.org/abs/2209.08383)</sup> 尽管如此，在真实语言的规模上，手工构造仍然耗时且易错，这也是编译器教材还要介绍解析阶段自动化背后原理的原因。<sup>[2](https://www.cs.wustl.edu/~cytron/cacweb/Chapters/5/)</sup>

## References

1. [Parsing, CSC 173 course notes, University of Rochester](https://www.cs.rochester.edu/u/nelson/courses/csc_173/grammars/parsing.html)
2. [Crafting a Compiler, Chapter 5: Top-Down Parsing, Washington University in St. Louis](https://www.cs.wustl.edu/~cytron/cacweb/Chapters/5/)
3. [The History of Parsing: From BNF to ANTLR, Geschichte der Informatik](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)
4. [Parsing – an overview, ScienceDirect Topics](https://www.sciencedirect.com/topics/computer-science/parsing)
5. [Recursive Descent Predictive Parsing, Deva Computer Science](https://devacomputerscience.in/toc/syntax%20analysis.pdf)
6. [Recursive descent parsing, Owais' Places](https://desertthunder.github.io/garden/programming/algorithms/recursive-descent/)
7. [LL vs. LR Parsing, Baeldung on Computer Science](https://www.baeldung.com/cs/ll-vs-lr-parsing)
8. [Introduction to Recursive Descent Parsing, TH Köln](https://www.gm.th-koeln.de/~ehses/compiler/parsdemo-1.1/recintro.html)
9. [Top-down parsing research notes, PetarKirov/sparkles, GitHub](https://github.com/PetarKirov/sparkles/blob/main/docs/research/parsing/theory/top-down.md)
10. [Parsing Expressions, Crafting Interpreters, GitHub](https://github.com/munificent/craftinginterpreters/blob/master/book/parsing-expressions.md)
11. [Compiler design text, Section 8.2.6 LL(1) and recursive descent, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=fa60fc44c0a6431439ea7950c1320d22e828d1ff&repid=rep1&type=pdf)
12. [Hugo Correia, thesis comparing parser generators, IST Lisboa](https://web.ist.utl.pt/antonio.menezes.leitao/Rosetta/FinalReport/dissertations/HugoCorreiaThesis.pdf)
13. [Parser Overview, OriLang Compiler Design](https://ori-lang.com/docs/compiler-design/04-parser/)
14. [PEP 617 – New PEG parser for CPython](https://peps.python.org/pep-0617/)
15. [Packrat Parsing: Simple, Powerful, Lazy, Linear Time, arXiv](https://arxiv.org/abs/cs/0603077)
16. [Recursive Descent – an overview, ScienceDirect Topics](https://www.sciencedirect.com/topics/computer-science/recursive-descent)
17. [Parsing Expression Grammars Made Practical, arXiv](https://arxiv.org/abs/1509.02439)
18. [Pika parsing: reformulating packrat parsing as a dynamic programming algorithm, arXiv](https://arxiv.org/abs/2005.06444)
19. [Chapter 3 sample chapter, parser text, Elsevier](https://booksite.elsevier.com/samplechapters/9780120884780/Chapter_3.pdf)
20. [Parr and Quong (1995), ANTLR: A predicated-LL(k) parser generator, Software: Practice and Experience](https://onlinelibrary.wiley.com/doi/10.1002/spe.4380250705)
21. [Practical LR Parser Generation, arXiv](https://arxiv.org/abs/2209.08383)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 25, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 25, 2026*

Topic: 社会与历史 › 语言与语言学 › 语言学 › Formal and computational linguistics › Parsing algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/recursive-descent-parser

English original: https://www.edgepedia.org/recursive-descent-parser
