COMP 4426421 Compiler Design 1 Click to edit























- Slides: 23
COMP 442/6421 – Compiler Design 1 Click to edit Master title style COMPILER DESIGN Table-driven syntax-directed translation Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 2 Click to edit Master title style Top-down table-driven syntax-directed translation Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 3 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 4 Top-down table-driven syntax-directed translation id Attribute Grammar r 1: E TAE’B E r 2: E’ 1 +TCE’ 2 D E’ r 3: E’ E T r 4: T FFT’G T’ r 5: T’ 1 *FHT’ 2 I F ( ) + * Semantic Actions $ A: {E’i = Ts} B: {Es = E’s} C: {E’ 2 i = E’ 1 i+Ts} D: {E’ 1 s = E’ 2 s} E: {E’s = E’i} r 6: T’ J F: {T’i = Fs} r 7: F id. K G: {Ts = T’s} r 8: F (E)L H: {T’ 2 i = T’ 1 i*Fs} I: {T’ 1 s = T’ 2 s} J: {T’s = T’i} K: {Fs = id. val} L: {Fs = Es} Concordia University r 1 r 3 r 2 r 3 r 4 r 6 r 5 r 6 r 7 r 8 Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 5 Attribute migration id 1+id 2*id 3$ E B: {Es = E's} A: {E'i = Ts} T G: {Ts = T's} F F: {T'i = Fs} T + D: {E's = E's} C: {E'i = E'i+Ts} J: {T's = T'i} E' G: {Ts = T's} T' K: {Fs = id. val} id E' F T' F: {T'i = Fs} K: {Fs = id. val} id * E: {E's = E'i} I: {T's = T's} F H: {T'i = T'i * Fs} T' J: {T's = T'i} K: {Fs = id. val} id Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 6 Attribute migration id 1*id 2+id 3$ E B: {Es = E's} A: {E'i = Ts} T E' G: {Ts = T's} + F E: {E's = E'i} I: {T's = T's} F H: {T'i = T'i * Fs} T' id F: {T'i = Fs} F J: {T's = T'i} K: {Fs = id. val} Concordia University E' G: {Ts = T's} K: {Fs = id. val} * C: {E'i = E'i+Ts} T' F: {T'i = Fs} id T D: {E's = E's} T' J: {T's = T'i} K: {Fs = id. val} id Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 7 Parsing example using semantic stack for attribute migration parsing stack input action 1 $E id 1*id 2+id 3$ R 1 2 $BE'AT id 1*id 2+id 3$ R 4 3 $BE'AGT'FF id 1*id 2+id 3$ R 7 4 $BE'AGT'FKid id 1*id 2+id 3$ 5 $BE'AGT'FK *id 2+id 3$ K F 1 s[id 1. val] 6 $BE'AGT'F *id 2+id 3$ F T'1 i[id 1. val] 7 $BE'AGT' *id 2+id 3$ R 5 T'1 i[id 1. val] 8 $BE'AGIT'HF* *id 2+id 3$ 9 $BE'AGIT'HF id 2+id 3$ 10 $BE'AGIT'HKid id 2+id 3$ 11 $BE'AGIT'HK +id 3$ K T'1 i[id 1. val] F 2 s[id 2. val] 12 $BE'AGIT'H +id 3$ H T'2 i[id 1. val * id 2. val] 13 $BE'AGIT' +id 3$ R 6 T'2 i[id 1. val * id 2. val] 14 $BE'AGIJ +id 3$ J T'2 s[id 1. val * id 2. val] 15 $BE'AGI +id 3$ I T'1 s[id 1. val * id 2. val] 16 $BE'AG +id 3$ G T 1 s[id 1. val * id 2. val] 17 $BE'A +id 3$ A E'1 i[id 1. val * id 2. val] Concordia University semantic stack T'1 i[id 1. val] R 7 T'1 i[id 1. val] Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 8 Parsing example using semantic stack for attribute migration parsing stack input action semantic stack R 2 E'1 i[id 1. val * id 2. val] 18 $BE' +id 3$ 19 $BDE'CT+ +id 3$ 20 $BDE'CT id 3$ R 4 E'1 i[id 1. val * id 2. val] 21 $BDE'CGT'FF id 3$ R 7 E'1 i[id 1. val * id 2. val] 22 $BDE'CGT'FKid id 3$ 23 $BDE'CGT'FK $ K E'1 i[id 1. val * id 2. val] F 3 s[id 3. val] 24 $BDE'CGT'F $ F E'1 i[id 1. val * id 2. val] T'3 i[id 3. val] 25 $BDE'CGT' $ R 6 E'1 i[id 1. val * id 2. val] T'3 i[id 3. val] 26 $BDE'CGJ $ J E'1 i[id 1. val * id 2. val] T'3 s[id 3. val] 27 $BDE'CG $ G E'1 i[id 1. val * id 2. val] T 2 s[id 3. val] 28 $BDE'C $ C E'2 i[id 1. val * id 2. val + id 3. val] 29 $BDE' $ R 3 E'2 i[id 1. val * id 2. val + id 3. val] 30 $BDE $ E E'2 s[id 1. val * id 2. val + id 3. val] 31 $BD $ D E'1 s[id 1. val * id 2. val + id 3. val] 32 $B $ B E 1 s[id 1. val * id 2. val + id 3. val] 33 $ $ accept E 1 s[id 1. val * id 2. val + id 3. val] Concordia University E'1 i[id 1. val * id 2. val] Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 9 Click to edit Master title style Top-down syntax-directed translation grammar transformation Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 10 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? a+b*c E E E’ T T’ F TE’ +TE’ | FT’ | id T 1 E’ 1 F 1 T’ 1 id (va : ) + T 2 E’ 2 F 2 id (vb : ) T’ 2 * F 3 T’ 3 id (vc : ) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 11 With left recursions E E T T F a+b*c E {Es = Ts} {Ts = Fs} T {Fs = lookup(a)} F id (va : ) Concordia University E = Es + Ts} + {Es = Ts} F {Fs T {Ts T = lookup(b)} E+T T T*F F id {Es {Ts {Fs = = = Es+Ts} Ts*Fs} lookup(id)} = Ts * Fs} F {Fs * = lookup(c)} id (vc : ) id (vb : ) Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 12 Without left recursions a+b*c E E’ E’ T T’ T’ F E {Es = E’s} T T{E’i=Ts}E’{Es=E’s} +T{E’i=E’i+Ts}E’{E’s=E’s} {E’s=E’i} F{T’i=Fs}T’{Ts=T’s} F{T’i=T’i*Fs}T’{T’s=T’s} {T’s=T’i} id{Fs=lookup(id)} E’ {E’i = Ts} {E’s = E’s} {Ts = T’s} + F {T’i = Fs} T’ T E’ {E’i = E’i+Ts} {T’s = T’i} {Fs = lookup(a)} {E’s = E’i} {Ts = T’s} id (va : ) F T’ {T’i = Fs} {T’s = T’s} {Fs = lookup(b)} id (vb : ) * F {T’i = Fs} {Fs = lookup(c)} id (vc : ) Concordia University Department of Computer Science and Software Engineering T’ {T’s = T’i} Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 13 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. A 1 A 2 A 3 A 1 Q 1 Q 2 Q 3 Q 4 • 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 s=f(A 2 s, )} A 3 {A 3 s=g( )} Concordia University A 1 {Q 1 i=g( )}Q 1{A 1 s=Q 1 s} 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} Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 14 Top-down syntax-directed translation: attribute grammar transformation A 1 A 2 A 3 A 1 Q 1 Q 2 Q 3 Q 4 E 1 E 2+T 1 E 3 T 2 E 1 T 2 E 1’ E 2’ +T 1 E 3’ E 4’ A 1 A 2 {A 1 s=f(A 2 s, )} A 3 {A 3 s=g( )} Q 4 {Q 4 s=Q 4 i} A 1 {Q 1 i=g( )}Q 1{A 1 s=Q 1 s} Q 2 {Q 3 i=f(Q 2 i, )}Q 3{Q 2 s=Q 3 s} E 1 E 2+T 1 {E 1 s=E 2 s+T 1 s} E 3 T 2 {E 3 s=T 2 s} E 4’ {E 4’s=E 4’i} E 1 T 2{E 1’i=T 2 s}E 1’{E 1 s=E 1’s} E 2’ +T 1{E 3’i=E 2’i+T 1 s}E 3’{E 2’s=E 3’s} where: A 1, 2, 3 Q 1, 2, 3, 4 f(As, ) g( ) Concordia University E 1, 2, 3 E’ 1, 2, 3, 4 Ts Ts +(Es, Ts) Ts Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 15 Click to edit Master title style Bottom-up syntax-directed translation Building an abstract syntax tree using syntax-directed translation Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 16 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 leftrecursions 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 Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 17 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: node. Kind = (internal, leaf) tree. Node = record token : token. Type case kind : node. Kind of internal : (left, right : *tree. Node) leaf : (location : integer) end Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 18 Bottom-up syntax-directed translation: building an abstract syntax tree • We need a make. Tree function that will be used to create subtrees and a make. Leaf function that will be used to create tree leaves: node. Ptr make. Tree(op token. Type, right. Son, left. Son root. Ptr = new(node. Ptr) root. Ptr. kind = internal root. Ptr. token = op root. Ptr. left = left. Son root. Ptr. right = right. Son return root. Ptr } node. Ptr){ node. Ptr make. Leaf(tok token. Type, location integer){ leaf. Ptr = new(node. Ptr) leaf. Ptr. kind = leaf. Ptr. token = tok leaf. Ptr. location = location return leaf. Ptr } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 19 Bottom-up syntax-directed translation: building an abstract syntax tree r 1: S id = EA A: {S. root = make. Tree(‘=‘, id. location, E. root)} r 2: E 1 E 2 + E 3 B B: {E 1. root = make. Tree(‘+’, E 2. root, E 3. root)} r 3: E 1 E 2 * E 3 C C: {E 1. root = make. Tree(‘*’, E 2. root, E 3. root)} r 4: E 1 (E 2)D D: {E 1. root = E 2. root} r 5: E id. E E: {E 1. root = make. Leaf(id, id. location)} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 20 Bottom-up syntax-directed translation: building an abstract syntax tree Concordia University 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$ 6 $id=(E 1 +id)*id$ 7 $id=(E 1 + 8 $id=(E 1 + id )*id$ 9 $id=(E 1 + E 2 )*id$ 10 $id=(E 1 + E 2) *id$ r 2 B 11 $id=(E 3) *id$ r 4 D 12 $id=E 4 *id$ 13 $id=E 4* 14 $id=E 4* id $ r 5 E 15 $id=E 4* E 5 $ r 3 C 16 $id=E 6 $ r 1 A 17 $S $ accept = (id+id)*id$ r 5 E id$ Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 21 Bottom-up syntax-directed translation: building an abstract syntax tree E 1 id 1 E 5 5 E E 2 id 2 14 8 E 1 E E 3 + + E 2 * id 3 E E 4 B E 5 id 1 = id 3 + D 16 S id 4 * id 2 id 1 15 E 6 11 E 4 + id 2 id 1 E 6 10 * id 3 + id 2 C id 1 id 2 A Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 22 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017
COMP 442/6421 – Compiler Design 23 References • Fischer, Cytron, Leblanc. Crafting a Compiler, Chapter 7. Addison-Wesley. 2010. • Robert Paul Corbett. Static Semantics and Compiler Error Recovery. Ph. D thesis, University of California Berkeley. 1985. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2017























