Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler design Table-driven syntax-directed translation

Similar presentations


Presentation on theme: "Compiler design Table-driven syntax-directed translation"— Presentation transcript:

1 Compiler design Table-driven syntax-directed translation
COMP 442/6421 – Compiler Design Compiler design Table-driven syntax-directed translation Joey Paquet,

2 Top-down table-driven syntax-directed translation
COMP 442/6421 – Compiler Design Top-down table-driven syntax-directed translation Joey Paquet,

3 Top-down table-driven syntax-directed translation
COMP 442/6421 – Compiler Design Top-down table-driven syntax-directed translation 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. The grammar becomes an attribute grammar. 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. The semantic actions typically pop semantic records from the semantic stack, do some processing, then push a semantic record on the stack. Joey Paquet,

4 Top-down table-driven syntax-directed translation
COMP 442/6421 – Compiler Design Top-down table-driven syntax-directed translation Attribute Grammar r1: E  TAE’B r2: E’1  +TCE’2D r3: E’  E r4: T  FFT’G r5: T’1  *FHT’2I r6: T’  J r7: F  idK r8: F  (E)L id ( ) + * $ E r1 E’ r3 r2 T r4 T’ r6 r5 F r7 r8 Semantic Actions A: {E’i = Ts} B: {Es = E’s} C: {E’2i = E’1i+Ts} D: {E’1s = E’2s} E: {E’s = E’i} F: {T’i = Fs} G: {Ts = T’s} H: {T’2i = T’1i*Fs} I: {T’1s = T’2s} J: {T’s = T’i} K: {Fs = id.val} L: {Fs = Es} Joey Paquet,

5 Attribute migration id1+id2*id3$ E T E' + T E'  F T' F T'  id id * F
COMP 442/6421 – Compiler Design Attribute migration id1+id2*id3$ E B:{Es = E's} A:{E'i = Ts} T E' D:{E's = E's} G:{Ts = T's} C:{E'i = E'i+Ts} + T E' F T' G:{Ts = T's} E:{E's = E'i} F:{T'i = Fs} F T' J:{T's = T'i} F:{T'i = Fs} K:{Fs = id.val} K:{Fs = id.val} I:{T's = T's} id id * F T' H:{T'i = T'i * Fs} K:{Fs = id.val} J:{T's = T'i} id Joey Paquet,

6 Attribute migration id1*id2+id3$ E T E' + T E'  F T' id * F T' F T' 
COMP 442/6421 – Compiler Design Attribute migration id1*id2+id3$ E B:{Es = E's} A:{E'i = Ts} T E' D:{E's = E's} G:{Ts = T's} C:{E'i = E'i+Ts} + T E' F T' E:{E's = E'i} F:{T'i = Fs} G:{Ts = T's} K:{Fs = id.val} I:{T's = T's} id * F T' F F:{T'i = Fs} T' H:{T'i = T'i * Fs} J:{T's = T'i} J:{T's = T'i} K:{Fs = id.val} K:{Fs = id.val} id id Joey Paquet,

7 Parsing example using semantic stack for attribute migration
COMP 442/6421 – Compiler Design Parsing example using semantic stack for attribute migration parsing stack input action semantic stack 1 $E id1*id2+id3$ R1 2 $BE'AT R4 3 $BE'AGT'FF R7 4 $BE'AGT'FKid 5 $BE'AGT'FK *id2+id3$ K F1s[id1.val] 6 $BE'AGT'F F T'1i[id1.val] 7 $BE'AGT' R5 8 $BE'AGIT'HF* 9 $BE'AGIT'HF id2+id3$ 10 $BE'AGIT'HKid 11 $BE'AGIT'HK +id3$ T'1i[id1.val] F2s[id2.val] 12 $BE'AGIT'H H T'2i[id1.val * id2.val] 13 $BE'AGIT' R6 14 $BE'AGIJ J T'2s[id1.val * id2.val] 15 $BE'AGI I T'1s[id1.val * id2.val] 16 $BE'AG G T1s[id1.val * id2.val] 17 $BE'A A E'1i[id1.val * id2.val] Joey Paquet,

8 Parsing example using semantic stack for attribute migration
COMP 442/6421 – Compiler Design Parsing example using semantic stack for attribute migration parsing stack input action semantic stack 18 $BE' +id3$ R2 E'1i[id1.val * id2.val] 19 $BDE'CT+ 20 $BDE'CT id3$ R4 21 $BDE'CGT'FF R7 22 $BDE'CGT'FKid 23 $BDE'CGT'FK $ K E'1i[id1.val * id2.val] F3s[id3.val] 24 $BDE'CGT'F F E'1i[id1.val * id2.val] T'3i[id3.val] 25 $BDE'CGT' R6 26 $BDE'CGJ J E'1i[id1.val * id2.val] T'3s[id3.val] 27 $BDE'CG G E'1i[id1.val * id2.val] T2s[id3.val] 28 $BDE'C C E'2i[id1.val * id2.val + id3.val] 29 $BDE' R3 30 $BDE E E'2s[id1.val * id2.val + id3.val] 31 $BD D E'1s[id1.val * id2.val + id3.val] 32 $B B E1s[id1.val * id2.val + id3.val] 33 accept Joey Paquet,

9 Top-down syntax-directed translation grammar transformation
COMP 442/6421 – Compiler Design Top-down syntax-directed translation grammar transformation Joey Paquet,

10 Top-down syntax-directed translation
COMP 442/6421 – Compiler Design Top-down syntax-directed 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? E T1 T2 T’2 * 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,

11 With left recursions a+b*c E E + T T T * F F F id (vc : ) id (va : )
COMP 442/6421 – Compiler Design With left recursions a+b*c 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)} {Fs = lookup(a)} F F {Fs = lookup(b)} id (vc : ) id (va : ) id (vb : ) Joey Paquet,

12 Without left recursions
COMP 442/6421 – Compiler Design Without left recursions E  T{E’i=Ts}E’{Es=E’s} E’  +T{E’i=E’i+Ts}E’{E’s=E’s} E’  {E’s=E’i} T  F{T’i=Fs}T’{Ts=T’s} T’  F{T’i=T’i*Fs}T’{T’s=T’s} T’  {T’s=T’i} F  id{Fs=lookup(id)} a+b*c E {Es = E’s} T E’ {E’i = Ts} {E’s = E’s} {Ts = T’s} + F T’ T E’ {T’i = Fs} {E’i = E’i+Ts} {T’s = T’i} {E’s = E’i} {Fs = lookup(a)} {Ts = T’s} id (va : ) F T’ {T’i = Fs} {T’s = T’s} {Fs = lookup(b)} * id (vb : ) F T’ {T’i = Fs} {T’s = T’i} {Fs = lookup(c)} id (vc : ) Joey Paquet,

13 Top-down syntax-directed translation: attribute grammar transformation
COMP 442/6421 – Compiler Design Top-down syntax-directed translation: attribute grammar transformation Solution: The grammar is transformed as we saw before to eliminate left recursions in a context-free grammar. But when we introduce attributes, the transformation spreads some attributes over multiple rules, thus introducing the need for 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,

14 Top-down syntax-directed translation: attribute grammar transformation
COMP 442/6421 – Compiler Design Top-down syntax-directed translation: attribute grammar transformation A1  A2 A1  Q1 A3   Q2  Q3 Q4   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  T2 {E3s=T2s} E2’  +T1{E3’i=E2’i+T1s}E3’{E2’s=E3’s} E4’  {E4’s=E4’i} where: A1,2,3  E1,2,3 Q1,2,3,4  E’1,2,3,4   Ts   Ts f(As, )  +(Es,Ts) g()  Ts Joey Paquet,

15 Bottom-up syntax-directed translation
COMP 442/6421 – Compiler Design Bottom-up syntax-directed translation Building an abstract syntax tree using syntax-directed translation Joey Paquet,

16 Bottom-up syntax-directed translation
COMP 442/6421 – Compiler Design Bottom-up syntax-directed translation 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, so there is no need for an additional semantic stack. Semantic rules are triggered as handles are popped from the stack Joey Paquet,

17 COMP 442/6421 – Compiler Design
Bottom-up syntax-directed translation: building an abstract syntax 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,

18 COMP 442/6421 – Compiler Design
Bottom-up syntax-directed translation: building an abstract syntax 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,

19 COMP 442/6421 – Compiler Design
Bottom-up syntax-directed translation: building an abstract syntax tree 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,

20 COMP 442/6421 – Compiler Design
Bottom-up syntax-directed translation: building an abstract syntax tree parsing stack input action 1 $ id = (id+id)*id$ 2 $id = (id+id)*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 Joey Paquet,

21 COMP 442/6421 – Compiler Design
Bottom-up syntax-directed translation: building an abstract syntax tree 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,

22 Bottom-up syntax-directed translation
COMP 442/6421 – Compiler Design Bottom-up syntax-directed translation 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. A similar technique can also be adopted by top-down parsing to build syntax trees as an intermediate representation. Joey Paquet,

23 COMP 442/6421 – Compiler Design
References Fischer, Cytron, Leblanc. Crafting a Compiler, Chapter 7. Addison-Wesley Robert Paul Corbett. Static Semantics and Compiler Error Recovery. PhD thesis, University of California Berkeley Joey Paquet,


Download ppt "Compiler design Table-driven syntax-directed translation"

Similar presentations


Ads by Google