Presentation is loading. Please wait.

Presentation is loading. Please wait.

Directed Acyclic Graphs (DAG)

Similar presentations


Presentation on theme: "Directed Acyclic Graphs (DAG)"— Presentation transcript:

1 Directed Acyclic Graphs (DAG)

2 Expression: a + a * ( b - c ) + ( b - c ) * d Parse Tree

3 Expression: a + a * ( b - c ) + ( b - c ) * d Parse Tree DAG

4 sequence of instructions
(1) p1  := mkleaf(id,a);        (2) p2  := mkleaf(id,a);        (3) p3  := mkleaf(id,b);        (4) p4  := mkleaf(id,c);        (5) p5  := mknode(' - ' ,p3,p4);        (6) p6  := mknode(' * ' ,p2,p5);        (7) p7  := mknode(' + ' ,p1,p6);        (8) p8  := mkleaf(id,b);        (9) p9  := mkleaf(id,c);      (10) p10 := mknode(' - ' ,p8,p9);      (11) p11 := mkleaf(id,d);      (12) p12 := mknode(' * ' ,p10,p11);      (13) p13 := mknode(' + ' ,p7,p12); a + a * ( b - c ) + ( b - c ) * d

5 Array Representation

6 Bottom-Up Evaluation of S-Attributed Definitions
A syntax directed definition that uses synthesized attributes exclusively is said to be an S- attributed definition. Table: Parser stack with attributes. State Val X X.x Y Y.x Z Z.x top

7 Production Code fragment
L->E n         print(val[top])   E->E + T       val[ntop] :=val[top-2] + val[top]   E->T   E->T*F         val[ntop] :=val[top-2] * val[top]   T->F   F->(E)            val[ntop] := val[top-1]   F-> digit

8

9 L-attributed definition
These are definitions that define rules for determining inherited attributes in which each inherited attribute depends only on · The attributes of the symbols to the left of it in the production · The inherited attributes of the non-terminal on the left-side of the production Every L-attributed definition is L-attributed, as the rules stated above apply only to inherited attributes. L-attributed definitions are said to be L-attributed as information flows from left to right in the syntax tree.

10 Order of Evaluation dfvisit(node n) {
  for each child m of n, from left to right evaluate inherited attributes of m   dfvisit(m) } evaluate synthesized attributes of n

11 Translation Scheme E->TR R->add op T {print(add op.lexeme)}R1|€
T->num {print(num.val)} Parse tree for 9-5+2

12 Top Down Translation A translation scheme must be designed carefully if we have both inherited and synthesized attributes.

13 Grammar with left recursion:
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;}

14 Equivalent grammar without left-recursion:
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;} R-> € {R.s=R.i;} T->(E) {T.val=E.val;} T->num {T.val=num.val;}

15 Equivalent grammar without left-recursion:
Evaluation of 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;} R-> € {R.s=R.i;} T->(E) {T.val=E.val;} T->num {T.val=num.val;}

16 A syntax directed definition for constructing syntax trees
E-> T { R.i=T.ptr } 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) } R-> € { R.s=R.i;} T->(E) { T.nptr=E.nptr } T->id { T.nptr=mkleaf(id,id.entry) } T->num { T.nptr=mkleaf(num,num.val) }

17 Syntax trees using inherited attributes for the expression a-4+c

18 Bottom-Up Evaluation of Inherited Attributes
Grammar has no left-recursion and it is left-factored.

19 Translation scheme E  T R
R  + T { print(‘+’) } R | - T { print(‘-’) } R |  T  num { print(num.val) }

20 PRODUCTION SEMANTIC RULE
D  T L L.in = T.type T  int T.type = integer T  real T.type = real L  L1 , id L1.in = L.in addtype(id.entry, L.in) L  id addtype(id.entry, L.in)

21 Example


Download ppt "Directed Acyclic Graphs (DAG)"

Similar presentations


Ads by Google