上海交通大学:《编译原理》教学资源_教学资料_第四周讲义_Syntax-Directed Translation

Syntax-Directed Translation CS308 Compiler Theory 1
Syntax-Directed Translation CS308 Compiler Theory 1

Syntax-Directed Translation Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are evaluated by the semantic rules associated with the production rules. Evaluation of these semantic rules: -may generate intermediate codes may put information into the symbol table may perform type checking may issue error messages may perform some other activities -in fact,they may perform almost any activities. An attribute may hold almost any thing. a string,a number,a memory location,a complex record. CS308 Compiler Theory 2
Syntax-Directed Translation • Grammar symbols are associated with attributes to associate information ith the programming lang age constr cts that the information with the programming lang uage constructs that they represent. • Values of these attributes are evaluated by the Values of these attributes are evaluated by the semantic rules semantic rules associated with the production rules. • Evaluation of these semantic rules: – may generate intermediate codes – may put information into the symbol table – ma y p yp g erform type checkin g – may issue error messages – may perform some other activities – in fact, they may perform almost any activities. ivities. • An attribute may hold almost any thing. – a string, a number, a memory location, a complex record. CS308 Compiler Theory 2

Syntax-Directed Definitions and Translation Schemes When we associate semantic rules with productions,we use two notations: Syntax-Directed Definitions Translation Schemes 。 Syntax-Directed Definitions: give high-level specifications for translations hide many implementation details such as order of evaluation of semantic actions. We associate a production rule with a set of semantic actions,and we do not say when they will be evaluated. Translation Schemes: indicate the order of evaluation of semantic actions associated with a production rule. In other words,translation schemes give a little bit information about implementation details. CS308 Compiler Theory 3
Syntax-Directed Definitions and Translation Schemes • When we associate semantic rules with productions, we use two notat ions: – Syntax-Directed Definitions – Translation Schemes • Syntax-Directed Definitions: – give high-level specifications for translations – hide many implementation details such as order of evaluation of semantic actions. – We associate a p yy roduction rule with a set of semantic actions, and we do not sa y when the y will be evaluated. • Translation Schemes: – i di h d f l i f i i i d i h d i l indicate t he or der o f evaluation o f semantic actions associate d wit h a pro duction rule. – In other words, translation schemes give a little bit information about implementation details. CS308 Compiler Theory 3

Syntax-Directed Definitions A syntax-directed definition is a generalization of a context-free grammar in which: -Each grammar symbol is associated with a set of attributes. This set of attributes for a grammar symbol is partitioned into two subsets called synthesized and inherited attributes of that grammar symbol. Each production rule is associated with a set of semantic rules. Semantic rules set up dependencies between attributes which can be represented by a dependency graph. This dependency graph determines the evaluation order of these semantic rules. Evaluation of a semantic rule defines the value of an attribute.But a semantic rule may also have some side effects such as printing a value. CS308 Compiler Theory
Syntax-Directed Definitions • A syntax-directed definition is a generalization of a context-free grammar i hi h n whi c h: – Each grammar symbol is associated with a set of attributes. – This set of attributes for a grammar symbol is partitioned into two subsets called This set of attributes for a grammar symbol is partitioned into two subsets called synthesized and inherited attributes of that grammar symbol. – Each production rule is associated with a set of semantic rules. • Semantic rules set up dependencies between attributes which can be represented by a dependency graph. • This dependency graph determines the evaluation order of these semantic rules. • Evaluation of a semantic rule defines the value of an attribute. But a semantic rule may also have some side effects such as printing a value. CS308 Compiler Theory 4

Annotated Parse Tree A parse tree showing the values of attributes at each node is called an annotated parse tree. The process of computing the attributes values at the nodes is called annotating (or decorating)of the parse tree. Of course,the order of these computations depends on the dependency graph induced by the semantic rules. CS308 Compiler Theory 5
Annotated Parse Tree • A parse tree showing the values of attributes at each node is called an annotated parse tree. • The process of computing the attributes values at the nodes is called annot ti a ng (or d ti ecorating) f th t ) o f the parse tree. • Of course, the order of these computations depends on the dependency graph induced by the semantic rules dependency graph induced by the semantic rules. CS308 Compiler Theory 5

Syntax-Directed Definition In a syntax-directed definition,each production A->a is associated with a set of semantic rules of the form: b=fC,C2,…,Cd where f is a function, and b can be one of the followings: b is a synthesized attribute of A and cc2c are attributes of the grammar symbols in the production A-a). OR b is an inherited attribute one of the grammar symbols in a(on the right side of the production),and cc2...c are attributes of the grammar symbols in the production A-a). CS308 Compiler Theory 6
Syntax-Directed Definition • In a syntax-directed definition, each production A→α is associated wih f i l f h f i t h a set o f semant ic ru les o f t he form: b=f(c 1,c 2,…,c n) where f is a function, and b can be one of the followings: Î b is a synthesized attribute of A and c 1,c 2,…,c n are attributes of the grammar symb l i th d ti ( b o ls in the pro duction ( A→α ). OR Î b i i h i d ib f h b l i is an i n her ite d attribute one o f t he grammar sym b o ls in α ( h on t h e right side of the production), and c 1,c 2,…,c n are attributes of the grammar symb l i th d ti ( b o ls in the pro duction ( A→α ). CS308 Compiler Theory 6

Attribute Grammar ● So,a semantic rule b-fc.c.c indicates that the attribute b depends on attributes cc2...c In a syntax-directed definition,a semantic rule may just evaluate a value of an attribute or it may have some side effects such as printing values. An attribute grammar is a syntax-directed definition in which the functions in the semantic rules cannot have side effects (they can only evaluate values of attributes) CS308 Compiler Theory 7
Attribute Grammar • So, a semantic rule b=f(c 1,c 2,…,c n) indicates that the attribute b d d epen ds o n attributes c 1,c 2,…,c n. • In a syntax-directed definition, a semantic rule may just evaluate a val f tt ib t it h id ff t h lue o f an att rib u te or it may have some side effec ts suc h as printing values. • An attribute grammar is a syntax-directed definition in which the functions in the semantic rules cannot have side effects (they can functions in the semantic rules cannot have side effects (they can only evaluate values of attributes). CS308 Compiler Theory 7

Syntax-Directed Definition --Example Production Semantic Rules L→E return print(E.val) E→E1+T E.val E.val T.val E→T E.val T.val T→T1*F T.val T val F.val T→F T.val F.val F→(E) F.val E.val F→digit F.val digit.lexval Symbols E,T,and F are associated with a synthesized attribute val The token digit has a synthesized attribute lexval(it is assumed that it is evaluated by the lexical analyzer). CS308 Compiler Theory 8
Syntax-Directed Definition -- Example Production Semantic Rules L → E return print(E.val) E → E1 + T E.val = E1.val + T.val E → T E.val = T.val T → T1 * F T.val = T1.val * F.val T → F T lF l .va l = F.va l F → ( E ) F.val = E.val F → digit F val = F.val = digit.lexval • Symbols E T and F are associated with a synthesized attribute Symbols E, T, and F are associated with a synthesized attribute val. • The token digit has a synthesized attribute lexval (it is assumed that it is evaluated by the lexical analyzer). CS308 Compiler Theory 8

Annotated Parse Tree--Example Input:5+3*4 E.val=17 return E.val=5 + T.val=12 T.val=5 T.val=3 F.val=4 F.val=5 F.val=3 digit.lexval=4 digit.lexval=5 digit.lexval=3 CS308 Compiler Theory 9
Annotated Parse Tree -- Example Input: 5+3*4 L E.val=17 return E.val=5 + T.val=12 T.val=5 T.val=3 * F.val=4 F.val=5 F.val=3 digit.lexval=4 digit.lexval=5 digit.lexval=3 CS308 Compiler Theory 9

Dependency Graph Input:5+3*4 E.val=17 平 E.val=5 T.val=12 ↑ T.val=5 T.val=3 F.val=4 ↑ ↑ F.val=5 F.val=3 digit.lexval=4 ↑ digit.lexval=5 digit.lexval=3 CS308 Compiler Theory 10
Dependency Graph Input: 5+3*4 L E.val=17 E.val=5 T.val=12 T.val=5 T.val=3 F.val=4 F.val=5 F.val=3 digit.lexval=4 digit.lexval=5 digit.lexval=3 CS308 Compiler Theory 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Run-Time Environments.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Heap Management.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Intermediate Code Generation.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅲ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅰ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第五周讲义_Type Checking.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_lex.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_Syntax Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_Lexical Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_Machine-Independent Optimizations Ⅳ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Bottom-Up Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Top-Down Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第七周讲义_Code Generation Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_A Simple Syntax-Directed Translator.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_Introduction to Compilin.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_Parameter Passing Mechanisms.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_CS308 Compiler Theory.pdf
- 上海交通大学:《计算机辅助设计》教学资源_Product Visualization.doc
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 1:Introduction, calculus review and computer representation of numbers.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 2:Solution of nonlinear equations.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 3:Polynomial Interpolation.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 4:Numerical differentiation and integration.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第一次上机_第一次上机内容_10.18.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第一次上机_第一次上机内容_10.18.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第三次上机_python第三次上机.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第三次上机_Python第三次上机解析-update.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第五次上机_第五次上机.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第四次上机_Python第四次上机题目.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源_作业_第一次作业内容要求.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源_作业_第一次作业内容要求.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_参考教材_HowToThink-Python.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_参考教材_参考教材-2002版-PythonProgrammingBook.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_参考教材_参考教材-python-programming.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_往年试卷_CT2012-A卷-final 2_参考答案.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源_往年试卷_CT2012-A卷-final.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源_期末大作业要求.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)chapter01 课程简介、计算机与程序.ppt