# 解析

解析是确定给定形式文法赋予特定字符串何种结构的过程，通常产生一棵树或一张图，显示该字符串如何由文法规则推导而来。<sup>[1](https://academic.oup.com/edited-volume/34563/chapter/293289103)</sup> 在编译器中，解析器主要负责识别语法，判定正在编译的程序是否是该语言句法模型中的合法句子。<sup>[2](https://www.sciencedirect.com/topics/computer-science/parsing)</sup> 在自然语言处理中，解析为输入赋予树结构，以便识别每个动词的主语和宾语，以及每个修饰语修饰什么。<sup>[3](https://www.cambridge.org/core/books/abs/computational-linguistics/syntax-analysis/2B6220E2EE873738543EB7F7835D6A0A)</sup> 同样的操作也服务于数据格式读取器和协议实现，凡是需要对符号序列作结构化解释之处皆然。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 确定文法在其语言内赋予字符串的结构；对于字符串 s 和文法 G，解析器试图构造一个构造性证明，证明 s 可在 G 中推导出来。<sup>[1](https://academic.oup.com/edited-volume/34563/chapter/293289103)</sup><sup> • </sup><sup>[2](https://www.sciencedirect.com/topics/computer-science/parsing)</sup> |
| 记法 | 巴科斯-诺尔范式（BNF）：巴科斯（Backus）于1959年用“元语言公式”描述ALGOL 58；诺尔（Naur）于1960年在《ACM通讯》上将该记法应用于ALGOL 60。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup> |
| 方向 | 自顶向下解析器从文法的开始符号出发并展开非终结符；自底向上解析器从输入向上工作，而且大多数自顶向下文法也可自底向上解析。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup><sup> • </sup><sup>[2](https://www.sciencedirect.com/topics/computer-science/parsing)</sup> |
| 通用算法 | 厄利算法（Earley's algorithm，1970）在最坏情况下以立方时间识别一般上下文无关文法；CYK算法以科克（Cocke）、卡萨米（Kasami）和扬格（Younger）命名，使用自底向上动态规划。<sup>[5](https://www.sciencedirect.com/science/article/pii/S0065245808604519)</sup><sup> • </sup><sup>[6](https://plato.stanford.edu/archives/fall2019/entries/computational-linguistics/)</sup> |
| 歧义输出 | 富田（Tomita）的GLR解析器返回一个共享紧凑解析森林（SPPF），表示一个字符串的所有推导，最坏情况下具有无界多项式阶。<sup>[5](https://www.sciencedirect.com/science/article/pii/S0065245808604519)</sup> |
| 现代工具 | ANTLR 4（2013）引入了自适应LL()（ALL()）解析，动态计算前瞻信息，并对大多数真实输入保持线性时间。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup> |
| 自然语言资源 | 通用依存（Universal Dependencies）提供150多种语言的200多个树库，由600多名贡献者构建，用于多语言解析器开发。<sup>[7](https://universaldependencies.org/)</sup> |

## 作为记法的文法

20世纪60年代初以前，编译器把语言定义深埋在代码之中，这使得验证编译器是否接受该语言的全部语法且仅接受该语法变得非常困难；当时不存在描述文法的统一方法。<sup>[8](https://www.andrews.edu/~bidwell/456/history.html)</sup> 变化随BNF而来：其中每条规则定义非终结符如何被备选项替换，用双冒号等号（::=）表示“可替换为”，用竖线表示“或”。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup>

BNF不仅用于规定语言，也用于设计语言：因为把文法写成BNF迫使语言设计者以英语文字叙述无法做到的方式保持精确，任何歧义——即一个句子有不止一种推导——都会在形式记法中清楚地显现出来。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup> ALGOL 60报告的文法是第一个使用后来成为标准记法的形式语言规范。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup> [尼克劳斯·维尔特](https://www.edgepedia.org/zh/niklaus-wirth)（Niklaus Wirth）后来在1977年《ACM通讯》的一则短文中将BNF扩展为EBNF，增加了重复和可选运算符，ISO于1996年将EBNF标准化。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup>

## 自顶向下与自底向上策略

最早的解析算法采用自顶向下方式，从文法的开始符号开始，按照文法规则反复展开非终结符，试图产生输入字符串。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup> 在称为递归下降或LL解析的自顶向下风格中，文法被重新组织，使定义给定非终结符的每条规则的第一个符号指示应选择哪条规则。<sup>[8](https://www.andrews.edu/~bidwell/456/history.html)</sup> 自顶向下解析器预测解析树的根将由某个产生式展开，然后将所得符号与扫描器给出的记号进行匹配，如果记号不同则宣布语法错误。<sup>[2](https://www.sciencedirect.com/topics/computer-science/parsing)</sup>

自底向上解析器，包括移进-归约族和LR族，从输入向上构建树。两个方向都有典型弱点：自顶向下解析器在回溯期间反复重新求值相同的子短语，并在左递归规则上无限循环；而朴素的自底向上解析器会被贪婪冲突困住。<sup>[9](https://www.cosmiclearn.com/nltk/chart-parsing.php)</sup> 在实践中，实际使用的编程语言中的表达式通常由嵌入[递归下降解析器](https://www.edgepedia.org/zh/recursive-descent-parser)内部的专用算法处理，因为自顶向下解析可以使用自顶向下运算符优先级（Pratt）解析等技术高效处理嵌套运算符表达式。<sup>[10](https://jeffreykegler.github.io/Ocean-of-Awareness-blog/individual/2014/11/ll.html)</sup><sup> • </sup><sup>[17](https://tdop.github.io/)</sup>

## 动态规划与图表解析

图表解析使用动态规划来弥补两种朴素策略的弱点。它把已完成的子短语和部分假设记录在称为图表的记忆化表中，并附带解析器任务的优先议程；这消除了冗余计算，处理所有形式的递归，并在多项式时间内定位所有有效解析树。<sup>[9](https://www.cosmiclearn.com/nltk/chart-parsing.php)</sup><sup> • </sup><sup>[1](https://academic.oup.com/edited-volume/34563/chapter/293289103)</sup> 在图表中，被动边表示完全识别的成分，主动边表示部分识别的成分，每个解析器应用规则将它们组合起来。<sup>[9](https://www.cosmiclearn.com/nltk/chart-parsing.php)</sup>

两种经典的通用算法都适合这一框架。CYK算法以约翰·科克（John Cocke）、卡萨米（Tadao Kasami）和丹尼尔·扬格（Daniel Younger）命名，采用自底向上方式：它先列出长度为1的句子片段可能的非终结符标签，再列出长度为2的，依此类推，始终基于先前发现的片段类型构建。<sup>[6](https://plato.stanford.edu/archives/fall2019/entries/computational-linguistics/)</sup> 杰伊·厄利（Jay Earley）1970年发表于《ACM通讯》的算法，在一般上下文无关文法上以最坏情况立方时间检验一个字符串能否从文法推导出来。<sup>[5](https://www.sciencedirect.com/science/article/pii/S0065245808604519)</sup> 对于超出上下文无关类的文法，代价上升：树邻接文法和组合范畴文法用当前算法在最坏情况下以 \( O(n^{6}) \) 时间解析，而上下文无关文法是 \( O(n^{3}) \)。<sup>[6](https://plato.stanford.edu/archives/fall2019/entries/computational-linguistics/)</sup>

## 处理歧义

歧义是自然语言解析中的核心实际问题。当一个文法能以不止一种方式推导一个句子时，通用解析器必须返回所有读法：富田的GLR解析器返回一个共享紧凑解析森林，即给定字符串所有推导的紧凑表示。<sup>[5](https://www.sciencedirect.com/science/article/pii/S0065245808604519)</sup> 规模可能很大。在一项解析器比较评估中，三个测试文法显示出截然不同的歧义程度：任务特定口语语言文法每句5.4个解析，从树库提取的ATIS文法每句940个解析，而从[宾州树库](https://www.edgepedia.org/zh/penn-treebank)（Penn Treebank）提取的文法每句 \( 7.2 \times 10^{27} \) 个解析。<sup>[11](https://users.sussex.ac.uk/~johnca/elsps.html)</sup>

对于概率解析器，评估指标决定算法。大多数解析算法，包括[维特比算法](https://www.edgepedia.org/zh/viterbi-algorithm)，优化得到正确标记树的概率，但也存在分别最大化期望标记召回率和括号召回率的算法，选择与指标匹配的算法可改善实测性能。<sup>[12](https://www.microsoft.com/en-us/research/publication/parsing-algorithms-and-metrics/)</sup>

## 解析器生成器与现代工具

解析器生成器从文法描述自动产生解析器，取代手工编码的状态机。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup> ANTLR 4于2013年发布，使用自适应LL()（ALL()）解析，这是一种在解析期间而非静态计算前瞻信息的动态算法，消除了大多数文法限制，同时为大多数真实输入保持线性时间；它成为许多复杂语言工具首选的解析器生成器，包括Eclipse IDE语言工具以及众多领域特定和查询语言实现。<sup>[4](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)</sup>

对于一般文法，研究继续围绕共享的立方时间核心展开。一个2020年的算法将解析表示为对正则语言和关系的操作，最坏情况为立方，对无歧义文法为平方，对LR正则文法为线性，其概念验证实现优于当前厄利、GLR和GLL的最佳实现。<sup>[13](https://www.sigplan-www.sigplan.hosting.acm.org/OpenTOC/pldi20.html)</sup>

## 当今自然语言解析

在自然语言处理中，解析后的结构为命名实体识别、信息抽取和机器翻译等任务提供输入。<sup>[2](https://www.sciencedirect.com/topics/computer-science/parsing)</sup> 该领域的共享基础设施是[通用依存](https://www.edgepedia.org/zh/universal-dependencies)，这是一个跨语言一致标注词性、形态特征和句法依存的框架，是一项开放社区努力，拥有600多名贡献者和150多种语言的200多个树库。<sup>[7](https://universaldependencies.org/)</sup> 该项目维护解析准确率的官方评估，其共享任务评分器将分词器、词形还原器、标注器和解析器输出与金标准数据进行比较。<sup>[14](https://github.com/UniversalDependencies/tools/blob/master/udtools/README.md)</sup>

依存解析评估由两个指标主导：无标签依存正确率（UAS），衡量预测的中心词-依存词关系的正确性；有标签依存正确率（LAS），评估依存标签的准确率；成分句法分析用短语结构匹配的F1分数衡量。<sup>[15](https://journals.sagepub.com/doi/10.3233/ATDE250654)</sup>

## 比较解析器效率

仅凭理论最坏情况界并不能预测实际性能，因为行为取决于文法、输入和实现；因此经验评估已成为比较解析系统的既定手段。<sup>[16](https://aclanthology.org/W00-1601.pdf)</sup> 但除非使用相同的文法、测试数据、实现语言和机器，跨研究比较原始时间并不可靠。微软研究院的鲍勃·摩尔（Bob Moore）提出的一种方法在COLING 2000解析器效率研讨会上获得广泛认同：研究人员在共享测试数据上运行自己的解析器，并报告执行时间，以标准算法的参考实现所记录时间的百分比表示，该参考实现用相同语言在同一机器上编写，从而剔除语言和平台影响。<sup>[11](https://users.sussex.ac.uk/~johnca/elsps.html)</sup>

## References

1. [parsing, Glossary, The Oxford Handbook of Computational Linguistics, Oxford Academic](https://academic.oup.com/edited-volume/34563/chapter/293289103)
2. [Parsing - an overview, ScienceDirect Topics](https://www.sciencedirect.com/topics/computer-science/parsing)
3. [Syntax analysis, Chapter 2, Ralph Grishman, Computational Linguistics, Cambridge University Press](https://www.cambridge.org/core/books/abs/computational-linguistics/syntax-analysis/2B6220E2EE873738543EB7F7835D6A0A)
4. [The History of Parsing: From BNF to ANTLR, Geschichte der Informatik](https://www.geschichte-der-informatik.de/articles/the_history_of_parsing/)
5. [Parsing of General Context-Free Languages, Advances in Computers Vol. 14, ScienceDirect](https://www.sciencedirect.com/science/article/pii/S0065245808604519)
6. [Computational Linguistics, Stanford Encyclopedia of Philosophy (Fall 2019)](https://plato.stanford.edu/archives/fall2019/entries/computational-linguistics/)
7. [Universal Dependencies](https://universaldependencies.org/)
8. [History of Parsing Methods, Andrews University](https://www.andrews.edu/~bidwell/456/history.html)
9. [NLTK Chart Parsing: Top-Down, Bottom-Up & Earley, CosmicLearn](https://www.cosmiclearn.com/nltk/chart-parsing.php)
10. [Parsing: Top-down versus bottom-up, Ocean of Awareness blog, Jeffrey Kegler](https://jeffreykegler.github.io/Ocean-of-Awareness-blog/individual/2014/11/ll.html)
11. [Efficiency in Large-scale Parsing Systems - Parser Comparison, John Carroll, University of Sussex](https://users.sussex.ac.uk/~johnca/elsps.html)
12. [Parsing Algorithms and Metrics, Joshua Goodman, Microsoft Research](https://www.microsoft.com/en-us/research/publication/parsing-algorithms-and-metrics/)
13. [Faster general parsing through context-free memoization, PLDI 2020 proceedings](https://www.sigplan-www.sigplan.hosting.acm.org/OpenTOC/pldi20.html)
14. [udtools README, Universal Dependencies tools, GitHub](https://github.com/UniversalDependencies/tools/blob/master/udtools/README.md)
15. [Optimization of English Syntactic Structure Parsing Based on Large Language Models, SAGE Journals](https://journals.sagepub.com/doi/10.3233/ATDE250654)
16. [Efficient Large-Scale Parsing - a Survey, John Carroll and Stephan Oepen, ACL Anthology](https://aclanthology.org/W00-1601.pdf)
17. [TDOP Pratt](https://tdop.github.io/)

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

Topic: 社会与历史 › 语言与语言学 › 语言学 › 形式与计算语言学 › 句法分析算法

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/parsing

English original: https://www.edgepedia.org/parsing
