东南大学 编译原理 Cp5PPT课件

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Inherited Attribute
The value of an inherited attribute is computed from the values of attributes at the siblings and parent of that node.
Annotated Parse Tree
Allow some implementation details to be shown.
Conceptual View of SyntaxDirected Translation (1)
Input string
Parse tree
Dependency graph
Evaluation order for semantic rules
Guided by context-free grammar (Translating while or after parsing)
Attaching attributes to the grammar symbols representing the program construction.
Syntax-directed Definition
A generalization of a context-free grammar in which each grammar来自百度文库symbol has an associated set of attributes.
Types of Attribute
Conceptual View of SyntaxDirected Translation (2)
Special cases of syntax-directed definitions can be implemented in a single pass by evaluating semantic rules during parsing, without explicitly constructing a parse tree or a graph showing dependencies between attributes.
E.g. Annotated parse tree for 3*5+4
E.val=19
E.val=15 + T.val=4
T.val=15 T.val=3 * F.val=5
F.val=4 digit.lexval=4
F.val=3
digit.lexval =5
digit.lexval =3
Basic Idea(1)
Chapter 5 Syntax-Directed Translation
Zhai Yuqing Zhou Xiaoyu
Approaches to Implement Syntax-Directed Translation
Semantic
Static Semantic Dynamic Semantic
The value of an attribute at a parse tree node is computed by a “semantic rule” associated with the production used at that node.
Basic Idea(3)
Evaluation of the semantic rules may generate code, save information in a symbol table, issue error messages, or perform any other activities.
Hide implementation details.
No need to specify explicitly the order in which translation takes place
EE1+E2 {E.val=E1.val+E2.val}
Translation Scheme
Indicate the order in which semantic rules are to be evaluated
The attributes represent anything we choose: a string, a number, a type, a memory location, etc.
Basic Idea(2)
The dependency between the attributes of relevant nodes in syntax tree.
Form of a Syntax-Directed Definition
Each grammar production A has associated with it a set of semantic rules of the form b=f(c1,c2,….,ck), f is a function.
Associating Semantic Rules with Productions
Syntax-directed Definition.
Translation Schemes.
Syntax-directed definitions
High-level specifications for translations.
Synthesized Attribute
The value of a synthesized attribute at a node is computed from the values of attributes at the children of that node in the parse tree.
A parse tree showing the values of attributes at each node
The process of computing the attribute values at the nodes is called annotating or decorating the parse tree.
相关文档
最新文档