230 likes | 406 Views
Lecture 9. Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up). Part I. Top-Down Table-Driven Syntax-Directed Translation. Top-Down Table-Driven SDT. Augment the parser algorithm to implement attribute migration
E N D
Lecture 9 Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up) Joey Paquet, 2000-2014
Part I Top-Down Table-Driven Syntax-Directed Translation Joey Paquet, 2000-2014
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, 2000-2014
Top-Down Table-Driven SDT Joey Paquet, 2000-2014
Q Attribute migration id1+id2*id3$ E B:{Es = Qs} A:{Qi = Ts} T Q D:{Qs = Qs} G:{Ts = Rs} C:{Qi = Qi+Ts} T + G:{Ts = Rs} F R 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, 2000-2014
Q Attribute migration id1*id2+id3$ E B:{Es = Qs} A:{Qi = Ts} T Q D:{Qs = Qs} G:{Ts = Rs} C:{Qi = Qi+Ts} T + F R E:{Qs = Qi} F:{Ri = Fs} G:{Ts = Rs} I:{Rs = Rs} K:{Fs = id.val} id F:{Ri = Fs} F R F R * H:{Ri = Ri * Fs} J:{Rs = Ri} J:{Rs = Ri} K:{Fs = id.val} K:{Fs = id.val} id id Joey Paquet, 2000-2014
Parsing Example Joey Paquet, 2000-2014
Parsing Example Joey Paquet, 2000-2014
Part II Top-Down Syntax-Directed Translation Grammar Transformation Joey Paquet, 2000-2014
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, 2000-2014
a+b*c E E TE’ E’ +TE’ | T FT’ T’ FT’ | F id T1 E’1 T’1 E’2 F1 + T2 id (va : ) F2 T’2 T’3 id (vb : ) F3 * id (vc : ) Top-Down Translation: Problem Joey Paquet, 2000-2014
With Left Recursions 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)} a+b*c E {Es = Es * Ts} E + T {Es = Ts} {Ts = Ts * Fs} T T {Ts = Fs} F * {Es = Ts} {Fs = lookup(c)} F F id (vc: ) {Fs = lookup(b)} {Fs = lookup(a)} id (va: ) id (vb: ) Joey Paquet, 2000-2014
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} T E’ {E’s = E’i+E’s} {Ts = T’s} {E’i = Ts} + F T’ T E’ {Fs = lookup(a)} {T’s = Ti} {Ts = T’s} {E’s = E’i} {T’i = Fs} {E’i = Ts} id (va: ) F T’ {Fs = lookup(b)} {T’s = Ti*T’s} {T’i = Fs} * id (vb: ) F T’ {T’s = Ti} {Fs = lookup(c)} {T’i = Fs} Joey Paquet, 2000-2014 id (vc: )
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, 2000-2014
E1 E2+T1E1 T2E1’ E3 T2 E2’ +T1E3’ E4’ 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{E1s = E2s+T1s} E1 T2{E1’i = T2s}E1’{E1s = E1’s} E3 T2{E3s = T2s} E2’ +T1{E3’i = E2’i+T1s}E3’{E2’s = E3’s} E4’ {E4’s = E4’i} Top-Down Translation: Example 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, 2000-2014
Part III Bottom-Up Table-Driven Syntax-Directed Translation Joey Paquet, 2000-2014
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, 2000-2014
nodeKind = (internal,leaf) treeNode = record token : tokenType case kind : nodeKind of internal : (left,right : *treeNode) leaf : (location : integer) end 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: Joey Paquet, 2000-2014
nodePtr makeLeaf(tok tokenType, location integer{ leafPtr = new(nodePtr) leafPtr.kind = leaf leafPtr.token = tok leafPtr.location = location return leafPtr} nodePtr makeTree(op tokenType, rightSon,leftSon nodePtr){ rootPtr = new(nodePtr) rootPtr.kind = internal rootPtr.token = op rootPtr.left = leftSon rootPtr.right = rightSon return rootPtr} 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: Joey Paquet, 2000-2014
Bottom-Up SDT: Example Joey Paquet, 2000-2014
Parsing Example Joey Paquet, 2000-2014
E4 + + + + + id1 id1 id1 id1 id1 = E2 E5 E2 S E1 E1 E4 * * * id2 id2 id2 id2 id2 id3 id3 id3 E5 E3 E6 E6 E3 id4 5 8 10 11 E E B D 14 15 16 E C A Joey Paquet, 2000-2014
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, 2000-2014