Download presentation
Presentation is loading. Please wait.
Published byChristina Fisher Modified over 8 years ago
1
Chapter4 Syntax-Directed Translation Introduction : 1.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 ; 2.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.
2
4.1 Syntax-Directed Definitions e.g.Syntax-directed definition of a simple desk calculator PRODUCTION SEMANTIC RULES L E n L E n print (E.val) print (E.val) E E 1 + T E E 1 + T E.val := E 1.val + T.val E.val := E 1.val + T.val E T E T E.val := T.val E.val := T.val T T 1 * F T T 1 * F T.val := T 1.val * F.val T.val := T 1.val * F.val T F T F T.val := F.val T.val := F.val F (E) F (E) F.val := E.val F.val := E.val F digit F digit F.val := digit.lexval F.val := digit.lexval
3
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(c 1, c 2, …, c k ), where f is the function, b and c 1, c 2, …, c k are the attribute of the grammar symbol. Synthesized Attributes : if b is the attribute of A , c 1, c 2, …, c k 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. Attribute is defined at the children or it self Attribute is defined at its parent, it self and its siblings 15/36
4
4.1 Syntax-Directed Definitions 4.1.2 Synthesized Attributes S-Attributed Definitions : every attribute is synthesized PRODUCTION SEMANTIC RULES L E n L E n print (E.val) print (E.val) E E 1 + T E E 1 + T E.val := E 1.val + T.val E.val := E 1.val + T.val E T E T E.val := T.val E.val := T.val T T 1 * F T T 1 * F T.val := T 1.val * F.val T.val := T 1.val * F.val T F T F T.val := F.val T.val := F.val F (E) F (E) F.val := E.val F.val := E.val F digit F digit F.val := digit.lexval F.val := digit.lexval
5
4.1 Syntax-Directed Definitions The annotated parse tree for 8+5*2 n digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 Each node’s attribute value is annotated According to the annotated parse tree, let’s study the calculaton order of S-attributed definition
6
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
7
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
8
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n 20/36
9
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
10
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
11
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
12
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
13
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n 25/36
14
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
15
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
16
4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
17
4.1 Syntax-Directed Definitions The annotated parse tree :Each node’s attribute value is annotated digit.lexval = 2 L E.val = 18 n T.val = 10E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n
18
4.1 Syntax-Directed Definitions 4.1.3 Inherited Attributes int id, id, id PRODUCTION SEMANTIC RULES D TL D TL L.in := T.type L.in := T.type T int T int T. type := integer T. type := integer T real T real T. type := real T. type := real L L 1, id L L 1, id L 1.in := L.in; L 1.in := L.in; addtype (id.entry, L.in ) addtype (id.entry, L.in ) L id L id addtype (id.entry, L.in ) addtype (id.entry, L.in ) 30/36
19
4.1 Syntax-Directed Definitions Annotated parse tree for int id 1, id 2, id 3 Dint T.type = integer, id 3 L.in = integer id 2 id 1,
20
4.1 Syntax-Directed Definitions 4.1.4 Dependency Graphs Dependency graph for the annotated parse tree of int id 1, id 2, id 3 D TL L.in := T.type D TL L.in := T.typeD int int T, id 3 L L L id 2 id 1, 1 entry 10 2 entry 3 entry in 9 8 in 7 6 in 5 4 type
21
4.1 Syntax-Directed Definitions 4.1.4 Dependency Graphs Dependency graph for the annotated parse tree of int id 1, id 2, id 3 L L 1, id L 1.in := L.in; addtype (id.entry, L.in ) addtype (id.entry, L.in )D int int T, id 3 L L L id 2 id 1, 1 entry 10 2 entry 3 entry in 9 8 in 7 6 in 5 4 type
22
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 D int int T, id 3 L L L id 2 id 1, 1 entry 10 2 entry 3 entry in 9 8 in 7 6 in 5 4 type
23
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
24
Supplement The understanding of attribute nonterminal analysis process ( function ) synthesized attributes the return value of process inherited attributes the parameters of process
25
e.g. int id, id, id PRODUCTION SEMANTIC RULES D TL D TL L.in := T.type L.in := T.type T int T int T. type := integer T. type := integer T real T real T. type := real T. type := real L id, L 1 L id, L 1 L 1.in := L.in; L 1.in := L.in; addtype (id.entry, L.in ) addtype (id.entry, L.in ) L id L id addtype (id.entry, L.in ) addtype (id.entry, L.in ) Review
26
Review e.g. void D() { T_temp = T(); L_in = T_temp; L(L_in); L(L_in); return ; } int T() { switch lookahead { case INT: return INTEGER; { case INT: return INTEGER; case REAL: return REAL; case REAL: return REAL; default: error; default: error;}} void L(int L_in) {match(id); match( ‘, ’ ); match( ‘, ’ ); addtype(id.entry, L_in); L(L_in); L(L_in);} Void L(int L_in) { match (id); addtype(id.entry, L.in); } PRODUCT ION SEMANTIC RULES D TL D TL L.in := T.type T int T int T. type := integer T real T real T. type := real L id, L 1 L id, L 1 L 1.in := L.in; addtype (id.entry, L.in ) L id L id addtype (id.entry, L.in )
27
Exercise 4.1 According to the syntax-directed definition of table 4.1 , please structure the annotated parse tree for expression5 * (4*3+2) 4.1 According to the syntax-directed definition of table 4.1 , please structure the annotated parse tree for expression5 * (4*3+2) 。
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.