Presentation is loading. Please wait.

Presentation is loading. Please wait.

Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)

Similar presentations


Presentation on theme: "Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)"— Presentation transcript:

1 Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)
Lecture 9 Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up) Joey Paquet,

2 Top-Down Table-Driven Syntax-Directed Translation
Part I Top-Down Table-Driven Syntax-Directed Translation Joey Paquet,

3 Top-Down Table-Driven SDT
Augment the parser algorithm to implement attribute migration Introduce additional symbols in the grammar’s right hand sides for semantic actions that process semantic attributes When such a symbol is on top of the stack, execute the semantic action Problem: the attributes have to be pushed and popped at a different pace compared to symbols on the parsing stack Solution: use an additional stack (the semantic stack) to store the attributes Joey Paquet,

4 Top-Down Table-Driven SDT
E  TAQB r2: Q1  +TCQ2D r3: Q  E r4: T  FFRG r5: R1  *FHR2I r6: R  J r7: F  idK r8: F  (E)L id ( ) + * $ E r1 Q r3 r2 T r4 R r6 r5 F r7 r8 A: {Qi = Ts} B: {Es = Qs} C: {Q2i = Q1i+Ts} D: {Q1s = Q2s} E: {Qs = Qi} F: {Ri = Fs} G: {Ts = Rs} H: {R2i = R1i * Fs} I: {R1s = R2s} J: {Rs = Ri} K: {Fs = id.val} L: {Fs = Es} Joey Paquet,

5 Attribute migration id1+id2*id3$ E T Q + T Q  F R F R  id id * F R
B:{Es = Qs} A:{Qi = Ts} T Q D:{Qs = Qs} G:{Ts = Rs} C:{Qi = Qi+Ts} + T Q F R G:{Ts = Rs} E:{Qs = Qi} F:{Ri = Fs} F R J:{Rs = Ri} F:{Ri = Fs} K:{Fs = id.val} K:{Fs = id.val} I:{Rs = Rs} id id * F R H:{Ri = Ri * Fs} J:{Rs = Ri} K:{Fs = id.val} id Joey Paquet,

6 Attribute migration id1*id2+id3$ E T Q + T Q  F R id * F R F R  id 
B:{Es = Qs} A:{Qi = Ts} T Q D:{Qs = Qs} G:{Ts = Rs} C:{Qi = Qi+Ts} + T Q F R E:{Qs = Qi} F:{Ri = Fs} G:{Ts = Rs} I:{Rs = Rs} K:{Fs = id.val} id * F R F:{Ri = Fs} F R H:{Ri = Ri * Fs} J:{Rs = Ri} J:{Rs = Ri} K:{Fs = id.val} K:{Fs = id.val} id id Joey Paquet,

7 Parsing Example parsing stack input action semantic stack 1 $E
id1*id2+id3$ r1 2 $BQAT r4 3 $BQAGRFF r7 4 $BQAGRFKid 5 $BQAGRFK *id2+id3$ K F1s[id1.val] 6 $BQAGRF F R1i[id1.val] 7 $BQAGR r5 8 $BQAGIRHF* 9 $BQAGIRHF id2+id3$ 10 $BQAGIRHKid 11 $BQAGIRHK +id3$ R1i[id1.val] F2s[id2.val] 12 $BQAGIRH H R2i[id1.val * id2.val] 13 $BQAGIR r6 14 $BQAGIJ J R2s[id1.val * id2.val] 15 $BQAGI I R1s[id1.val * id2.val] 16 $BQAG G T1s[id1.val * id2.val] 17 $BQA A Q1i[id1.val * id2.val] Parsing Example Joey Paquet,

8 Parsing Example parsing stack input action semantic stack 18 $BQ +id3$
Q1i[id1.val * id2.val] 19 $BDQCT+ 20 $BDQCT id3$ r4 21 $BDQCGRFF r7 22 $BDQCGRFKid 23 $BDQCGRFK $ K Q1i[id1.val * id2.val] F3s[id3.val] 24 $BDQCGRF F Q1i[id1.val * id2.val] R3i[id3.val] 25 $BDQCGR r6 26 $BDQCGJ J Q1i[id1.val * id2.val] R3s[id3.val] 27 $BDQCG G Q1i[id1.val * id2.val] T2s[id3.val] 28 $BDQC C Q2i[id1.val * id2.val + id3.val] 29 $BDQ r3 30 $BDE E Q2s[id1.val * id2.val + id3.val] 31 $BD D Q1s[id1.val * id2.val + id3.val] 32 $B B E1s[id1.val * id2.val + id3.val] 33 accept Parsing Example Joey Paquet,

9 Top-Down Syntax-Directed Translation Grammar Transformation
Part II Top-Down Syntax-Directed Translation Grammar Transformation Joey Paquet,

10 Top-Down Translation Problem: Left recursion is not allowed in predictive top-down parsing. We must transform the grammar. We saw how to transform a context-free grammar to remove left recursions. How is an attribute grammar transformed? Joey Paquet,

11 Top-Down Translation: Problem
* F1 id (va : ) T’1 F2 id (vb : ) E’2 T’3 F3 id (vc : ) E’1 + a+b*c E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  id Joey Paquet,

12 With Left Recursions a+b*c E E + T T T * F F F id (vc : ) id (va : )
E  E+T {Es = Es*Ts} E  T {Es = Ts} T  T*F {Ts = Ts*Fs} T  F {Ts = Fs} F  id {Fs = lookup(id)} E {Es = Es * Ts} {Es = Ts} E + T {Ts = Ts * Fs} {Ts = Fs} T {Es = Ts} T * F {Fs = lookup(c)} F F {Fs = lookup(b)} id (vc : ) {Fs = lookup(a)} id (va : ) id (vb : ) Joey Paquet,

13 Without Left Recursions
a+b*c E  T{E’i=Ts}E’{Es=E’s} E’  +T{E’i=Ts}E’{E’s=E’i+E’s} E’  {E’s=E’i} T  F{T’i=Fs}T’{Ts=T’s} T’  F{T’i=Fs}T’ {T’s=Ti*T’s} T’  {T’s=Ti} F  id{Fs=lookup(id)} E {Es = E’s} {Ts = T’s} T E’ {E’s = E’i+E’s} {E’i = Ts} + {Fs = lookup(a)} F T’ {T’s = Ti} {Ts = T’s} T E’ {E’s = E’i} {T’i = Fs} {E’i = Ts} id (va : ) {Fs = lookup(b)} F T’ {T’s = Ti*T’s} {T’i = Fs} * id (vb : ) {Fs = lookup(c)} F T’ {T’s = Ti} {T’i = Fs} Joey Paquet, id (vc : )

14 Top-Down Translation Solution: The grammar is transformed as we saw before. But changing the grammar spreads some attributes over multiple rules, thus introducing attribute inheritance. The following transformation should be applied: A1  A2 A1  Q1 A3   Q2  Q3 Q4   A1  A2 {A1s = f(A2s, )} A1  {Q1i = g()}Q1{A1s = Q1s} A3   {A3s = g()} Q2  {Q3i = f(Q2i, )}Q3{Q2s = Q3s} Q4  {Q4s = Q4i} Joey Paquet,

15 Top-Down Translation: Example
A1  A2 A1  Q1 A3   Q2  Q3 Q4   where: A1,2,3  E1,2,3 Q1,2,3,4  E’1,2,3,4   +T1   T2 f(A2s, )  E2s+T1s g()  T2s E1  E2+T1 E1  T2E1’ E3  T2 E2’  +T1E3’ E4’   A1  A2 {A1s = f(A2s, )} A1  {Q1i = g()}Q1{A1s = Q1s} A3   {A3s = g()} Q2  {Q3i = f(Q2i, )}Q3{Q2s = Q3s} Q4  {Q4s = Q4i} E1  E2+T1 {E1s = E2s+T1s} E1  T2{E1’i = T2s}E1’{E1s = E1’s} E3  T {E3s = T2s} E2’  +T1{E3’i = E2’i+T1s}E3’{E2’s = E3’s} E4’  {E4’s = E4’i} Joey Paquet,

16 Bottom-Up Table-Driven Syntax-Directed Translation
Part III Bottom-Up Table-Driven Syntax-Directed Translation Joey Paquet,

17 Bottom-Up SDT Syntax-directed translation is much easier to implement bottom-up than top-down. Synthetized attributes are propagated from the bottom-up, so we have this propagation mechanism for free in a bottom-up parse The presence of inherited attributes generally comes from the elimination of left-recursions and ambiguities. As these are not a problem in bottom-up parsing, we seldom need to process inherited attributes in bottom-up translation In bottom-up translation, the parse and semantic stacks move in synchronism. Semantic rules are triggered as handles are popped from the stack Joey Paquet,

18 Bottom-Up SDT: Building a Tree
The tree is built by grafting subtrees (handles) to the nodes The semantic actions build the subtrees by grafting generally through simple pointer manipulations Generally, all nodes (internal or leaves) are of the same type. Differences can be managed by using a variant record structure: nodeKind = (internal,leaf) treeNode = record token : tokenType case kind : nodeKind of internal : (left,right : *treeNode) leaf : (location : integer) end Joey Paquet,

19 Bottom-Up SDT: Building a Tree
We need a makeTree function that will be used to create subtrees and a makeLeaf function that will be used to create tree leaves: nodePtr makeTree(op tokenType, rightSon,leftSon nodePtr){ rootPtr = new(nodePtr) rootPtr.kind = internal rootPtr.token = op rootPtr.left = leftSon rootPtr.right = rightSon return rootPtr} nodePtr makeLeaf(tok tokenType, location integer{ leafPtr = new(nodePtr) leafPtr.kind = leaf leafPtr.token = tok leafPtr.location = location return leafPtr} Joey Paquet,

20 Bottom-Up SDT: Example
r1: S  id = EA r2: E1  E2 + E3B r3: E1  E2 * E3C r4: E1  (E2)D r5: E  idE A: {S.root = makeTree(‘=‘, id.location, E.root)} B: {E1.root = makeTree(‘+’, E2.root, E3.root)} C: {E1.root = makeTree(‘*’, E2.root, E3.root)} D: {E1.root = E2.root} E: {E1.root = makeLeaf(id, id.location)} Joey Paquet,

21 Parsing Example parsing stack input action 1 $ id = (id+id)*id$ 2 $id
3 $id= (id+id)*id$ 4 $id=( id+id)*id$ 5 $id=(id +id)*id$ r5 E 6 $id=(E1 7 $id=(E1 + id)*id$ 8 $id=(E1 + id )*id$ 9 $id=(E1 + E2 10 $id=(E1 + E2) *id$ r2 B 11 $id=(E3) r4 D 12 $id=E4 13 $id=E4* id$ 14 $id=E4* id 15 $id=E4* E5 r3 C 16 $id=E6 r1 A 17 $S accept Parsing Example Joey Paquet,

22 5 8 10 11 E1 E2 E3 E3 E4 + + + id1 id2 E1 E2 id1 id2 id1 id2 E E B D
14 15 16 S E5 E6 E6 id4 = * * E4 E5 + id3 * id3 E id1 id3 id2 + C id1 id2 A Joey Paquet,

23 Bottom-Up SDT We can use a similar process to build other kinds of intermediate representations A similar process can also be used to generate target code directly, but that diminishes the possibilities of high-level code optimization Joey Paquet,


Download ppt "Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)"

Similar presentations


Ads by Google