# 属性文法

属性文法是一种上下文无关文法，它在原有文法的基础上增加了属性——即附加在语法分析树文法符号上的值——以及定义这些值如何计算的语义规则。它提供了一种声明式、语法制导的方式来定义编程语言的语义，与编写手写编译器遍形成对比。<sup>[1](https://www.researchgate.net/publication/268445765_Attribute_Grammars_and_Their_Applications)</sup><sup> • </sup><sup>[2](https://www.ccs.neu.edu/home/chadwick/files/knuth.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 提出者 | 高德纳（Donald E. Knuth）1968年的论文《上下文无关语言的语义》（"Semantics of Context-Free Languages"），《数学系统理论》（Mathematical Systems Theory）第2卷，第127–145页<sup>[2](https://www.ccs.neu.edu/home/chadwick/files/knuth.pdf)</sup><sup> • </sup><sup>[3](https://gen.wtf/reference/papers/knuth-1990-genesis-attribute-grammars/)</sup> |
| 属性种类 | 综合属性仅由某结点后代的属性定义；继承属性则依据其兄弟结点和祖先结点的属性定义<sup>[2](https://www.ccs.neu.edu/home/chadwick/files/knuth.pdf)</sup> |
| 良定义性 | 高德纳给出了一个算法，用于检测语义规则何时会在某棵语法分析树上产生某些属性的循环定义<sup>[2](https://www.ccs.neu.edu/home/chadwick/files/knuth.pdf)</sup><sup> • </sup><sup>[3](https://gen.wtf/reference/papers/knuth-1990-genesis-attribute-grammars/)</sup> |
| 求值类别 | S属性定义仅使用综合属性；每个 S属性定义都是 L属性定义，可在一次深度优先、从左到右的遍历中求值<sup>[4](https://homepage.iis.sinica.edu.tw/~tshsu/compiler/2013/slides/slide4.pdf)</sup> |
| 纯粹性 | 属性文法是一种语法制导定义，其语义规则不能有副作用<sup>[5](https://www.csd.uwo.ca/~mmorenom/CS447/Lectures/Translation.html/node1.html)</sup> |
| 现代工具 | JastAdd（引用属性文法）、Silver 和乌得勒支属性文法编译器（Utrecht Attribute Grammar Compiler, UUAGC）；Lark 于2024年获得属性文法支持<sup>[6](https://jastadd.cs.lth.se/)</sup><sup> • </sup><sup>[7](https://doi.org/10.4230/OASIcs.SLATE.2024.7)</sup> |
| 理论结果 | 字符串值属性文法（以字符串为语义域、以拼接为基本运算）的输出语言可对数空间归约到上下文无关语言<sup>[8](https://epubs.siam.org/doi/10.1137/0215005)</sup> |

## 定义与结构

语法制导定义将上下文无关文法的每个产生式与一组有限语义规则配对，规则形式为 b := f(c1, …, ck)，其中 f 是函数，ci 是产生式中出现的文法符号或左部非终结符的属性。终结符没有继承属性。在每个结点上附上计算出的属性值的语法分析树称为带注释的语法分析树。<sup>[5](https://www.csd.uwo.ca/~mmorenom/CS447/Lectures/Translation.html/node1.html)</sup><sup> • </sup><sup>[9](https://cs.nyu.edu/~gottlieb/courses/2000s/2006-07-spring/compilers/lectures/lecture-02.html)</sup> 作为与文法相关联的、由语法导出的语义检查的形式化表达，属性文法表示语言中并非由语法显式传达的规则，并定义为了执行语义分析而必须存在于抽象语法树中的信息。<sup>[10](https://archive.nptel.ac.in/content/storage2/courses/106104072/chapter_5/5_9.html)</sup>

**综合属性与继承属性。** 综合属性把语义信息沿语法树向上传播，由该结点子结点的属性计算；继承属性把信息向下传播，由某结点的兄弟结点和父结点的属性计算。在编译器中，典型的综合属性是 errors 属性，它收集语义错误并传播到根结点，以便报告给程序员；典型的继承属性是从声明上下文传递给其左侧构造的类型或环境，例如变量的类型依赖于编译器的符号表时。<sup>[11](https://melt.cs.umn.edu/silver/tutorial/4_attribute_grammars/)</sup><sup> • </sup><sup>[12](https://www.cl.cam.ac.uk/~lp15/papers/Reports/popl82.pdf)</sup>

编译器课程中的一个完整示例定义了二进制串：非终结符 D 带有一个综合属性 val 和一个继承属性 pow，对于产生式 D → 1，语义规则置 D.val := 2<sup>D.pow</sup>，而父结点根据该数字的位置计算 D.pow。整个串的值由诸如 B.val := B₁.val + D.val 的综合规则汇总而成。<sup>[5](https://www.csd.uwo.ca/~mmorenom/CS447/Lectures/Translation.html/node1.html)</sup>

## 求值

属性值必须无循环地计算，而文法的类别决定了这能有多容易做到。S属性定义仅使用综合属性，因此可在构建语法分析树时自底向上计算值，这适合由 bison 等工具生成的语法分析器。<sup>[13](https://www.cs.ru.ac.za/compilerbook/CSharpAndJava/book2005/book2005text/chap09.pdf)</sup><sup> • </sup><sup>[14](https://isac-artzi.github.io/Resources-for-AI-courses/principles-of-compiler-design/topic-2-minimal-compiler/lecture-notes.html)</sup> L属性定义允许产生式 A → X₁ … Xₙ 中子结点 Xᵢ 的继承属性只依赖于 A 的继承属性，以及其左侧子结点 X₁ … Xᵢ₋₁ 的继承属性或综合属性，因此整个注释可在一次深度优先、从左到右的遍历中计算；每个 S属性定义都是 L属性定义。<sup>[4](https://homepage.iis.sinica.edu.tw/~tshsu/compiler/2013/slides/slide4.pdf)</sup>在这些受限类别之外，高德纳1968年的论文给出了一个算法，用于检测一个文法是否循环，即是否存在某棵语法分析树会在属性依赖中产生环。<sup>[2](https://www.ccs.neu.edu/home/chadwick/files/knuth.pdf)</sup><sup> • </sup><sup>[3](https://gen.wtf/reference/papers/knuth-1990-genesis-attribute-grammars/)</sup> 实际系统也支持迭代：在 JastAdd 中，属性可声明为循环属性，并从初始值开始迭代求值，直到该值等于上一轮迭代的值；当值构成有限高度的格，且环上的每个方程都是单调的时，收敛可得到保证。JastAdd 自 2.3.0 版起支持并发属性求值，会为除并行化集合之外的属性种类生成无锁代码。<sup>[15](https://jastadd.cs.lth.se/web/documentation/reference-manual.php)</sup><sup> • </sup><sup>[16](https://jastadd.cs.lth.se/web/concurrent.php)</sup>

约斯特·恩格尔弗里特（Joost Engelfriet）的一项理论结果给出了字符串值情形的能力上界：当语义域是某字母表上的字符串、以拼接为基本运算时，翻译的输出语言可对数空间归约到上下文无关语言。<sup>[8](https://epubs.siam.org/doi/10.1137/0215005)</sup>

## 起源

属性文法诞生于20世纪60年代中期。1964年在维也纳（Vienna）举行的一次形式语言描述会议上，小T. B. 斯蒂尔（T. B. Steel, Jr.）概括了当时的气氛，他说自己并不完全知道如何描述一种语言的语义，并怀疑在座无人知道。所有已知的定义程序含义的方法都基于复杂算法，其复杂度大致与编译器相当，甚至更糟，这与乔姆斯基（Chomsky）简洁优雅的上下文无关语法形成鲜明对比；内德·艾恩斯（Ned Irons）1960年的机制通过组合各部分的意义来处理算术表达式等相对简单的语言。<sup>[17](https://www.dcs.warwick.ac.uk/~sk/cs325/gag.pdf)</sup>

高德纳是斯坦福大学（Stanford University）的计算机科学家，已经创立了 LL(k) 与 LR(k) 语法分析理论，他把这一想法归功于1967年2月的一次美国计算机协会（ACM）巡回讲座：1967年2月4—5日住在[纽约州伊萨卡](https://www.edgepedia.org/zh/ithaca-new-york)（Ithaca, New York）的彼得·韦格纳（Peter Wegner）家中，后来又访问了其他校园，“于是，属性文法就这样诞生了。”<sup>[18](https://www.cs.miami.edu/home/odelia/teaching/csc419_spring17/syllabus/Knuth_AttributeHistory.pdf)</sup> 正式结果发表于1968年的《上下文无关语言的语义》，其更正发表于1971年《数学系统理论》第5卷，第95–96页。<sup>[2](https://www.ccs.neu.edu/home/chadwick/files/knuth.pdf)</sup><sup> • </sup><sup>[19](https://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/AGabstract.html)</sup> 高德纳本人的回顾文章《属性文法的起源》（"The Genesis of Attribute Grammars"）拉开了1990年第一届属性文法及其应用国际会议（First International Conference on Attribute Grammars and their Applications）的帷幕。<sup>[17](https://www.dcs.warwick.ac.uk/~sk/cs325/gag.pdf)</sup><sup> • </sup><sup>[3](https://gen.wtf/reference/papers/knuth-1990-genesis-attribute-grammars/)</sup> 该框架用纯声明式规约取代了操作式的、类似编译器的语义定义；高德纳在一个小型命令式语言图灵戈（Turingol）上演示了它，把类型检查、作用域解析和代码生成表达为属性计算。<sup>[3](https://gen.wtf/reference/papers/knuth-1990-genesis-attribute-grammars/)</sup>

## 应用

属性文法主要用于语言处理中的语义分析和翻译。它们已应用于语言定义、编译器生成、定子句文法以及算法的设计与规约。<sup>[1](https://www.researchgate.net/publication/268445765_Attribute_Grammars_and_Their_Applications)</sup> 在实际编译器构造中，通常向语法分析器生成器指定一种 S属性文法，把程序转换为抽象语法树，然后再多次遍历该树以进行语义分析和代码生成。<sup>[20](https://cecs.wright.edu/~tkprasad/papers/Attribute-Grammars.pdf)</sup> 它们还能规定静态语义，例如检查数组下标类型是否正确，这些是编译器可在程序运行前确定的规则。<sup>[21](https://www.cs.nmsu.edu/~jcook/posts/pl-attribute-grammars/)</sup>

**工具支持。** 若干系统围绕这一形式化方法构建。JastAdd 是一个支持引用属性文法的元编译系统；引用属性文法是一种扩展，其中属性可以持有对其他树结点的引用，高阶属性支持跨语言复用静态分析。<sup>[6](https://jastadd.cs.lth.se/)</sup><sup> • </sup><sup>[22](https://arxiv.org/abs/2002.06187)</sup> Silver 是明尼苏达可扩展语言工具组（Minnesota Extensible Language Tools group）的系统，以属性文法作为其核心规约语言；其编译器用257行代码取代了1,698行手写的环境序列化代码，这些代码通过对未装饰语法树的反射实现。<sup>[11](https://melt.cs.umn.edu/silver/tutorial/4_attribute_grammars/)</sup><sup> • </sup><sup>[23](https://www.sciencedirect.com/science/article/abs/pii/S2590118421000125)</sup> 由皇家霍洛威学院（Royal Holloway）开发的 ART 支持纯属性文法，并支持项重写和用于规定语义的结构操作语义规则。<sup>[24](https://art.csle.cs.rhul.ac.uk/)</sup> 2024年，Lark 编译器生成器通过 LarkAG 项目扩展了属性文法支持，增加了用于属性出现选择与语义规则的记法。<sup>[7](https://doi.org/10.4230/OASIcs.SLATE.2024.7)</sup>

## 研究方向

调试属性文法很困难，因为其文法结构递归且存在属性依赖。基于查询的系统化调试方法，包括算法调试和基于切片的调试，能缩小潜在错误空间并找出错误的属性规则；这些方法已在 Aki 中实现，Aki 是一个面向属性文法描述的调试器。<sup>[25](https://arxiv.org/abs/cs/0011029)</sup><sup> • </sup><sup>[26](https://arxiv.org/abs/cs/0310026)</sup>

近期工作将该形式化方法与其他技术结合，并实现了其编写过程的自动化。2021年的一项研究将属性文法与策略项重写一起嵌入基于拉链的纯函数式环境，产生了一个小型库，可优化 Haskell 的 let 表达式、表达 Haskell 重构，并解决 LDTA 工具挑战任务。<sup>[27](https://arxiv.org/abs/2110.07902)</sup> PANINI 是2022年的一个项目，从字符串与其含义配对的示例中自动生成语义动作，依赖一种称为推导覆盖率的覆盖度量；其评估覆盖了十二个基准，其中包括一个前向微分引擎、一个处理 Java 字节码子集的解释器，以及一个把 C 翻译成二地址码的小型编译器。<sup>[28](https://arxiv.org/abs/2208.06916)</sup>

## References

1. [Krishnaprasad Thirunarayan. Attribute Grammars and Their Applications. Encyclopedia of Information Science and Technology, IGI Global.](https://www.researchgate.net/publication/268445765_Attribute_Grammars_and_Their_Applications)
2. [Donald E. Knuth (1968). Semantics of Context-Free Languages. Mathematical Systems Theory 2, pp. 127–145.](https://www.ccs.neu.edu/home/chadwick/files/knuth.pdf)
3. [Knuth (1990) — The Genesis of Attribute Grammars, reading summary at gen.wtf.](https://gen.wtf/reference/papers/knuth-1990-genesis-attribute-grammars/)
4. [Syntax-Directed Translation, lecture slides, Institute of Information Science, Academia Sinica.](https://homepage.iis.sinica.edu.tw/~tshsu/compiler/2013/slides/slide4.pdf)
5. [Syntax-Directed Definitions, CS 447 lecture notes, University of Western Ontario.](https://www.csd.uwo.ca/~mmorenom/CS447/Lectures/Translation.html/node1.html)
6. [JastAdd.org.](https://jastadd.cs.lth.se/)
7. [Daniel Faria, Tiago João Baptista, Pedro Rangel Henriques (2024). Upgrade of Lark Compiler Generator to Support Attribute Grammars. OASIcs SLATE 2024.](https://doi.org/10.4230/OASIcs.SLATE.2024.7)
8. [Joost Engelfriet. The Complexity of Languages Generated by Attribute Grammars. SIAM Journal on Computing.](https://epubs.siam.org/doi/10.1137/0215005)
9. [Compilers Lecture #2, New York University.](https://cs.nyu.edu/~gottlieb/courses/2000s/2006-07-spring/compilers/lectures/lecture-02.html)
10. [Attribute Grammar Framework, NPTEL Power System Analysis course notes, Chapter 5: Semantic Analysis.](https://archive.nptel.ac.in/content/storage2/courses/106104072/chapter_5/5_9.html)
11. [Attribute Grammars, Minnesota Extensible Language Tools Group (Silver tutorial).](https://melt.cs.umn.edu/silver/tutorial/4_attribute_grammars/)
12. [A Semantics-Directed Compiler Generator, Computer Laboratory, University of Cambridge.](https://www.cl.cam.ac.uk/~lp15/papers/Reports/popl82.pdf)
13. [Chapter 9: Syntax-Directed Translation, compiler course notes, Rhodes University.](https://www.cs.ru.ac.za/compilerbook/CSharpAndJava/book2005/book2005text/chap09.pdf)
14. [Grammars, Parsing, Translation — and a Compiler End to End, course notes.](https://isac-artzi.github.io/Resources-for-AI-courses/principles-of-compiler-design/topic-2-minimal-compiler/lecture-notes.html)
15. [JastAdd Reference Manual: Circular attributes.](https://jastadd.cs.lth.se/web/documentation/reference-manual.php)
16. [JastAdd: Concurrent Attributes.](https://jastadd.cs.lth.se/web/concurrent.php)
17. [Donald E. Knuth. The Genesis of Attribute Grammars (full text).](https://www.dcs.warwick.ac.uk/~sk/cs325/gag.pdf)
18. [Donald E. Knuth. The Genesis of Attribute Grammars (alternative full text).](https://www.cs.miami.edu/home/odelia/teaching/csc419_spring17/syllabus/Knuth_AttributeHistory.pdf)
19. [Attribute grammars bibliography, INRIA.](https://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/AGabstract.html)
20. [Attribute Grammars, Wright State University course paper.](https://cecs.wright.edu/~tkprasad/papers/Attribute-Grammars.pdf)
21. [Jonathan Cook. PL: Attribute Grammars.](https://www.cs.nmsu.edu/~jcook/posts/pl-attribute-grammars/)
22. [Johannes Mey et al. (2020). Reusing Static Analysis across Different Domain-Specific Languages using Reference Attribute Grammars. arXiv.](https://arxiv.org/abs/2002.06187)
23. [Reflection of terms in attribute grammars: Design and applications. Journal of Computer Languages 64 (2021).](https://www.sciencedirect.com/science/article/abs/pii/S2590118421000125)
24. [Software Language Engineering with ART, Royal Holloway, University of London.](https://art.csle.cs.rhul.ac.uk/)
25. [Yohei Ikezoe et al. (2000). Systematic Debugging of Attribute Grammars. arXiv.](https://arxiv.org/abs/cs/0011029)
26. [Akira Sasaki, Masataka Sassa (2003). Generalized Systematic Debugging for Attribute Grammars. arXiv.](https://arxiv.org/abs/cs/0310026)
27. [José Nuno Macedo, Marcos Viera, João Saraiva (2021). Zipping Strategies and Attribute Grammars. arXiv.](https://arxiv.org/abs/2110.07902)
28. [Pankaj Kumar Kalita, Miriyala Jeevan Kumar, Subhajit Roy (2022). Synthesis of Semantic Actions in Attribute Grammars. arXiv.](https://arxiv.org/abs/2208.06916)

---
*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 › Attribute and affix grammar formalisms

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/attribute-grammar

English original: https://www.edgepedia.org/attribute-grammar
