Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax-Directed Translation

Similar presentations


Presentation on theme: "Syntax-Directed Translation"— Presentation transcript:

1 Syntax-Directed Translation
Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called “annotating the parse tree.” Computing values to be stored at the annotated nodes is called “decorating the parse tree.”

2 Synthesized Attributes
Attributes passed up the parse tree. For example, the following involves synthesizing attributes: expr: expr ‘+’ expr { $$ = $1; }

3 Synthesized Attribute
$$ = $1; E ($$) / | \ E ($1) E ($2)

4 Inherited Attributes Attributes passed downwards or sideways in the parse tree. The following involves passing attributes down the parse tree: expr: expr ‘+’ expr { $1 = $$; } The following involves passing sideways the parse tree: expr: expr ‘+’ expr { $1 = $2; }

5 Attribute Inherited Downwards
$1 = $$; E ($$) / | \ E ($1) E ($2)

6 Attribute Inherited Sideways
$2 = $1; E ($$) / | \ E ($1) E ($2)

7 Desk Calculator - Semantic Rules
Production Semantic Rules L  E n E  E1 + T E  T T  T1 * F T  F F  ( E ) F  digit 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 = digit.lexval

8 T.val = 3 * F.val = 5 digit.lexval = 4
Parse Tree for 3 * n L | \ E.val = n / | \ E.val = T.val = 4 | | T.val = F.val = 4 / | \ | T.val = 3 * F.val = digit.lexval = 4 | | F.val = digit.lexval = 5 | digit.lexval = 3

9 Declarations – Semantic Rules
production Semantic Rules D  T L T  int T  float L  L1 , id L  id L.in = T.type T.type = integer T.type = float L1.in = L.in addtype(id.entry,L.in)

10 Parse Tree for float id1, id2, id3
/ \ T.type = float L.in = float | / | \ float L.in = float , id3 / | \ L.in = float , id2 | id1

11 Dependency Graph 4 5 T.type L.in 7 6 float L.in , id3 9 8 3
T.type L.in float L.in , id3 L.in , id id1 1

12 Topological Sort Any ordering m1, m2,… mk of nodes of a graph such that if mi  mj is an edge from mi to mj in the graph, then mi appears before mj in the ordering

13 Evaluation order T  float D  T L L  L , id3 L  L , id2 L  id1

14 Evaluation Action Node 1 – Create id1 Node 2 – Create id2
Node 4 – a4 = real Node 5 – a5 = a4 Node 6 – addtype(id3.entry, a5) Node 7 – a7 = a5 Node 8 – addtype(id2.entry, a7) Node 9 – a9 = a7 Node 10 – addtype(id1.entry, a9)

15 Evaluation of Semantic Rules
Parse Tree Methods – Arrange productions at compile time. Fails only if there are cycles in graph Rule-Based Methods – Design compiler so that it analyzes productions and outputs them in the proper order Oblivious Methods – Don’t worry about order. This restricts the class of syntax-directed definitions that can be implemented

16 Abstract Syntax Tree (AST)
(a  b) + ((a  b) + c) + / \ / \ / \ a b  c / \ a b

17 Table Representation of AST
(1) a b (2) (3) + c (4)

18 Directed Acyclic Graph (DAG)
(a  b) + ((a  b) + c) + \ / \  c a b

19 Table Representation of DAG
(1) a b (2) + c (3)

20 Generate AST from Productions
Semantic Rules E  E1 + T E  E1 – T E  T T  ( E ) T  id T  num E.nptr = mknode(‘+’, E1.nptr,T.nptr) E.nptr = mknode(‘’, E1.nptr,T.nptr) E.nptr = T.nptr T.nptr = E.nptr T.nptr = mkleaf(id, id.entry) T.nptr = mkleaf(num, num.val)

21 Parse Tree for a – 4 + c E / | \ E T / | \ | E – T c | | T | a

22 Translation to AST – Example 1
p1 = mkleaf(id,a); p2 = mkleaf(num,4); p3 = mknode(– p1, p2); p4 = mkleaf(id,c); p5 = mknode(– p3, p4); + id c id a num 4

23 Translation to AST – Example 2
(a  b) + ((a  b) + c) p1 = mkleaf(id,a) p2 = mkleaf(id,b) p3 = mknode(‘-’, p1, p2) p4 = mkleaf(id,a) p5 = mkleaf(id,b) p6 = mknode(‘-’, p4, p5) p7 = mkleaf(id,c) p8 = mknode(‘+’, p6, p7) p9 = mknode(‘+’, p3, p8)

24 Triples from AST If a = 2, b = 7, c = 9 Triples Optimized Triples
= a 2 (1) = a 2 = b 7 (2) = b 7 – (1) (2) (3) – (1) (2) = a 2 (7) = c 9 = b 7 (8) + (3) (7) – (1) (2) (9) + (3) (8) = c 9 + (6) (7) + (3) (8)

25 Finding Redundant Triples
9 11 13 (1) = a 2 (2) = b 7 (3) (1) (2)

26 S-Attributed Definition
A syntax-directed definition that uses synthesized attributes exclusively is said to be an S-Attributed Definition Can be evaluated properly with a bottom up parser

27 Bottom-Up Parse Given the production and semantic rule:
A  XYZ A.a = f(X.x,Y.y, Z.z) State Val X X.x Y Y.y Top  Z Z.z

28 Desktop Calculator (Revisited)
Production Code Fragment L  E n E  E1 + T E  T T  T1 * F T  F F  ( E ) F  digit print(val[top]) val[ntop] = val[ntop]+val[top] val[ntop] = val[ntop]*val[top] val[ntop] = val[top – 1]

29 Management of top and ntop
When a prduction with r symbols on the right side is reduced, the value of ntop is set to top – r + 1 After each code fragment is executed, top is set to ntop

30 Translation of 3*5+4n Input state val Production Used 3 * 5 + 4 n -
F  digit * n T T  F 5 + 4 n T * 3 _ + 4 n T * 5 3 _ 5 T * F 15 E E  T 4 n E + 15 _ n E + 4 15 _ 4 E + F E + T 19 E  E + T E n 19 _ L L  E n

31 Depth-First Evaluation
Used with top-down parsers: Procedure dfvisit(n:node); begin for each child m of n, from left to right do evaluate inherited attributes of m; dfvisit(m) end; evaluate synthesized attributes of n; end

32 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  X1, X2, … Xn, depends only on: The Attributes X1, X2, … Xj–1 to the left of Xj in the production and the inherited attributes of A By definition, any S-Attributed definition is L-Attributed, because the restrictions above only apply to inherited attributes

33 non-L-Attributed Definition
Production Semantic Rules A  L M L.i = f(A.i) M.i = g(L.s) A.s = h(M.s) A  Q R R.i = f(A.i) Q.i = g(R.s)  PROBLEM! A.s = h(Q.s)

34 Inherited and Synthesized Attributes
An inherited attribute for a symbol on the right side of a production must be computed in an action before that symbol An action must not refer to a synthesized attribute of a symbol to the right of the action A synthesized attribute for the non-terminal on the left can only be computed after all attributes it references have been computed. The action computing such attributes is usually placed at the end of the right side of the production

35 Problem Grammar S  A1 A2 {A1.in = 1; A2.in = 2} A  a { print(A1.in)}
Solution: S  {A1.in = 1} A1 {A2.in = 2} A2

36 Problem Grammar Production Semantic Rules S  B B.ps = 10 S.ht = B.ht
B  B1 B2 B1 = B.ps B2 = B.ps B.ht = max(B1.ht, B2.ht) B  B1 sub B2 B2.ps = shrink(B.ps) B.ht = disp(B1.ht, B2.ht) B  text B.ht = text.h x B.ps

37 Solution The following handles both synthesized and inherited attributes in top down parsing S  {B.ps = 10} B {S.ht = B.ht} B  {B1 = B.ps} B1 {B2 = B.ps} B2 {B.ht = max(B1.ht, B2.ht)} B  {B1 = B.ps} B1 sub {B2.ps = shrink(B.ps)} B2 {B.ht = disp(B1.ht, B2.ht)} B  text {B.ht = text.h x B.ps}

38 Left Recursive Grammar
Production Semantic Rules E  E1 + T E  E1 – T E  T T  F F  ( E ) F  num E.val = E1.val + T.val E.val = E1.val – T.val E.val = T.val T.val = F.val F.val = E.val F.val = num.val

39 Left Recursion Eliminated
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} R  e {R.s = R1.s} T  ( E ) {T.val = E.val} T  num {T.val = num.val}

40 Inherited Attributes 9 – 5 + 2
T.val=9 R.i=9 num.val=9 – T.val= R.i=4 num.val=5 + T.val=2 R.i=6 num.val=2

41 Left Recursive Productions
A  A1Y {A.a = g(A1.a,Y.y)} A  X {A.a = f(X.x)} Left recursion eliminated: A  X {R.i = f(X.x)} R {A.a = R.s} R  Y {R1.i = g(R.i, Y.y)} R1 {R.s = R1.s} R  e {R.s = R.i}

42 Attribute Final Result is Preserved
A.a = g(g(f(X.x),Y1.y),Y2.y) A Y X R.i = f(X.x) A.a = g(f(X.x),Y1.y) Y R.i = g(f(X.x),Y1.y) Y Y R.i = g(g(f(X.x),Y1.y),Y2.y) A.a = f(X.x) e X

43 Example 2 Production Semantic Rules E  E1 + T E  E1 – T E  T
T  id T  num E.nptr = mknode(‘+’, E1.nptr,T.nptr) E.nptr = mknode(‘’, E1.nptr,T.nptr) E.nptr = T.nptr T.nptr = E.nptr T.nptr = mkleaf(id, id.entry) T.nptr = mkleaf(num, num.val) T.nptr = mkleaf(id, id.val)

44 Example 2, Left Recursion Eliminated
E  T {R.i = T.nptr} R {E.nptr = R.s} R  + T {R1.i = mknode(‘+’,R.i,T.nptr} R1 {R.s = R1.s} R  – T {R1.i = mknode(‘–’,R.i,T.nptr} R1 {R.s = R1.s} R  e {R.s = R1.s} T  ( E ) {T.nptr = E.nptr} T  num {T.val = mkleaf(num,num.val)} T  num {T.val = mkleaf(id,id.val)}

45 Moving Semantic Rules to End
In a top-down parse, if there are embedded semantic rules, a marker non-terminal can be added to the grammar moving all semantic rules to the end of productions

46 Inserting Marker M E  T R
R  + T {print(‘+’) R | – T {print(‘–’) R | e T  num {print(num.val)} E  T R R  + T M R | – T N R | e T  num {print(num.val)} M  e {print(‘+’)} N  e {print(‘–’)}

47 Declaration Grammar production Semantic Rules D  T L T  int
T  float L  L1 , id L  id L.in = T.type T.type = integer T.type = float L1.in = L.in addtype(id.entry,L.in)

48 Inheritance T.type L.in float L.in , r L.in , q p

49 Bottom Up Parse Input state Production Used real p, q, r - p, q, r
T  real , q, r T p T L L  id q, r T L , , r T L , q L  L , id r T L , r D D  T L

50 Looking Down in the Parse Stack
In this case, the thing we want (type info) is set early, and it is always is available a fixed distance away from where we want to use it to store typing information in a symbol table entry.

51 Revised Rules production Code Fragment D  T L T  int T  float
L  L1 , id L  id val[ntop] = integer val[ntop] = float addtype(val[top],val[top – 3]) addtype(val[top],val[top – 1])

52 Parse Stack Problems Sometimes what we need is in fact on the parse stack but instead of always being available at some fixed distance from the stack top, it is a variable distance down depending on which productions were applied

53 Variable Stack Distance Example
Production Semantic Rules S  a A C C.i = A.s S  a A B C C  c C.s = g(C.i)

54 Bad Solution Production Code Fragment S  a A C S  b A B C C  c
sval[top] = sval[top – 1]

55 Inserting Marker M production Semantic Rules S  a A C S  b A B M C
C  c M  e C.i = A.s M.i = A.s ; C.i = M.s C.s = g(C.i) M.s = M.i

56 Variable Distance Solution
production Code Fragment S  a A C S  b A B M C C  c M  e sval[top] = sval[top – 1]


Download ppt "Syntax-Directed Translation"

Similar presentations


Ads by Google