1 / 27

Chapter4 Syntax-Directed Translation

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 ;

jmalone
Télécharger la présentation

Chapter4 Syntax-Directed Translation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 4.1 Syntax-Directed Definitions e.g.Syntax-directed definition of a simple desk calculator

  3. 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. 4.1 Syntax-Directed Definitions 4.1.2 Synthesized Attributes S-Attributed Definitions:every attribute is synthesized

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 4.1 Syntax-Directed Definitions 4.1.3Inherited Attributes int id, id, id 30/36

  19. 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

  20. 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

  21. 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 LL1, id L1.in := L.in; addtype (id.entry, L.in )

  22. 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

  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. Review e.g. int id, id, id

  26. 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); }

  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) 。

More Related