Download presentation
Presentation is loading. Please wait.
Published byMatthew Ralf Blair Modified over 9 years ago
1
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Table-driven syntax-directed translation Joey Paquet, 2000-2015 1COMP 442/6421 – Compiler Design
2
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Top-down table-driven syntax-directed translation Joey Paquet, 2000-2015 2COMP 442/6421 – Compiler Design
3
Concordia University Department of Computer Science and Software Engineering 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. Top-down table-driven syntax-directed translation Joey Paquet, 2000-2015 3COMP 442/6421 – Compiler Design
4
Concordia University Department of Computer Science and Software Engineering Top-down table-driven syntax-directed translation Joey Paquet, 2000-2015 4COMP 442/6421 – Compiler Design Attribute Grammar r1: E TAE’B r2: E’ 1 +TCE’ 2 D r3: E’ E r4: T FFT’G r5: T’ 1 *FHT’ 2 I r6: T’ J r7: F idK r8: F (E)L Semantic Actions A:A:{E’i = Ts} B:B:{Es = E’s} C:C:{E’ 2 i = E’ 1 i+Ts} D:D:{E’ 1 s = E’ 2 s} E:E:{E’s = E’i} F:F:{T’i = Fs} G:G:{Ts = T’s} H:H:{T’ 2 i = T’ 1 i*Fs} I:I:{T’ 1 s = T’ 2 s} J:J:{T’s = T’i} K:K:{Fs = id.val} L:L:{Fs = Es} id()+*$ Er1 E’r3r2r3 Tr4 T’r6 r5r6 Fr7r8
5
Concordia University Department of Computer Science and Software Engineering Attribute migration Joey Paquet, 2000-2015 5COMP 442/6421 – Compiler Design id1+id2*id3$ E T T T' * F id T' F id E' T' F id E' + K:{Fs = id.val} F:{T'i = Fs} J:{T's = T'i} G:{Ts = T's} A:{E'i = Ts} B:{Es = E's} D:{E's = E's} C:{E'i = E'i+Ts} E:{E's = E'i} I:{T's = T's} J:{T's = T'i} K:{Fs = id.val} H:{T'i = T'i * Fs} F:{T'i = Fs} K:{Fs = id.val} G:{Ts = T's}
6
Concordia University Department of Computer Science and Software Engineering Attribute migration Joey Paquet, 2000-2015 6COMP 442/6421 – Compiler Design id1*id2+id3$ E T T T' * F id F E' T' F id E' + K:{Fs = id.val} F:{T'i = Fs} G:{Ts = T's} A:{E'i = Ts} B:{Es = E's} D:{E's = E's} C:{E'i = E'i+Ts} E:{E's = E'i} I:{T's = T's} J:{T's = T'i} K:{Fs = id.val} H:{T'i = T'i * Fs} F:{T'i = Fs} K:{Fs = id.val} G:{Ts = T's} T' J:{T's = T'i}
7
Concordia University Department of Computer Science and Software Engineering Parsing example using semantic stack for attribute migration Joey Paquet, 2000-2015 7COMP 442/6421 – Compiler Design parsing stackinputactionsemantic stack 1$Eid 1 *id 2 +id 3 $R1 2$BE'ATid 1 *id 2 +id 3 $R4 3$BE'AGT'FFid 1 *id 2 +id 3 $R7 4$BE'AGT'FKidid 1 *id 2 +id 3 $ 5$BE'AGT'FK*id 2 +id 3 $KF 1 s[id 1.val] 6$BE'AGT'F*id 2 +id 3 $FT' 1 i[id 1.val] 7$BE'AGT'*id 2 +id 3 $R5T' 1 i[id 1.val] 8$BE'AGIT'HF**id 2 +id 3 $T' 1 i[id 1.val] 9$BE'AGIT'HFid 2 +id 3 $R7T' 1 i[id 1.val] 10$BE'AGIT'HKidid 2 +id 3 $T' 1 i[id 1.val] 11$BE'AGIT'HK+id 3 $KT' 1 i[id 1.val] F 2 s[id 2.val] 12$BE'AGIT'H+id 3 $HT' 2 i[id 1.val * id 2.val] 13$BE'AGIT'+id 3 $R6T' 2 i[id 1.val * id 2.val] 14$BE'AGIJ+id 3 $JT' 2 s[id 1.val * id 2.val] 15$BE'AGI+id 3 $IT' 1 s[id 1.val * id 2.val] 16$BE'AG+id 3 $GT 1 s[id 1.val * id 2.val] 17$BE'A+id 3 $AE' 1 i[id 1.val * id 2.val]
8
Concordia University Department of Computer Science and Software Engineering Parsing example using semantic stack for attribute migration Joey Paquet, 2000-2015 8COMP 442/6421 – Compiler Design parsing stackinputactionsemantic stack 18$BE'+id 3 $R2E' 1 i[id 1.val * id 2.val] 19$BDE'CT++id 3 $E' 1 i[id 1.val * id 2.val] 20$BDE'CTid 3 $R4E' 1 i[id 1.val * id 2.val] 21$BDE'CGT'FFid 3 $R7E' 1 i[id 1.val * id 2.val] 22$BDE'CGT'FKidid 3 $E' 1 i[id 1.val * id 2.val] 23$BDE'CGT'FK$KE' 1 i[id 1.val * id 2.val] F 3 s[id 3.val] 24$BDE'CGT'F$FE' 1 i[id 1.val * id 2.val] T' 3 i[id 3.val] 25$BDE'CGT'$R6E' 1 i[id 1.val * id 2.val] T' 3 i[id 3.val] 26$BDE'CGJ$JE' 1 i[id 1.val * id 2.val] T' 3 s[id 3.val] 27$BDE'CG$GE' 1 i[id 1.val * id 2.val] T 2 s[id 3.val] 28$BDE'C$CE' 2 i[id 1.val * id 2.val + id 3.val] 29$BDE'$R3E' 2 i[id 1.val * id 2.val + id 3.val] 30$BDE$EE' 2 s[id 1.val * id 2.val + id 3.val] 31$BD$DE' 1 s[id 1.val * id 2.val + id 3.val] 32$B$B$BE 1 s[id 1.val * id 2.val + id 3.val] 33$$acceptE 1 s[id 1.val * id 2.val + id 3.val]
9
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Top-down syntax-directed translation grammar transformation Joey Paquet, 2000-2015 9COMP 442/6421 – Compiler Design
10
Concordia University Department of Computer Science and Software Engineering 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? Top-down syntax-directed translation Joey Paquet, 2000-2015 10COMP 442/6421 – Compiler Design E T1T1 T2T2 T’ 2 * F1F1 id (v a : ) T’ 1 F2F2 id (v b : ) E’ 2 T’ 3 F3F3 id (v c : ) E’ 1 + a+b*c E TE’ E’ +TE’ | T FT’ T’ FT’ | F id
11
Concordia University Department of Computer Science and Software Engineering With left recursions Joey Paquet, 2000-2015 11COMP 442/6421 – Compiler Design E E F id (v a : ) T F id (v b : ) + {Fs = lookup(a)} {Ts = Fs} {Es = Ts} T T {Fs = lookup(b)} * F id (v c : ) {Fs = lookup(c)} {Ts = Ts * Fs} {Es = Es + Ts} 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
12
Concordia University Department of Computer Science and Software Engineering Without left recursions Joey Paquet, 2000-2015 12COMP 442/6421 – Compiler Design E T T T’ * F id (v a : ) T’ F id (v b : ) E’ T’ F id (v c : ) E’ + 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=T’i} F id{Fs=lookup(id)} {Fs = lookup(a)} {Fs = lookup(b)} {Fs = lookup(c)} {E’i = Ts} {T’i = Fs} {T’s = Ti} {Ts = T’s} {T’i = Fs} {T’s = T’i} {T’i = Fs} {T’s = Ti*T’s} {Ts = T’s} {E’i = Ts} {E’s = E’i} {E’s = E’i+E’s} {Es = E’s} a+b*c
13
Concordia University Department of Computer Science and Software Engineering Top-down syntax-directed translation: attribute grammar transformation Joey Paquet, 2000-2015 13COMP 442/6421 – Compiler Design 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: A 1 A 2 A 1 Q 1 A 3 Q 2 Q 3 Q 4 A 1 A 2 {A 1 s=f(A 2 s, )} A 1 {Q 1 i=g()}Q 1 {A 1 s=Q 1 s} A 3 {A 3 s=g()}Q 2 {Q 3 i=f(Q 2 i, )}Q 3 {Q 2 s=Q 3 s} Q 4 {Q 4 s=Q 4 i}
14
Concordia University Department of Computer Science and Software Engineering Top-down syntax-directed translation: attribute grammar transformation Joey Paquet, 2000-2015 14COMP 442/6421 – Compiler Design E 1 E 2 +T 1 {E 1 s=E 2 s+T 1 s} E 1 T 2 {E 1 ’i=T 2 s}E 1 ’{E 1 s=E 1 ’s} E 3 T 2 {E 3 s=T 2 s}E 2 ’ +T 1 {E 3 ’i=E 2 ’i+T 1 s}E 3 ’{E 2 ’s=E 3 ’s} E 4 ’ {E 4 ’s=E 4 ’i} E 1 E 2 +T 1 E 1 T 2 E 1 ’ E 3 T 2 E 2 ’ +T 1 E 3 ’ E 4 ’ A 1 A 2 A 1 Q 1 A 3 Q 2 Q 3 Q 4 A 1 A 2 {A 1 s=f(A 2 s,)} A 1 {Q 1 i=g()}Q 1 {A 1 s=Q 1 s} A 3 {A 3 s=g()}Q 2 {Q 3 i=f(Q 2 i,)}Q 3 {Q 2 s=Q 3 s} Q 4 {Q 4 s=Q 4 i} where: A 1,2,3 E 1,2,3 Q 1,2,3,4 E’ 1,2,3,4 +T 1 T 2 f(A 2 s, ) E 2 s+T 1 s g() T 2 s
15
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Bottom-up syntax-directed translation Building an abstract syntax tree using syntax-directed translation Joey Paquet, 2000-2015 15COMP 442/6421 – Compiler Design
16
Concordia University Department of Computer Science and Software Engineering 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 Bottom-up syntax-directed translation Joey Paquet, 2000-2015 16COMP 442/6421 – Compiler Design
17
Concordia University Department of Computer Science and Software Engineering 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: Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, 2000-2015 17COMP 442/6421 – Compiler Design nodeKind = (internal,leaf) treeNode = record token : tokenType case kind : nodeKind of internal : (left,right : *treeNode) leaf : (location : integer) end
18
Concordia University Department of Computer Science and Software Engineering We need a makeTree function that will be used to create subtrees and a makeLeaf function that will be used to create tree leaves: Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, 2000-2015 18COMP 442/6421 – Compiler Design 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 }
19
Concordia University Department of Computer Science and Software Engineering Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, 2000-2015 19COMP 442/6421 – Compiler Design r1: S id = EA r2: E 1 E 2 + E 3 B r3: E 1 E 2 * E 3 C r4: E 1 (E 2 )D r5: E idE A:A:{S.root = makeTree(‘=‘, id.location, E.root)} B:B:{E 1.root = makeTree(‘+’, E 2.root, E 3.root)} C:C:{E 1.root = makeTree(‘*’, E 2.root, E 3.root)} D:D:{E 1.root = E 2.root} E:E:{E 1.root = makeLeaf(id, id.location)}
20
Concordia University Department of Computer Science and Software Engineering Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, 2000-2015 20COMP 442/6421 – Compiler Design parsing stackinputaction 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=(E 1 +id)*id$ 7$id=(E 1 +id)*id$ 8$id=(E 1 + id)*id$r5 E 9$id=(E 1 + E 2 )*id$ 10$id=(E 1 + E 2 )*id$r2 B 11$id=(E 3 )*id$r4 D 12$id=E 4 *id$ 13$id=E 4 *id$ 14$id=E 4 * id$r5 E 15$id=E 4 * E 5 $r3 C 16$id=E 6 $r1 A 17$S$S$accept
21
Concordia University Department of Computer Science and Software Engineering Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, 2000-2015 21COMP 442/6421 – Compiler Design E1E1 id 1 E2E2 id 2 E3E3 E1E1 E2E2 + E3E3 + id 1 id 2 E4E4 + id 1 id 2 E5E5 id 3 E6E6 E4E4 E5E5 * + id 1 id 2 E6E6 * id 3 + id 1 id 2 * id 3 = S id 4 A B C DE 581011 141516 E E
22
Concordia University Department of Computer Science and Software Engineering 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. Bottom-up syntax-directed translation Joey Paquet, 2000-2015 22COMP 442/6421 – Compiler Design
23
Concordia University Department of Computer Science and Software Engineering Fischer, Cytron, Leblanc. Crafting a Compiler, Chapter 7. Addison- Wesley. 2010. References Joey Paquet, 2000-2015 23COMP 442/6421 – Compiler Design
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.