Chapter 5 Syntax directed translation Use the grammar
![• Chapter 5: Syntax directed translation – Use the grammar to direct the • Chapter 5: Syntax directed translation – Use the grammar to direct the](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-1.jpg)
![• Syntax directed definitions • Each grammar production A->a is associated with a • Syntax directed definitions • Each grammar production A->a is associated with a](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-2.jpg)
![– An S-attributed definition: • A syntax directed definition that uses synthesized attributes exclusively – An S-attributed definition: • A syntax directed definition that uses synthesized attributes exclusively](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-3.jpg)
![– Example: inherited attributes production D ->T L T->int T->real L->L 1, id L->id – Example: inherited attributes production D ->T L T->int T->real L->L 1, id L->id](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-4.jpg)
![– Syntax-directed construction of syntax trees • Tree mkleaf(type, value); • Tree mknode(op, left, – Syntax-directed construction of syntax trees • Tree mkleaf(type, value); • Tree mknode(op, left,](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-5.jpg)
![– L-attributed definitions: • A syntax directed definition is L-attributed if each inherited attribute – L-attributed definitions: • A syntax directed definition is L-attributed if each inherited attribute](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-6.jpg)
![• Given a syntax directed definition, how to build a translator? – For • Given a syntax directed definition, how to build a translator? – For](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-7.jpg)
![• Syntax directed translation scheme: – a context-free grammar in which attributes are • Syntax directed translation scheme: – a context-free grammar in which attributes are](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-8.jpg)
![• Syntax directed translation scheme: – another example: E->T {R. I = T. • Syntax directed translation scheme: – another example: E->T {R. I = T.](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-9.jpg)
![– S-attributed definitions can directly translated into a translation scheme by placing the semantic – S-attributed definitions can directly translated into a translation scheme by placing the semantic](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-10.jpg)
![– Syntax directed translation with YACC. • Only has synthesized attributes • parser stack – Syntax directed translation with YACC. • Only has synthesized attributes • parser stack](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-11.jpg)
- Slides: 11
![Chapter 5 Syntax directed translation Use the grammar to direct the • Chapter 5: Syntax directed translation – Use the grammar to direct the](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-1.jpg)
• Chapter 5: Syntax directed translation – Use the grammar to direct the translation • The grammar defines the syntax of the input language. Attributes are attached to grammar symbols. Semantic rules are associated with grammar productions. • Attributes -- type of values associated with grammar symbols representing programming language constructs. These values are computed by the semantic rules. – 2 notations for associating semantic rules with productions. • Syntax directed definitions • Syntax directed translation schemes
![Syntax directed definitions Each grammar production Aa is associated with a • Syntax directed definitions • Each grammar production A->a is associated with a](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-2.jpg)
• Syntax directed definitions • Each grammar production A->a is associated with a set of semantic rules of the form b=f(c 1, c 2, …, ck) – If b is an attributed associated with A, it is called a synthesized attribute. – If b is an attributed associated with a grammar symbol on the right side of the production, b is called an inherited attribute. • The definitions does not specify explicitly the order in which the attributes can be evaluated. – The semantic rules implicitly indicate the order (b depends on c 1, c 2, …, ck). • Syntax directed definitions hide implementation details.
![An Sattributed definition A syntax directed definition that uses synthesized attributes exclusively – An S-attributed definition: • A syntax directed definition that uses synthesized attributes exclusively](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-3.jpg)
– An S-attributed definition: • A syntax directed definition that uses synthesized attributes exclusively is said to be an S-attributed definition. Production L ->E n E->E 1 + T E->T T->T 1 * F T->F F->(E) F->digits 3*5+4 n semantic rules print(E. val) E. val = E 1. val + T. val E. val = T 1. val * F. val T. val = F. val = E. val F. val = digits. lexval
![Example inherited attributes production D T L Tint Treal LL 1 id Lid – Example: inherited attributes production D ->T L T->int T->real L->L 1, id L->id](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-4.jpg)
– Example: inherited attributes production D ->T L T->int T->real L->L 1, id L->id real id 1, id 2, id 3 semantic rules L. in = T. type = integer T. type = real L 1. in = L. in, addtype(id. entry, L. in)
![Syntaxdirected construction of syntax trees Tree mkleaftype value Tree mknodeop left – Syntax-directed construction of syntax trees • Tree mkleaf(type, value); • Tree mknode(op, left,](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-5.jpg)
– Syntax-directed construction of syntax trees • Tree mkleaf(type, value); • Tree mknode(op, left, right); • Production E->E 1+T E->E 1 -T E->T T->(E) T->num – how is the tree for 1 -2+3 constructed?
![Lattributed definitions A syntax directed definition is Lattributed if each inherited attribute – L-attributed definitions: • A syntax directed definition is L-attributed if each inherited attribute](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-6.jpg)
– L-attributed definitions: • A syntax directed definition is L-attributed if each inherited attribute of Xj, 1<=j<=n, on the right side of A->X 1 X 2…Xn depends only on – attributes of the symbols X 1, X 2, …, Xj-1. – the inherited attributes of A. • L stands for Left since information appears to flow from left to right in the compilation process. • Example: A->LM A->QR {L. i=A. i; M. i=L. s; A. s = M. s} {R. i = A. i; Q. i = R. s; A. s = Q. s} – Relation between S-attributed definitions and Lattributed definitions? – Why L-attributed definitions are important?
![Given a syntax directed definition how to build a translator For • Given a syntax directed definition, how to build a translator? – For](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-7.jpg)
• Given a syntax directed definition, how to build a translator? – For general definitions, to evaluate the semantic rules correctly, we need to follow the dependence of the attributes (defined by the semantic rules). • Build a dependency graph for the parsing tree. Topologically sort the graph, then evaluate the rules accordingly. • Example: real id, id – For some special definitions, we can perform translation while parsing • e. g. bottom-up evaluation of S-attributed definitions. • Most L-attributed definitions also works.
![Syntax directed translation scheme a contextfree grammar in which attributes are • Syntax directed translation scheme: – a context-free grammar in which attributes are](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-8.jpg)
• Syntax directed translation scheme: – a context-free grammar in which attributes are associated with grammar symbols and the semantic actions are enclosed between {} and are inserted within the right side of productions to indicate the order in which translation takes place -- must be careful with the order. – Example: E->T R R->+ T {print(‘+’)} R | - T {print(‘-’)} R | e T->num {print(num. val)}
![Syntax directed translation scheme another example ET R I T • Syntax directed translation scheme: – another example: E->T {R. I = T.](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-9.jpg)
• Syntax directed translation scheme: – another example: E->T {R. I = T. val} R {E. val = R. s} R->+ T {R 1. I = R. I + T. val} R 1 {R. s = R 1. s} R-> {R. s = R. I} T->num {T. val : = num. val} evaluation of 9 -5+2
![Sattributed definitions can directly translated into a translation scheme by placing the semantic – S-attributed definitions can directly translated into a translation scheme by placing the semantic](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-10.jpg)
– S-attributed definitions can directly translated into a translation scheme by placing the semantic actions at the end of each productions. • Perfect for bottom up parsing (LR parsing) – Actions in the middle of productions can be removed to be put at the end of productions by changing the grammar (adding markers). • Example: previous page.
![Syntax directed translation with YACC Only has synthesized attributes parser stack – Syntax directed translation with YACC. • Only has synthesized attributes • parser stack](https://slidetodoc.com/presentation_image_h/ceb7ca2934e14f76c7e024e2f60448fb/image-11.jpg)
– Syntax directed translation with YACC. • Only has synthesized attributes • parser stack with field for synthesized attributes – default attribute is of integer type. • How to handle inherited attributes? – The information of a symbol is on the stack, which can be accessed directly. • Semantic actions only happen at the end of a production? – Automatically done
Syntax directed translation
Applications of syntax directed translation
Syntax directed definition
Syntax directed definition
Syntax vs grammar
Parse tree for if else statement
Grammar vs syntax
Syntax and grammar
Syntax and grammar
Right linear grammar to left linear grammar
Characteristics of traditional grammar
Unrestricted grammar example