• 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 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. – 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->E1 + T E->T T->T1 * F T->F F->(E) F->digits 3*5+4n semantic rules print(E.val) E.val = E1.val + T.val E.val = T.val T.val = T1.val * F.val T.val = F.val F.val = E.val F.val = digits.lexval – Example: inherited attributes production D ->T L T->int T->real L->L1, id L->id real id1, id2, id3 semantic rules L.in = T.type T.type = integer T.type = real L1.in = L.in, addtype(id.entry, L.in) addtype(id.entry, L.in) – 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? – 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->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 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 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. • 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: 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 – 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 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