300 likes | 371 Views
Chapter4 Syntax-Directed Translation. Introduction : In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer to the symbol-table entry for that id, the attribute of a num is the value of the num ;
E N D
Chapter4Syntax-Directed Translation Introduction: In the lexical analysis step, each token has its attribute,e.g., the attribute of an id is a pointer to the symbol-table entry for that id, the attribute of a num is the value of the num; thinking:In the syntax analysis step, should each grammar symbol has its attribute? tip1:Complier not only gives an answer whether the string is a sentence, but also need to do a lot of work, such as the generation of object code, error management and so on. tip2:Many compiler is based on syntax analysis stage as the core, around syntax analysis to complete semantic analysis, intermediate code generation, code optimization and so on。 tip3:The attribute of grammar symbol is very board, it can be generation of code, calculation of the value, information stored in the symbol table and so on.
4.1 Syntax-Directed Definitions e.g.Syntax-directed definition of a simple desk calculator
Attribute is defined at the children or it self Attribute is defined at its parent, it self and its siblings 4.1 Syntax-Directed Definitions 4.1.1 the form of Syntax-Directed Definitions • Basic grammar • Attributes are associated with grammar symbol • Each grammar production A has a set of semantic rules like b := f(c1, c2, …, ck ), where fis the function, band c1, c2, …, ckare the attribute of the grammar symbol. • Synthesized Attributes:if bis the attribute of A,c1 , c2 , …, ck are the attribute of right side symbol in the grammar production or other attribute of A. • Inherited Attributes:if b is the attribute of a right side sumbol of x. 15/36
4.1 Syntax-Directed Definitions 4.1.2 Synthesized Attributes S-Attributed Definitions:every attribute is synthesized
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 4.1 Syntax-Directed Definitions According to the annotated parse tree, let’s study the calculaton order of S-attributed definition The annotated parse tree for 8+5*2 n Each node’s attribute value is annotated
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up 20/36
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up 25/36
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up
L n E.val = 18 E.val = 8 T.val = 10 + F.val = 2 T.val = 5 * T.val = 8 digit.lexval = 2 F.val = 5 F.val = 8 digit.lexval = 8 digit.lexval = 5 8+5*2 n 4.1 Syntax-Directed Definitions • The annotated parse tree :Each node’s attribute value is annotated
4.1 Syntax-Directed Definitions 4.1.3Inherited Attributes int id, id, id 30/36
D L.in = integer T.type = integer , L.in = integer id3 int , L.in = integer id2 id1 4.1 Syntax-Directed Definitions Annotated parse tree for int id1, id2, id3
D T L in 5 4 type 6 , int L id3 in 7 8 3 entry , L in 9 10 id2 2 entry id1 1 entry 4.1 Syntax-Directed Definitions 4.1.4 Dependency Graphs Dependency graph for the annotated parse tree of int id1, id2, id3 D TLL.in := T.type
D T L in 5 4 type 6 , int L id3 in 7 8 3 entry , L in 9 10 id2 2 entry id1 1 entry 4.1 Syntax-Directed Definitions 4.1.4 Dependency Graphs Dependency graph for the annotated parse tree of int id1, id2, id3 LL1, id L1.in := L.in; addtype (id.entry, L.in )
D T L in 5 4 type 6 , int L id3 in 7 8 3 entry , L in 9 10 id2 2 entry id1 1 entry 4.1 Syntax-Directed Definitions 4.1.5 Ordering the Evaluation of Attributes Topological sort:if there is an edge of the dependency graph from Ni to Nj,then i < j e.g.:1,2,3,4,5,6,7,8,9,10
4.1 Syntax-Directed Definitions Ordering the Evaluation of Attributes 1、Structure input parse tree, 2、Structure dependency graph, 3、sort nodes by topological sort, 4、calculate the attribute by the order of topological sort。 35/36
Supplement The understanding of attribute nonterminalanalysis process(function) synthesized attributesthe return value of process inherited attributesthe parameters of process
Review e.g. int id, id, id
Review e.g. void D() { T_temp = T(); L_in = T_temp; L(L_in); return ; } int T() { switch lookahead { case INT: return INTEGER; case REAL: return REAL; default: error; } } void L(int L_in) { match(id); match(‘,’); addtype(id.entry, L_in); L(L_in); } Void L(int L_in) { match (id); addtype(id.entry, L.in); }
Exercise • 4.1 According to the syntax-directed definition of table 4.1,please structure the annotated parse tree for expression5 * (4*3+2) 。