compiler-theory - S-attributed 和 L-attributed 语法是什么意思?

标签 compiler-theory

我正在阅读一本编译器书,当它说“S 属性文法也是 L 属性文法”时有点困惑。无法理解。有人可以说清楚吗(一个例子应该很好)。谢谢。

最佳答案

L-attributed grammar

L-attributed grammars are a special type of attribute grammars. They allow the attributes to be evaluated in one left-to-right traversal of the abstract syntax tree. As a result, attribute evaluation in L-attributed grammars can be incorporated conveniently in top-down parsing. Many programming languages are L-attributed. Special types of compilers, the narrow compilers, are based on some form of L-attributed grammar. These are comparable with S-attributed grammars. Used for code synthesis.


S-attributed grammar

S-Attributed Grammars are a class of attribute grammars characterized by having no inherited attributes. Inherited attributes, which must be passed down from parent nodes to children nodes of the abstract syntax tree during the semantic analysis of the parsing process, are a problem for bottom-up parsing because in bottom-up parsing, the parent nodes of the abstract syntax tree are created after creation of all of their children. Attribute evaluation in S-attributed grammars can be incorporated conveniently in both top-down parsing and bottom-up parsing. Yacc is based on the S-attributed approach.

Any S-attributed grammar is also an L-attributed grammar.


在 L 属性语法中,属性评估可以在从左到右的遍历中执行。由于在 S 属性文法中,属性不是继承的,因此不会阻止您这样做。因此,您可以说 S 属性文法符合 L 文法的特征。

关于compiler-theory - S-attributed 和 L-attributed 语法是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2458558/

相关文章:

compiler-theory - 什么是词法错误的例子,一种语言是否可能没有词法错误?

compiler-construction - 哈希表优化

javascript - 计算机科学中函数闭包的正式定义

compiler-construction - 解析树和抽象语法树(AST)有什么区别?

sql - 编译器设计帮助

compiler-theory - 是否有可能有一个编译器可以预测所有可能的 'situation specific' 运行时错误?

compiler-construction - 自上而下的解析器希望在 'Code' 中有体面的案例示例左递归