Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.

Similar presentations


Presentation on theme: "Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are."— Presentation transcript:

1 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

2 Syntax directed definitions Each grammar production A->a is associated with a set of semantic rules of the form b=f(c1, c2, …, 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 c1, c2, …, ck). Syntax directed definitions hide implementation details.

3 –An S-attributed definition: A syntax directed definition that uses synthesized attributes exclusively is said to be an S-attributed definition. Production semantic rules L ->E n 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.val = F.val F->(E) F.val = E.val F->digits F.val = digits.lexval 3*5+4n

4 –Example: inherited attributes production semantic rules D ->T L L.in = T.type T->int T.type = integer T->real T.type = real L->L1, id L1.in = L.in, addtype(id.entry, L.in) L->id addtype(id.entry, L.in) real id1, id2, id3

5 –Syntax-directed construction of syntax trees Tree mkleaf(type, value); Tree mknode(op, left, right); Production E->E1+T E->E1-T E->T T->(E) T->num –how is the tree for 1-2+3 constructed?

6 –L-attributed definitions: A syntax directed definition is L-attributed if each inherited attribute of Xj, 1 X1X2…Xn depends only on –attributes of the symbols X1, X2, …, 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 {L.i=A.i; M.i=L.s; A.s = M.s} A->QR {R.i = A.i; Q.i = R.s; A.s = Q.s} –Relation between S-attributed definitions and L- attributed definitions? –Why L-attributed definitions are important?

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

8 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)}

9 Syntax directed translation scheme: –another example: E->T {R.I = T.val} R {E.val = R.s} R->+ T {R1.I = R.I + T.val} R1 {R.s = R1.s} R-> {R.s = R.I} T->num {T.val := num.val} evaluation of 9-5+2

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

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


Download ppt "Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are."

Similar presentations


Ads by Google