You are on page 1of 22
Syntax Directed Translation (asu chs) e{ 2 notations for associating semantic rules with productions — syntax-directed translations (grammar + semantic rules) — translation schemes (semantic rule evaluation order is given) conceptual process — input string — parse tree — dependency graph: dependencies between attributes are shown e.g. type, string, value, memory location — semantic rules: compute values of attributes associated with symbols appearing in a production © may generate code / save info in symbol table / issue error messages 05/10/2016 DFR-CC- SOT Syntax Directed Definitions «su cis.) grammar + semantic rules semantic rules — compute values of attributes asso — set up dependencies between attributes => evaluation order « synthesised attribute — value at parse tree node determined from attribute values at the children of the node (can be evaluated in bottom up traversal) iherited attribute - value at parse tree node determined from attribute values at the parent and/or the left siblings of that node « annotated parse tree shows the values of attributes at each node 05/10/2016 DFR-CC- SOT Syntax Directed Definitions «su cis.) e Each grammar P: A => a has an associated set of semantic rules ofthe form = b :=f (C4, Cp, C3, --- Cy) - either * bis a synthesised attribute of A and c,,¢,,¢,..., ¢, are attributes of the grammar symbols of P * bis an inherited attribute of one of the grammar symbols on the RHS of P and c,,¢,, ¢.,..., ¢, are attributes of the grammar symbols of P — b depends on the attributes c,, C2, C3, ..., C - attribute grammar: a syntax-directed definition in which the functions in semantic rules cannot have side effects - functions in semantic rules are often written as expressions 05/10/2016 DFR-CC- SOT Synthesised Attribute: example su Figs2) S-attributed definition production ema rules Ein print(E.val) Eval := E,.val + T.val E.val = T.val T.val == T,.val * F.val Tal : (E) F.val digit F.val = anaadamme \n = new line, X.val = integer value synthesised attribute, digit lexval is a synthesised attribute supplied by the lexical analyser 05/10/2016 DFR-CC- SOT Synthesised Attribute: example ax-directed definition, Ts are assumed to have synthesised values only the start symbol is assumed NOT to have any inherited attributes that uses exclusively synthesised attributes — can always be annotated by evaluating the semantic rules at each node bottom up 05/10/2016 (ASU Ch5.1, Fig 5.3) annotated parse tree: 3°5+4 \n lexval = 3 arly for type checking — type attr DFR-CC- SOT Inherited Attribute: example (asu Ch 5.1, Figs 5.4.5.5) semantic rules production «Parse tree for real id,, id,, id, D T T.type=real L.in= real T LG real in= real , id, L =>id addT(id-entry, L.in) L h =redl \ n= » id, wal 05/10/2016 DFR-CC- SOT Dependency Graphs aasu chs.) A directed graph showing interdependencies among inherited and synthesised attributes — if attribute b at a node depends on attribute c => semantic rule for b must be evaluated after the semantic rule for ¢ to construct a dependency graph for a parse tree — put each semantic rule into the form b := (C4, Cp, C3; ---» Cy) — the graph has a node for each attribute / an edge from ¢ to b if b depends onc = algorithm: for each node n in the parse tree do for each attribute a (of n) construct a node in the dependency graph for a for each node n in the parse tree do for each semantic rule b := (C4, C2, Ca, ..., €,) do fori := 1 to k construct an edge from c, to b 05/10/2016 DFR-CC- SOT ¢ Evaluation order is given by a topological sort - any TS gives a valid order of evaluation for the semantic rules i.e. the dependent attributes C4, Cp, C3, ..., Cy are known at the node before f is evaluated © rule sequence (see figure) a6 = a4 add_type(id3.entry, 25) a7 = a5 add_type(id2.entry, a7) a9 = a7 add_type(id‘.entry, 29) 05/10/2016 Dependency Graphs (ASU Ch5.1 Fig 5.7) Example directed graph real id,, id,, id, 4 T.type = real DFR-CC- SOT Methods for Evaluating Semantic Rules (ASU Ch 5.1) Parse tree methods: obtain evaluation order at compile time from the topological sort of the DG for parse tree for each input (DG must be DAG) ule-base ods. semantic rules associated with Ps analysed at compiler construction time (either by hand or special ed tool) - evaluation order predetermined at . ious Methods: evaluation order chosen without considering semantic rules - forced by the parsing method '¢the last two do not need to construct the DG explicitly - more efficient in use of compile time and space 05/10/2016 DFR-CC- SOT Construction of Syntax Trees «asu chs2€x5.7) Intermediate representation Syntax trees for expressions de-couples translation from (cf translation to post-fix form) parsing © operations = mknode(op left, right) — mkleaf(id, entry) = mkleaf(num, entry) condensed form of parse tree © examples if B then S, else S, if-then-else Bl SS knode(‘~’, p1, p2) p5 := mknode(‘+’, p3, p4) see ASU Fig 58 for syntax tree 05/10/2016 DFR-CC- SOT Syntax-directed Definition for Constructing Syntax Trees (ASU Ch 52 Fig 5.9) ‘S-attributed definition - expressions containing + - uses the underlying Ps of the G to schedule the semantic rules production semantic rules ET mknode(*, E,.nptr, T-nptr) E,-T mknode('~’, E,.nptr, T.nptr) T (E) id mkleaf(id, id.entry) T=> num = mkleaf(num, id.entry) ¢ nptris a synthesised attribute - pointer to nodes in the syntax tree see ASU Fig 5.8 for the syntax tree and Fig 5.10 for the syntax / parse tree 05/10/2016 DFR-CC- SOT Directed Acyclic Graphs for Expressions (ASU Ch 5.2 Fig 5.11) e ata*(b-c)+(b-c)*d «note the common sub-expressions a and (b-c) | see ASU Fig 5.12 (p291) for the instructions (mknode / mkleaf) and Fig 5.13 (p292) for an array implementation of the nodes 05/10/2016 DFR-CC- SOT Bottom Up Evaluation of S-attributed Definitions (ASU Ch 5.3 Fig 5.15) Syntax-directed definitions with only synthesised attributes canbe evaluated by a bottom up parser (BUP) as input is parsed e values associated with the attributes can be kept on the stack as extra fields «implementation using an LR parser (e.g. YACC) « eg. A=>XYZ and Aa=f(Xx, Vy, Z.2) state value x Xx Y Vy Tos |}—+ z La 05/10/2016 DFR-CC- SOT S-attributed Definitions: Parser Example {ASU Ch 5.3 Fig 5.16 see also Fig 5.2) semantic rules ri ot print(val[TOS]) val[NTOS] := val[TOS-2] + val[TOS] val[NTOS] := val[TOS-2] * val[TOS] (E) => id val[NTOS] := val[TOS-1] = NTOS=TOS-r+1 (ris # symbols reduced on RHS) — see ASU Fig 5.17 (next slide) for example of 3* 5 +4 05/10/2016 DFR-CC- SOT S-attributed Definitions: Parser Example {ASU Ch 5.3 Fig 5.17 - see also Fig 5.16 (previous slide)) input state production used 3°5+4\n - *5+4\n 3 *5+4\n EF *5+4\n T 5+4\n 1 +4\n TS +4\n TF +4\n T +4\n E 4\n E+ \n E+4 \n E+F \n E+T \n E E\n L L=>E\n 05/10/2016 DFR-CC- SOT L-attributed Definitions (ASU Ch 5.4) Order of evaluation of attributes is linked to the order in which the nodes of the parse tree are created depth-first order « (a syntax-directed definition is L-attributed if each inherited attribute of Xj 1<= j <= n on the RHS of A => X, X, ... X, depends only on — the attributes of X; X2... Xj, to the left of XjinP | left siblings — AND the inherited attributes of A parent NB: every S-attributed definition is L-attributed since the above restrictions apply only to inherited attributes 05/10/2016 DFR-CC- SOT Translation Scheme «su chs. Fig 5.20) Acontext free grammar in which the attributes are associated with grammar symbols and semantic actions enclosed within {} are inserted within the RHS of productions example of a translation scheme + parse tree for 9-5+2 => 95-2+ E=>TR R=> addop T{ print(addop.lexeme) } R, | = (= empty) T=>num — {print(num.val) } Fa Al [yt ae int PY 7 ta (2) > — 9 {print(9)} - 8 {print(’s’)} +2 {prin 05/10/2016 DFR-CC- SOT Translation Scheme «su chs. Fig 5.20) P:T=>T,* F and semantic rule T.val := T,.val * F.val yield production + semantic action T => T, * F { T.val == T;.val * F.val } © for both inherited and synthesised attributes = an inherited attribute for a symbol on the RHS of P must be computed in a (semantic) action before that symbol = an action must not refer to a synthesised attribute on the right of the action — a synthesised attribute for the NT on the left of a rule P can only be computed after all attributes it references have been computed = the action computing such attributes can be placed at the end of the RHS of P — see ASU p 299 for a counter example 05/10/2016 DFR-CC- SOT Top-Down Translation (ASU Ch5.5 Fig 5.24) implementation of L-attribute defi jons during predi parsing using left recursion grammars and left recursion elimination algorithms e.g. translation schema with left recursive grammar => E,+T E,-T T (E) => num 44immm 05/10/2016 { E.val == E,.val + T.val} { E.val == E,.val - T.val } { E.val = T.val} {T.val = E.val } {T.val = num.val } DFR-CC- SOT Top-Down Translation (ASU Ch5.5 Fig 5.25) transformed translation scheme with right recursive grammar (i.e. grammar + interleaved semantic actions) 05/10/2016 DFR-CC- SOT Top-Down Translation (ASU Ch5.5 Fig 5.26) num.val = 2 05/10/2016 DFR-CC- SOT syntax-directed translations - grammar + semantic rules — synthesised attributes inherited attributes = B= Fey ey Cy 6) S-attributed definition — only synthesised attributes dependency graphs constructing syntax trees — syntax-directed definition DAGs for expressions 05/10/2016 bottom up evaluation of S- attributed definitions L-attributed definitions - inherited attributes depend on A and left siblings A => a translation schemes = CFG + attributes + semantic actions in () in RHS of P top down translation = left recursive grammar = right recursive grammar - Xi&Xs attributes DFR-CC- SOT

You might also like