Вы находитесь на странице: 1из 22

Syntax Directed Translation (ASU Ch 5)

 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

1 05/10/2016 DFR - CC - SDT


Syntax Directed Definitions (ASU Ch 5.1)

 grammar + semantic rules


 semantic rules
– compute values of attributes associated with symbols in P
– 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)
 inherited 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

2 05/10/2016 DFR - CC - SDT


Syntax Directed Definitions (ASU Ch 5.1)

 Each grammar P: A =>  has an associated set of semantic


rules of the form b := f(c1, c2, c3, …, ck)
– either
 b is a synthesised attribute of A and c1, c2, c3, …, ck are
attributes of the grammar symbols of P
 b is an inherited attribute of one of the grammar symbols
on the RHS of P and c1, c2, c3, …, ck are attributes of the
grammar symbols of P
– b depends on the attributes c1, c2, c3, …, ck
– 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

3 05/10/2016 DFR - CC - SDT


Synthesised Attribute: example (ASU Fig 5.2)
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 => digit F.val := digit.lexval

\n = new line, X.val = integer value synthesised attribute, digit.lexval is a


synthesised attribute supplied by the lexical analyser

4 05/10/2016 DFR - CC - SDT


Synthesised Attribute: example (ASU Ch 5.1, Fig 5.3)

 In a syntax-directed annotated parse tree: 3*5+4 \n


definition, Ts are assumed to L
have synthesised values only \n
 the start symbol is assumed E.val = 19
NOT to have any inherited
attributes E.val = 15 + T.val = 4
 S-attributed definition: one
T.val = 15 F.val=4
that uses exclusively
synthesised attributes T.val = 3 * F.val = 5 digit.lexval = 4
– can always be annotated
by evaluating the F.val = 3 digit.lexval = 5
semantic rules at each digit.lexval = 3
node bottom up Similarly for type checking – type attr

5 05/10/2016 DFR - CC - SDT


Inherited Attribute: example (ASU Ch 5.1, Figs 5.4. 5.5)

production semantic rules  Parse tree for real id1, id2, id3
D
D => T L L.in := T.type
T => integer T.type := integer T.type = real L.in = real
T => real T.type := real
L => L1, id L1.in := L.in
real L.in = real , id3
addT(id.entry, L.in)
L => id addT(id.entry, L.in)
L.in = real , id2

id1

6 05/10/2016 DFR - CC - SDT


Dependency Graphs (ASU Ch 5.1)

 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 c
 to construct a dependency graph for a parse tree
– put each semantic rule into the form b := f(c1, c2, c3, …, ck)
– the graph has a node for each attribute / an edge from c to b if b
depends on c
– 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 := f(c1, c2, c3, …, ck) do for i := 1 to k construct an edge from ci to b

7 05/10/2016 DFR - CC - SDT


Dependency Graphs (ASU Ch 5.1 Fig 5.7)

 Evaluation order is given by a


topological sort - any TS Example directed graph
gives a valid order of real id1, id2, id3
evaluation for the semantic D
rules 4
 i.e. the dependent attributes 5 6
c1, c2, c3, …, ck are known at T.type = real L.in = real
7
the node before f is evaluated
8
 rule sequence (see figure) real L.in = real , id3 3
a4 := real
a5 := a4
add_type(id3.entry, a5) 9 L.in = real , id2 2
a7 := a5
add_type(id2.entry, a7) 10
a9 := a7 id1 1
add_type(id1.entry, a9)
8 05/10/2016 DFR - CC - SDT
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)
 Rule-based methods: semantic rules associated with Ps
analysed at compiler construction time (either by hand or
specialised tool) - evaluation order predetermined at
compile time
 Oblivious 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

9 05/10/2016 DFR - CC - SDT


Construction of Syntax Trees (ASU Ch 5.2 Ex 5.7)

 Intermediate representation  Syntax trees for expressions


(cf translation to post-fix form)
 de-couples translation from
parsing  operations
 condensed form of parse tree – mknode(op left, right)
– mkleaf(id, entry)
 examples
– mkleaf(num, entry)
3*5+4 if B then S1 else S2
 e.g. a-4+c
+ if-then-else p1 := mkleaf(id, entry-a)
* 4 p2 := mkleaf(num, 4)
B S1 S2 p3 := mknode(‘-’, p1, p2)
3 5 p4 := mkleaf(id, entry-c)
p5 := mknode(‘+’, p3, p4)
see ASU Fig 5.8 for syntax tree

10 05/10/2016 DFR - CC - SDT


Syntax-directed Definition for Constructing Syntax Trees
(ASU Ch 5.2 Fig 5.9)

 S-attributed definition - expressions containing + -


 uses the underlying Ps of the G to schedule the semantic rules
production semantic rules
E => E1 + T E.nptr := mknode(‘+’, E1.nptr, T.nptr)
E => E1 - T E.nptr := mknode(‘-’, E1.nptr, T.nptr)
E => T E.nptr := T.nptr
T => ( E ) T.nptr := E.nptr
T => id T.nptr := mkleaf(id, id.entry)
T => num T.nptr := mkleaf(num, id.entry)
 nptr is 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

11 05/10/2016 DFR - CC - SDT


Directed Acyclic Graphs for Expressions
(ASU Ch 5.2 Fig 5.11)

 a+a*(b-c)+(b-c)* d
 note the common sub-expressions a and (b-c)
+
+ *

*
-
d

a 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

12 05/10/2016 DFR - CC - SDT


Bottom Up Evaluation of S-attributed Definitions
(ASU Ch 5.3 Fig 5.15)

 Syntax-directed definitions with only synthesised attributes


 can be evaluated by a bottom up parser (BUP) as input is parsed
 values associated with the attributes can be kept on the stack as
extra fields
 implementation using an LR parser (e.g. YACC)
 e.g. A => XYZ and A.a := f (X.x, Y.y, Z.z)
state value
X X.x
Y Y.y
TOS Z Z.z

13 05/10/2016 DFR - CC - SDT


S-attributed Definitions: Parser Example
(ASU Ch 5.3 Fig 5.16 see also Fig 5.2)

production semantic rules


L => E \n print(val[TOS])
E => E1 + T val[NTOS] := val[TOS-2] + val[TOS]
E => T
T => T1 * F val[NTOS] := val[TOS-2] * val[TOS]
T => F
F => ( E ) val[NTOS] := val[TOS-1]
F => id

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


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

14 05/10/2016 DFR - CC - SDT


S-attributed Definitions: Parser Example
(ASU Ch 5.3 Fig 5.17 - see also Fig 5.16 (previous slide))

input state value production used


3 * 5 + 4 \n - -
* 5 + 4 \n 3 3
* 5 + 4 \n F 3 F => digit
* 5 + 4 \n T 3 T => F
5 + 4 \n T* 3:
+ 4 \n T*5 3:5
+ 4 \n T*F 3:5 F => digit
+ 4 \n T 15 T => T * F
+ 4 \n E 15 E => T
4 \n E+ 15 :
\n E+4 15 : 4
\n E+F 15 : 4 F => digit
\n E+T 15 : 4 T => F
\n E 19 E => E + T
E \n 19 :
L 19 L => E \n

15 05/10/2016 DFR - CC - SDT


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 => X1 X2 … Xn depends
only on
– the attributes of X1 X2 … Xj-1 to the left of Xj in P 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

16 05/10/2016 DFR - CC - SDT


Translation Scheme (ASU Ch 5.4 Fig 5.20)

 A context 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 => T R
R => addop T { print(addop.lexeme) } R1 | ¤ (¤ = empty)
T => num { print( num.val ) }
E
T R
T print(‘-’) R
T print(‘+’) R
9 { print(‘9’) } - 5 { print(‘5’) } + 2 { print(‘2’) } ¤

17 05/10/2016 DFR - CC - SDT


Translation Scheme (ASU Ch 5.4 Fig 5.20)

 P: T => T1 * F and semantic rule T.val := T1.val * F.val yield


production + semantic action T => T1 * F { T.val := T1.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

18 05/10/2016 DFR - CC - SDT


Top-Down Translation (ASU Ch 5.5 Fig 5.24)

 implementation of L-attribute definitions during predictive


parsing using left recursion grammars and left recursion
elimination algorithms

 e.g. translation schema with left recursive grammar

E => E1 + T { E.val := E1.val + T.val }


E => E1 - T { E.val := E1.val - T.val }
E => T { E.val := T.val }
T => (E) { T.val := E.val }
T => num { T.val := num.val }

19 05/10/2016 DFR - CC - SDT


Top-Down Translation (ASU Ch 5.5 Fig 5.25)

 Grammar  transformed translation scheme with right recursive


grammar (i.e. grammar + interleaved semantic actions)

1. ETR 1. E T { R.i := T.val } R { E.val := R.s }


2. R  + T R1 2. R+T { R1.i := R.i + T.val } R1 { R.s := R1.s }
3. R  - T R1 3. R- T { R1.i := R.i - T.val } R1 { R.s := R1.s }
4. R ¤ 4. R ¤ { R.s := R.i }
5. T(E) 5. T (E ) { T.val := E.val }
6. T  num 6. T  num { T.val := num.val }

X.i - inherited attribute


X.s - synthesised attribute

20 05/10/2016 DFR - CC - SDT


Top-Down Translation (ASU Ch 5.5 Fig 5.26)

 Evaluation of the expression 9-5+2 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-T { R1.i := R.i - T.val } R1 { R.s := R1.s }
E R¤ { R.s := R.i }
T  (E) { T.val := E.val }
T  num { T.val := num.val }

T.val = 9 R.i = 9

num.val = 9 - T.val = 5 R.i = 4

num.val = 5 + T.val = 2 R.i = 6

num.val = 2 ¤

21 05/10/2016 DFR - CC - SDT


Summary

 syntax-directed translations -  bottom up evaluation of S-


grammar + semantic rules attributed definitions
– synthesised attributes  L-attributed definitions
– inherited attributes – inherited attributes depend
– b := f(c1, c2, c3, …, ck) on A and left siblings A => 
 S-attributed definition  translation schemes
– only synthesised attributes – CFG + attributes + semantic
 dependency graphs actions in { } in RHS of P

 constructing syntax trees  top down translation


– syntax-directed definition – left recursive grammar
– right recursive grammar
 DAGs for expressions
– X.i & X.s attributes

22 05/10/2016 DFR - CC - SDT

Вам также может понравиться