# tree-sitter

**Tree-sitter** 是一个解析器生成工具，也是一个增量解析库，它为源文件构建具体语法树，并在文件被编辑时高效地更新该语法树。<sup>[1](https://github.com/tree-sitter/tree-sitter/blob/master/README.md)</sup> 它由 Max Brunsfeld 创建，最初是他的业余项目，后来由他在 GitHub 的 Atom 编辑器团队工作期间继续开发。<sup>[2](https://www.fit.vut.cz/person/meduna/public/work/lib/exe/fetch.php?media=lectures%3Alta%3A2023%3Axboave00_xmelot00_abstract.pdf)</sup> 该项目提出了四个设计目标：足够通用，能解析任何编程语言；足够快，能在文本编辑器中每次击键时进行解析；足够健壮，在存在语法错误时也能给出有用的结果；以及无依赖，使以纯 C11 编写的运行时库可以嵌入任何应用。<sup>[1](https://github.com/tree-sitter/tree-sitter/blob/master/README.md)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 一个解析器生成工具和增量解析库，生成随代码编辑而更新的具体语法树<sup>[1](https://github.com/tree-sitter/tree-sitter/blob/master/README.md)</sup> |
| 组件 | 用于嵌入的 C 运行时库（`libtree-sitter`），以及一个 Rust 命令行 CLI，可从 crates.io、npm 获取，也提供预构建的二进制文件<sup>[3](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/5-implementation.md)</sup> |
| 解析器生成 | `generate` 命令从 `grammar.js` 读取上下文无关文法，并输出一个 C 文件形式的解析器，即 `parser.c`<sup>[3](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/5-implementation.md)</sup> |
| 歧义处理 | 局部歧义的文法使用 GLR 解析，它并行探索多种解释；预期的冲突通过 `%conflicts` 指令声明<sup>[4](https://github.com/ambs/tree-sitter-bnf-tools/blob/main/docs/tutorial/03-concepts.md)</sup> |
| 错误容忍 | 解析器无法识别的文本在语法树中表示为 `(ERROR)` 节点，而不是停止解析<sup>[5](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html)</sup> |
| 官方语言绑定 | C#、Go、Haskell、Java（JDK 22+）、JavaScript（Node.js）、JavaScript（Wasm）、Kotlin、Python、Rust、Swift 和 Zig<sup>[6](https://tree-sitter.github.io/tree-sitter/)</sup> |
| 著名部署 | GitHub 基于搜索的代码导航建立在 tree-sitter 之上，覆盖一系列列出的语言，包括 C、C#、Go、Java、JavaScript、Python、Ruby、Rust 和 TypeScript<sup>[7](https://docs.github.com/en/repositories/working-with-files/using-files/navigating-code-on-github)</sup> |

## 工作原理

输出是一棵具体语法树（CST），其中每个节点直接对应文法中的一个终结符或非终结符；这与编译器产生的抽象语法树不同，后者丢弃了语法细节。<sup>[8](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/creating-parsers/3-writing-the-grammar.md)</sup> 由于树是具体的，编辑器和高亮器等工具可以依赖它来精确反映源文本。

解析器在设计上是错误容忍的。当它遇到无法识别的文本时，会在树中插入一个 `(ERROR)` 节点并继续解析，因此工具即使面对尚无法编译的代码也总能获得可用的树。<sup>[5](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html)</sup> 这一特性对编辑器很重要，因为代码在大多数时候语法上是不完整的；对必须处理任意真实世界文件的工具也很重要；一个建立在 tree-sitter 之上的语法代码搜索系统报告称，在其基准测试中使用标准错误恢复处理了每个文件，包括另一个工具完全无法解析的文件。<sup>[9](https://www2.eecs.berkeley.edu/Pubs/TechRpts/2024/EECS-2024-93.pdf)</sup>

**歧义与 GLR。** 如果文法存在局部歧义——即单个前瞻记号会留下多种解析可能——tree-sitter 会从 LR 算法切换到 GLR（广义 LR），后者同时跟踪多种解释，并使用文法作者提供的规则在它们之间做出裁决。<sup>[4](https://github.com/ambs/tree-sitter-bnf-tools/blob/main/docs/tutorial/03-concepts.md)</sup> 在没有指导的情况下，文法生成会因冲突错误而中止；`%conflicts` 指令声明某个歧义是预期且有意为之的，而冲突解决启发式通常偏好更长的“移进”解析，这就是悬空 `else` 被绑定到内层 `if` 的方式。<sup>[4](https://github.com/ambs/tree-sitter-bnf-tools/blob/main/docs/tutorial/03-concepts.md)</sup> 在运行时，当声明的冲突发生时，tree-sitter 探索所有可能的解释，如果多个解析成功，则选择其规则具有最高总动态优先级的子树。<sup>[10](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/creating-parsers/2-the-grammar-dsl.md)</sup>

**词法分析。** 解析分为两个阶段：解析和词法分析，即把字符分组为记号。词法分析是上下文感知的，并在解析过程中按需执行，因此在任何位置，词法分析器只尝试识别该位置有效的记号。当多个有效记号匹配相同字符时，tree-sitter 通过显式词法优先级、然后匹配长度、然后偏好 `String` 记号而非同等地位的 `RegExp` 记号、最后按文法中的规则顺序来解决冲突。<sup>[8](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/creating-parsers/3-writing-the-grammar.md)</sup>

## 文法编写与 CLI

Tree-sitter 由两个组件组成：C 库 `libtree-sitter`，嵌入应用中并生成和维护语法树；以及 `tree-sitter` CLI，一个用 Rust 编写的构建工具，在解析器生成后就不再需要。<sup>[3](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/5-implementation.md)</sup> `generate` 命令是 CLI 的核心功能：它从 `grammar.js` 接收上下文无关文法作为输入，并生成一个用 C 编写的解析器，保存为 `parser.c`。<sup>[3](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/5-implementation.md)</sup> CLI 通过调用 `node` 来求值 [JavaScript](https://www.edgepedia.org/zh/javascript) 文法文件，将其转换为由形式化模式指定的 JSON 格式，然后转换文法，将其拆分为描述非终结符的语法文法和描述终结符如何由单个字符组成的词法文法。<sup>[3](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/5-implementation.md)</sup>

由于生成的解析器是一个无依赖的 C 程序，它可以从许多不同的语言中调用；该项目发布了 C#、Go、Haskell、Java、JavaScript（Node.js 和 Wasm）、Kotlin、Python、Rust、Swift 和 Zig 的官方绑定，此外还有许多第三方绑定。<sup>[6](https://tree-sitter.github.io/tree-sitter/)</sup> 这种设计的一个后果是，当 tree-sitter 本身更新时，每个生成的解析器都必须重新构建，才能利用新功能或错误修复。<sup>[11](https://pmc.ncbi.nlm.nih.gov/articles/PMC12425133/)</sup> Tree-sitter 的文法被描述为类似于 Yacc 和 Bison 文法但限制更少，并且不同于 ANTLR 文法、解析表达式文法或语言规范中常用的歧义文法。<sup>[8](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/creating-parsers/3-writing-the-grammar.md)</sup>

## 查询与代码导航

Tree-sitter 包含一种查询语言，其中每个模式都是一个 S 表达式，匹配语法树中的一组节点；模式可以指定节点类型、子结构和字段名，使匹配更具体。<sup>[5](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html)</sup> 结合查询，tree-sitter 支持代码导航系统：`tree-sitter tags` 命令输出文件中感兴趣的语法节点的文本转储，使用一种捕获约定，其中 `@role.kind` 标记实体的角色（定义或引用）和种类（类定义、函数调用、变量引用等），内部 `@name` 捕获提取标识符。<sup>[12](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/4-code-navigation.md)</sup>

这种方法最著名的部署是 GitHub 的代码导航，它使用开源的 tree-sitter 库搜索仓库中的所有定义和引用，以查找具有给定名称的实体。GitHub 列出的支持语言包括 Bash、C、C#、C++、CodeQL、Elixir、Go、JSX、Java、JavaScript、Lua、PHP、Protocol Buffers、Python、R、Ruby、Rust、Scala、Starlark、Swift 和 [TypeScript](https://www.edgepedia.org/zh/typescript)，并为这些语言的仓库自动启用导航，无需配置。<sup>[7](https://docs.github.com/en/repositories/working-with-files/using-files/navigating-code-on-github)</sup>

## 在编辑器和工具中的采用

Tree-sitter 被描述为一种增量解析系统，适用于快速语法高亮、代码导航、重构和结构化搜索。<sup>[13](https://github.com/HerringtonDarkholme/awesome-tree-sitter)</sup>

除了编辑器之外，tree-sitter 还被用作研究和开发者工具中的解析前端：ACER 调用图生成器使用它与任何语言对接，<sup>[14](https://arxiv.org/abs/2308.15669)</sup> 代码相似性研究也将它与基于 ANTLR 的解析进行了比较。<sup>[15](https://aclanthology.org/anthology-files/pdf/acl/2024.acl-short.3.pdf)</sup>

## Tree-sitter 与 ANTLR 的比较

Tree-sitter 和 ANTLR 在解析器生成器的设计空间中占据不同的位置。ANTLR 接收 `.g4` 格式的文法，并生成一个完整、独立的解析器，目标语言如 Java、C#、Python 或 JavaScript，以及用于遍历解析树的监听器和访问者类；生成器本身即使目标语言是另一种语言也需要 Java 运行时。<sup>[16](https://www.faun.dev/toolbox/antlr-vs-tree-sitter/)</sup> Tree-sitter 则生成一个 C 解析器，通过其运行时库使用，并在许多宿主语言中提供绑定。<sup>[3](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/5-implementation.md)</sup>

在一项比较中，最大的差距是解析性能，据报道 tree-sitter 远远领先于 ANTLR。<sup>[16](https://www.faun.dev/toolbox/antlr-vs-tree-sitter/)</sup> ANTLR 4 引入了自适应预测用于错误恢复，但它无法匹敌 tree-sitter 的增量性能，这对于实时代码分析（例如在开发者输入时构建代码知识图谱）很重要。<sup>[17](https://inferensys.com/differences/ai-coding-agent-memory-and-repository-context-tools/code-knowledge-graph-platforms/tree-sitter-vs-antlr)</sup> 相反，ANTLR 丰富的遍历 API 和文法开发工具使其成为一次性深度分析、批处理流水线、学术语言研究以及没有现成 tree-sitter 文法的内部 DSL 的首选。<sup>[17](https://inferensys.com/differences/ai-coding-agent-memory-and-repository-context-tools/code-knowledge-graph-platforms/tree-sitter-vs-antlr)</sup><sup> • </sup><sup>[18](https://inferensys.com/differences/ai-software-factory-platforms/code-migration-and-modernization-ai/tree-sitter-vs-antlr-for-ai-assisted-code-parsing-in-modernization)</sup> 一项解析器生成器的排名比较将 tree-sitter 列为 78/100，ANTLR 为 77/100，总体几乎持平。<sup>[19](https://www.faun.dev/toolbox/compare-parser-generators/)</sup>

## 项目状态

开发活跃。版本 v0.26.10 于 2026 年 6 月 28 日发布在 Zenodo 上，<sup>[20](https://zenodo.org/records/21005449)</sup> GitHub 发布页面显示持续发布补丁版本直至 v0.26.13，修复了混合移进优先级下的结合性判定以及重复运算符中的解析错误等问题。<sup>[21](https://github.com/tree-sitter/tree-sitter/releases)</sup> 该项目的设计借鉴了已发表的增量解析研究，包括伯克利 Harmonia 项目的“高效灵活的增量解析”和“真实编程语言的增量分析”，以及上下文感知扫描和 LR 错误恢复方面的工作。<sup>[6](https://tree-sitter.github.io/tree-sitter/)</sup>

## References

1. [tree-sitter README, tree-sitter/tree-sitter, GitHub](https://github.com/tree-sitter/tree-sitter/blob/master/README.md)
2. [Tree-sitter: Parser Generator, lecture abstract, Brno University of Technology (FIT VUT)](https://www.fit.vut.cz/person/meduna/public/work/lib/exe/fetch.php?media=lectures%3Alta%3A2023%3Axboave00_xmelot00_abstract.pdf)
3. [Implementation, Tree-sitter documentation](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/5-implementation.md)
4. [Concepts, tree-sitter-bnf-tools tutorial, GitHub](https://github.com/ambs/tree-sitter-bnf-tools/blob/main/docs/tutorial/03-concepts.md)
5. [Query Syntax, Tree-sitter documentation](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html)
6. [Introduction, Tree-sitter documentation](https://tree-sitter.github.io/tree-sitter/)
7. [Navigating code on GitHub, GitHub Docs](https://docs.github.com/en/repositories/working-with-files/using-files/navigating-code-on-github)
8. [Writing the Grammar, Tree-sitter documentation](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/creating-parsers/3-writing-the-grammar.md)
9. [Syntactic Code Search with Sequence-to-Tree Matching, EECS-2024-93, UC Berkeley](https://www2.eecs.berkeley.edu/Pubs/TechRpts/2024/EECS-2024-93.pdf)
10. [The Grammar DSL, Tree-sitter documentation](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/creating-parsers/2-the-grammar-dsl.md)
11. [Pulse, Parse, and Ponder: Using Invisible XML to Dissect a Scientific Domain Specific Language, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC12425133/)
12. [Code Navigation Systems, Tree-sitter documentation](https://github.com/tree-sitter/tree-sitter/blob/master/docs/src/4-code-navigation.md)
13. [awesome-tree-sitter, GitHub](https://github.com/HerringtonDarkholme/awesome-tree-sitter)
14. [ACER: An AST-based Call Graph Generator Framework, arXiv:2308.15669](https://arxiv.org/abs/2308.15669)
15. [Revisiting Code Similarity Evaluation with Abstract Syntax Tree Edit, ACL 2024](https://aclanthology.org/anthology-files/pdf/acl/2024.acl-short.3.pdf)
16. [ANTLR vs Tree-sitter: Parser Generators Comparison, Faun](https://www.faun.dev/toolbox/antlr-vs-tree-sitter/)
17. [Tree-sitter vs ANTLR: Parsing for Code Graphs Compared, Inference Systems](https://inferensys.com/differences/ai-coding-agent-memory-and-repository-context-tools/code-knowledge-graph-platforms/tree-sitter-vs-antlr)
18. [Tree-sitter vs ANTLR for AI Code Parsing, Inference Systems](https://inferensys.com/differences/ai-software-factory-platforms/code-migration-and-modernization-ai/tree-sitter-vs-antlr-for-ai-assisted-code-parsing-in-modernization)
19. [Best Parser Generators: ranked and compared, Faun](https://www.faun.dev/toolbox/compare-parser-generators/)
20. [tree-sitter/tree-sitter v0.26.10, Zenodo](https://zenodo.org/records/21005449)
21. [Releases, tree-sitter/tree-sitter, GitHub](https://github.com/tree-sitter/tree-sitter/releases)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/tree-sitter

English original: https://www.edgepedia.org/tree-sitter
