29.04.2015 Views

Syntax Directed Translation

Syntax Directed Translation

Syntax Directed Translation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Syntax</strong> <strong>Directed</strong> <strong>Translation</strong> (ASU Ch 5)<br />

• 2 notations for associating semantic rules with productions<br />

– syntax-directed translations (grammar + semantic rules)<br />

– translation schemes (semantic rule evaluation order is given)<br />

• conceptual process<br />

– input string<br />

– parse tree<br />

– dependency graph: dependencies between attributes are<br />

shown e.g. type, string, value, memory location<br />

– semantic rules: compute values of attributes associated with<br />

symbols appearing in a production<br />

• may generate code / save info in symbol table / issue error messages<br />

1<br />

14/10/2014 DFR - CC - SDT


<strong>Syntax</strong> <strong>Directed</strong> Definitions (ASU Ch 5.1)<br />

• grammar + semantic rules<br />

• semantic rules<br />

– compute values of attributes associated with symbols in P<br />

– set up dependencies between attributes => evaluation order<br />

• synthesised attribute<br />

– value at parse tree node determined from attribute values at<br />

the children of the node (can be evaluated in bottom up traversal)<br />

• inherited attribute<br />

– value at parse tree node determined from attribute values at<br />

the parent and/or the left siblings of that node<br />

• annotated parse tree shows the values of attributes at each node<br />

2<br />

14/10/2014 DFR - CC - SDT


<strong>Syntax</strong> <strong>Directed</strong> Definitions (ASU Ch 5.1)<br />

• Each grammar P: A => α has an associated set of semantic<br />

rules of the form b := f(c 1 , c 2 , c 3 , …, c k )<br />

– either<br />

• b is a synthesised attribute of A and c 1 , c 2 , c 3 , …, c k are<br />

attributes of the grammar symbols of P<br />

• b is an inherited attribute of one of the grammar symbols<br />

on the RHS of P and c 1 , c 2 , c 3 , …, c k are attributes of the<br />

grammar symbols of P<br />

– b depends on the attributes c 1 , c 2 , c 3 , …, c k<br />

– attribute grammar: a syntax-directed definition in which the<br />

functions in semantic rules cannot have side effects<br />

– functions in semantic rules are often written as expressions<br />

3<br />

14/10/2014 DFR - CC - SDT


Synthesised Attribute: example (ASU Fig 5.2)<br />

S-attributed definition<br />

production<br />

semantic rules<br />

L => E n print(E.val)<br />

E => E 1 + T E.val := E 1 .val + T.val<br />

E => T E.val := T.val<br />

T => T 1 * F T.val := T 1 .val * F.val<br />

T => F T.val := F.val<br />

F => ( E ) F.val := E.val<br />

F => digit F.val := digit.lexval<br />

n = new line, X.val = integer value synthesised attribute, digit.lexval is a<br />

synthesised attribute supplied by the lexical analyser<br />

4<br />

14/10/2014 DFR - CC - SDT


Synthesised Attribute: example (ASU Ch 5.1, Fig 5.3)<br />

• In a syntax-directed<br />

definition, Ts are assumed to<br />

have synthesised values only<br />

• the start symbol is assumed<br />

NOT to have any inherited<br />

attributes<br />

• S-attributed definition: one<br />

that uses exclusively<br />

synthesised attributes<br />

– can always be annotated<br />

by evaluating the<br />

semantic rules at each<br />

node bottom up<br />

• annotated parse tree: 3*5+4 n<br />

L<br />

n<br />

E.val = 19<br />

E.val = 15 + T.val = 4<br />

T.val = 15<br />

F.val=4<br />

T.val = 3 * F.val = 5 digit.lexval = 4<br />

F.val = 3 digit.lexval = 5<br />

digit.lexval = 3<br />

Similarly for type checking – type attr<br />

5<br />

14/10/2014 DFR - CC - SDT


Inherited Attribute: example (ASU Ch 5.1, Figs 5.4. 5.5)<br />

production semantic rules<br />

D => T L L.in := T.type<br />

T => integer T.type := integer<br />

T => real T.type := real<br />

L => L 1 , id L 1 .in := L.in<br />

addT(id.entry, L.in)<br />

L => id addT(id.entry, L.in)<br />

• Parse tree for real id 1 , id 2 , id 3<br />

D<br />

T.type = real<br />

L.in = real<br />

real L.in = real , id 3<br />

L.in = real , id 2<br />

id 1<br />

6<br />

14/10/2014 DFR - CC - SDT


Dependency Graphs (ASU Ch 5.1)<br />

• A directed graph showing interdependencies among inherited<br />

and synthesised attributes<br />

– if attribute b at a node depends on attribute c => semantic rule for b<br />

must be evaluated after the semantic rule for c<br />

• to construct a dependency graph for a parse tree<br />

– put each semantic rule into the form b := f(c 1 , c 2 , c 3 , …, c k )<br />

– the graph has a node for each attribute / an edge from c to b if b<br />

depends on c<br />

– algorithm:<br />

for each node n in the parse tree do for each attribute a (of n) construct a<br />

node in the dependency graph for a<br />

for each node n in the parse tree do for each semantic rule<br />

b := f(c 1 , c 2 , c 3 , …, c k ) do for i := 1 to k construct an edge from c i to b<br />

7<br />

14/10/2014 DFR - CC - SDT


Dependency Graphs (ASU Ch 5.1 Fig 5.7)<br />

8<br />

• Evaluation order is given by a<br />

topological sort - any TS<br />

gives a valid order of<br />

evaluation for the semantic<br />

rules<br />

• i.e. the dependent attributes<br />

c 1 , c 2 , c 3 , …, c k are known at<br />

the node before f is evaluated<br />

• rule sequence (see figure)<br />

a4 := real<br />

a5 := a4<br />

add_type(id3.entry, a5)<br />

a7 := a5<br />

add_type(id2.entry, a7)<br />

a9 := a7<br />

add_type(id1.entry, a9)<br />

Example directed graph<br />

real id 1 , id 2 , id 3<br />

D<br />

4<br />

T.type = real<br />

L.in = real<br />

real L.in = real , id 3<br />

9<br />

L.in = real , id 2<br />

10<br />

id 1<br />

14/10/2014 DFR - CC - SDT<br />

7<br />

1<br />

5<br />

8<br />

6<br />

2<br />

3


Methods for Evaluating Semantic Rules<br />

(ASU Ch 5.1)<br />

• Parse tree methods: obtain evaluation order at compile time<br />

from the topological sort of the DG for parse tree for each input<br />

(DG must be DAG)<br />

• Rule-based methods: semantic rules associated with Ps<br />

analysed at compiler construction time (either by hand or<br />

specialised tool) - evaluation order predetermined at<br />

compile time<br />

• Oblivious Methods: evaluation order chosen without<br />

considering semantic rules - forced by the parsing method<br />

• the last two do not need to construct the DG explicitly - more efficient in<br />

use of compile time and space<br />

9<br />

14/10/2014 DFR - CC - SDT


Construction of <strong>Syntax</strong> Trees (ASU Ch 5.2 Ex 5.7)<br />

• Intermediate representation<br />

• de-couples translation from<br />

parsing<br />

• condensed form of parse tree<br />

• examples<br />

3<br />

*<br />

3*5+4 if B then S 1 else S 2<br />

+ if-then-else<br />

5<br />

4<br />

B S 1 S 2<br />

• <strong>Syntax</strong> trees for expressions<br />

(cf translation to post-fix form)<br />

• operations<br />

– mknode(op left, right)<br />

– mkleaf(id, entry)<br />

– mkleaf(num, entry)<br />

• e.g. a-4+c<br />

p1 := mkleaf(id, entry-a)<br />

p2 := mkleaf(num, 4)<br />

p3 := mknode(‘-’, p1, p2)<br />

p4 := mkleaf(id, entry-c)<br />

p5 := mknode(‘+’, p3, p4)<br />

see ASU Fig 5.8 for syntax tree<br />

10<br />

14/10/2014 DFR - CC - SDT


<strong>Syntax</strong>-directed Definition for Constructing <strong>Syntax</strong> Trees<br />

(ASU Ch 5.2 Fig 5.9)<br />

• S-attributed definition - expressions containing + -<br />

• uses the underlying Ps of the G to schedule the semantic rules<br />

production<br />

E => E 1 + T<br />

E => E 1 - T<br />

E => T<br />

T => ( E )<br />

T => id<br />

T => num<br />

semantic rules<br />

E.nptr := mknode(‘+’, E 1 .nptr, T.nptr)<br />

E.nptr := mknode(‘-’, E 1 .nptr, T.nptr)<br />

E.nptr := T.nptr<br />

T.nptr := E.nptr<br />

T.nptr := mkleaf(id, id.entry)<br />

T.nptr := mkleaf(num, id.entry)<br />

• nptr is a synthesised attribute - pointer to nodes in the syntax tree<br />

• see ASU Fig 5.8 for the syntax tree and Fig 5.10 for the syntax / parse tree<br />

11<br />

14/10/2014 DFR - CC - SDT


<strong>Directed</strong> Acyclic Graphs for Expressions (<br />

ASU Ch 5.2 Fig 5.11)<br />

• a + a * ( b - c ) + ( b - c ) * d<br />

• note the common sub-expressions a and (b-c)<br />

+<br />

+ *<br />

*<br />

-<br />

d<br />

a<br />

b<br />

c<br />

• see ASU Fig 5.12 (p291) for the instructions (mknode / mkleaf) and Fig 5.13<br />

(p292) for an array implementation of the nodes<br />

12<br />

14/10/2014 DFR - CC - SDT


Bottom Up Evaluation of S-attributed Definitions<br />

(ASU Ch 5.3 Fig 5.15)<br />

• <strong>Syntax</strong>-directed definitions with only synthesised attributes<br />

• can be evaluated by a bottom up parser (BUP) as input is parsed<br />

• values associated with the attributes can be kept on the stack as<br />

extra fields<br />

• implementation using an LR parser (e.g. YACC)<br />

• e.g. A => XYZ and A.a := f (X.x, Y.y, Z.z)<br />

TOS<br />

state<br />

X<br />

Y<br />

Z<br />

value<br />

X.x<br />

Y.y<br />

Z.z<br />

13<br />

14/10/2014 DFR - CC - SDT


S-attributed Definitions: Parser Example<br />

(ASU Ch 5.3 Fig 5.16 see also Fig 5.2)<br />

production<br />

semantic rules<br />

L => E n print(val[TOS])<br />

E => E 1 + T val[NTOS] := val[TOS-2] + val[TOS]<br />

E => T<br />

T => T 1 * F val[NTOS] := val[TOS-2] * val[TOS]<br />

T => F<br />

F => ( E ) val[NTOS] := val[TOS-1]<br />

F => id<br />

– NTOS = TOS - r + 1 ( r is # symbols reduced on RHS)<br />

– see ASU Fig 5.17 (next slide) for example of 3 * 5 + 4<br />

14<br />

14/10/2014 DFR - CC - SDT


S-attributed Definitions: Parser Example<br />

(ASU Ch 5.3 Fig 5.17 - see also Fig 5.16 (previous slide))<br />

15<br />

input state value production used<br />

3 * 5 + 4 n - -<br />

* 5 + 4 n 3 3<br />

* 5 + 4 n F 3 F => digit<br />

* 5 + 4 n T 3 T => F<br />

5 + 4 n T * 3 :<br />

+ 4 n T * 5 3 : 5<br />

+ 4 n T * F 3 : 5 F => digit<br />

+ 4 n T 15 T => T * F<br />

+ 4 n E 15 E => T<br />

4 n E + 15 :<br />

n E + 4 15 : 4<br />

n E + F 15 : 4 F => digit<br />

n E + T 15 : 4 T => F<br />

n E 19 E => E + T<br />

E n 19 :<br />

L 19 L => E n<br />

14/10/2014 DFR - CC - SDT


L-attributed Definitions (ASU Ch 5.4)<br />

• Order of evaluation of attributes is linked to the order in which<br />

the nodes of the parse tree are created<br />

• depth-first order<br />

• a syntax-directed definition is L-attributed if each inherited<br />

attribute of Xj 1


<strong>Translation</strong> Scheme (ASU Ch 5.4 Fig 5.20)<br />

• A context free grammar in which the attributes are associated with<br />

grammar symbols and semantic actions enclosed within { } are<br />

inserted within the RHS of productions<br />

• example of a translation scheme + parse tree for 9-5+2 => 95-2+<br />

E => T R<br />

R => addop T { print(addop.lexeme) } R 1 | ¤<br />

(¤ = empty)<br />

T => num { print( num.val ) }<br />

E<br />

T<br />

R<br />

T print(‘-’) R<br />

T print(‘+’) R<br />

9 { print(‘9’) } - 5 { print(‘5’) } + 2 { print(‘2’) } ¤<br />

17<br />

14/10/2014 DFR - CC - SDT


<strong>Translation</strong> Scheme (ASU Ch 5.4 Fig 5.20)<br />

• P: T => T 1 * F and semantic rule T.val := T 1 .val * F.val yield<br />

production + semantic action T => T 1 * F { T.val := T 1 .val * F.val }<br />

• for both inherited and synthesised attributes<br />

– an inherited attribute for a symbol on the RHS of P must be<br />

computed in a (semantic) action before that symbol<br />

– an action must not refer to a synthesised attribute on the right of the<br />

action<br />

– a synthesised attribute for the NT on the left can only be computed<br />

after all attributes it references have been computed<br />

– the action computing such attributes can be placed at the end of the<br />

RHS of P<br />

– see ASU p 299 for a counter example<br />

18<br />

14/10/2014 DFR - CC - SDT


Top-Down <strong>Translation</strong> (ASU Ch 5.5 Fig 5.24)<br />

• implementation of L-attribute definitions during predictive<br />

parsing using left recursion grammars and left recursion<br />

elimination algorithms<br />

• e.g. translation schema with left recursive grammar<br />

E => E 1 + T { E.val := E 1 .val + T.val }<br />

E => E 1 - T { E.val := E 1 .val - T.val }<br />

E => T { E.val := T.val }<br />

T => ( E ) { T.val := E.val }<br />

T => num { T.val := num.val }<br />

19<br />

14/10/2014 DFR - CC - SDT


Top-Down <strong>Translation</strong> (ASU Ch 5.5 Fig 5.25)<br />

• Grammar<br />

• transformed translation scheme with right recursive<br />

grammar (i.e. grammar + interleaved semantic actions)<br />

1. E T R<br />

2. R + T R 1<br />

3. R - T R 1<br />

4. R ¤<br />

5. T ( E )<br />

6. T num<br />

1. E T { R.i := T.val } R { E.val := R.s }<br />

2. R + T { R 1 .i := R.i + T.val } R 1 { R.s := R 1 .s }<br />

3. R - T { R 1 .i := R.i - T.val } R 1 { R.s := R 1 .s }<br />

4. R ¤ { R.s := R.i }<br />

5. T ( E ) { T.val := E.val }<br />

6. T num { T.val := num.val }<br />

X.i - inherited attribute<br />

X.s - synthesised attribute<br />

20<br />

14/10/2014 DFR - CC - SDT


Top-Down <strong>Translation</strong> (ASU Ch 5.5 Fig 5.26)<br />

• Evaluation of the expression 9-5+2<br />

T.val = 9 R.i = 9<br />

E<br />

E T { R.i := T.val } R { E.val := R.s }<br />

R + T { R 1 .i := R.i + T.val } R 1 { R.s := R 1 .s }<br />

R - T { R 1 .i := R.i - T.val } R 1 { R.s := R 1 .s }<br />

R ¤ { R.s := R.i }<br />

T (E) { T.val := E.val }<br />

T num { T.val := num.val }<br />

num.val = 9 - T.val = 5 R.i = 4<br />

num.val = 5 + T.val = 2 R.i = 6<br />

num.val = 2 ¤<br />

21<br />

14/10/2014 DFR - CC - SDT


Summary<br />

• syntax-directed translations -<br />

grammar + semantic rules<br />

– synthesised attributes<br />

– inherited attributes<br />

– b := f(c 1 , c 2 , c 3 , …, c k )<br />

• S-attributed definition<br />

– only synthesised attributes<br />

• dependency graphs<br />

• constructing syntax trees<br />

– syntax-directed definition<br />

• DAGs for expressions<br />

• bottom up evaluation of S-<br />

attributed definitions<br />

• L-attributed definitions<br />

– inherited attributes depend<br />

on A and left siblings A => α<br />

• translation schemes<br />

– CFG + attributes + semantic<br />

actions in { } in RHS of P<br />

• top down translation<br />

– left recursive grammar<br />

– right recursive grammar<br />

– X.i & X.s attributes<br />

22<br />

14/10/2014 DFR - CC - SDT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!