Assignment 2 olutions Compiler Design I Kompiltorteknik I) 2011 1 Context-free grmmrs Give the definition of context free grmmr over the lphbet Σ = {, b} tht describes ll strings tht hve different number of s nd b s U V U T U T T V T bv T bt T T bt bt T ϵ The intuition is tht the string will hve either more s non-terminl U) or more b s non-terminl V ) Non-terminl T produces string with blnced s nd b s 2 Prsing nd semntic ctions The following context-free grmmr cn prse ll the lowercse romn numerls from 1-99 The terminl symbols re { c, l, x, v, i } nd the initil symbol is If you re unfmilir with romn numerls, plese hve look t http://enwikipediorg/wiki/romn_numerls 1 Drw prse tree for 42: xlii xtu lx X T c l X xx U U iy vi I Y x v I ii ϵ x T l U i I i I I ϵ 1
2 Is this grmmr mbiguous? No 3 Write semntic ctions for ech of the 14 rules in the grmmr remember X A B is short for X A nd X B) to clculte the deciml vlue of the input string You cn ssocite synthesized ttribute vl to ech of the non-terminls to store their vlue The finl vlue should be returned in vl Given cvl = 100, lvl = 50, xvl = 10, vvl = 5, ivl = 1 nd ϵvl = 0: 3 LL1) Prsers xt U {vl = Tvl xvl + Uvl} lx {vl = lvl + Xvl} X {vl = Xvl} T c {Tvl = cvl} T l {Tvl = lvl} X 1 xx 2 {X 1 vl = xvl + X 2 vl} X U {Xvl = Uvl} U iy {Uvl = Yvl ivl} U vi {Uvl = vvl + Ivl} U I {Uvl = Ivl} Y x {Yvl = xvl} Y v {Yvl = vvl} I 1 ii 2 {I 1 vl = ivl + I 2 vl} I ϵ {Ivl = ϵvl} In the following context-free grmmr, the symbols 0, 1, 2 nd 3 re terminls nd is the initil symbol 0 1 2 3 1 A 3 A A 1 Explin briefly why this grmmr is not LL1) This grmmr cnnot be prsed by recursive descent prser This cn be shown by the following two exmples: If the prser hs to expnd n non-terminl nd the next token is 1, it is not possible to choose between the 2 productions from tht strt with 1 with just this informtion However LL1) lnguges llow for just one look-hed symbol If the prser were to mke use of the A A production, for some look-hed symbol, then in the new stte it would still hve to expnd the new A with the sme look-hed, leding to n infinite loop 2 Convert this grmmr to n equivlent tht is LL1) Fctorize the productions nd eliminte immedite left recursion from the A productions: 0 1 2 3 A 3 A A A A ϵ 2
Inline singulr A production rule to uncover nother possible fctoriztion: 0 1 2 3 A 3 A A ϵ Fctorize the production nd inline the new singulr it in s production: 0 1 2 3 A 3 A A ϵ 3 For the grmmr of the previous subtsk, construct the complete LL1) prsing tble First) = {0, 1} Follow) = {0, 1, 2, 3, $} First ) = {0, 1, 2, 3} Follow ) = {0, 1, 2, 3, $} FirstA ) = {0, 1, ϵ} FollowA ) = {3} 0 1 2 3 $ 0 1 A 3 A 3 2 3 A 3 A A A A A A ϵ 4 how ll the steps required to prse the input string: 1 1 0 2 0 3 0 1 0 3 3 tck Input Action $ 1 1 0 2 0 3 0 1 0 3 3 $ 1 1 $ 1 1 0 2 0 3 0 1 0 3 3 $ terminl $ 1 0 2 0 3 0 1 0 3 3 $ 1 1 $ 1 0 2 0 3 0 1 0 3 3 $ terminl $ 0 2 0 3 0 1 0 3 3 $ 0 0 $ 0 2 0 3 0 1 0 3 3 $ terminl $ 2 0 3 0 1 0 3 3 $ 2 3 2 3 $ 2 0 3 0 1 0 3 3 $ terminl 3 $ 0 3 0 1 0 3 3 $ 0 0 3 $ 0 3 0 1 0 3 3 $ terminl 3 $ 3 0 1 0 3 3 $ terminl $ 0 1 0 3 3 $ A 3 A 3 $ 0 1 0 3 3 $ A A A 3 $ 0 1 0 3 3 $ 0 0 A 3 $ 0 1 0 3 3 $ terminl A 3 $ 1 0 3 3 $ A A A 3 $ 1 0 3 3 $ 1 1 A 3 $ 1 0 3 3 $ terminl A 3 $ 0 3 3 $ 0 0 A 3 $ 0 3 3 $ terminl A 3 $ 3 3 $ A 3 A 3 A 3 $ 3 3 $ A ϵ 3 A 3 $ 3 3 $ terminl A 3 $ 3 $ A ϵ 3 $ 3 $ terminl $ $ ACCEPT 3
4 LR1) Prsers In the following context-free grmmr, the symbols,, ) nd, re terminls nd is the initil symbol 1) L ) 2) 3) L L, 4) L Becuse, is symbol of the lnguge we re going to use s seprtor between the core of the LR1) items nd the lookhed symbols Lookheds with the sme core cn be seprted s usul with / 1 Clculte the closure of the LR1) item [ L ) $ ] [ L ) $ ] [ L L, )/, ] [ L )/, ] [ L ) )/, ] [ )/, ] 2 Construct the full LR1) DFA, showing ll items in ech stte New unique initil production: 0) E [ L ) $ ] [ E $ ] [ L L, )/, ] strt 0 [ L ) $ ] 3 [ L )/, ] 5 [ L )/, ] [ $ ] [ L ) )/, ] [ )/, ] 1 [ E $ ] 2 [ $ ] 7 [ L ) $ ] [ L L, )/, ] ) 8 [ L ) $ ], L 11 6 [ )/, ] [ L L, )/, ] [ L ) )/, ] [ )/, ] 12 [ L L, )/, ], 4 [ L ) )/, ] [ L L, )/, ] [ L )/, ] [ L ) )/, ] [ )/, ] L 9 [ L ) )/, ] [ L L, )/, ] ) 10 [ L ) )/, ] 4
3 Construct the LR1) prsing tble using the DFA For the reduce ctions, plese use the provided enumertion of the productions in the grmmr tte ), $ L 0 s3 s2 1 1 ACCEPT 2 r2 3 s4 s6 5 7 4 s4 s6 5 9 5 r4 r4 6 r2 r2 7 s8 s11 8 r1 9 s10 s11 10 r1 r1 11 s4 s6 12 12 r3 r3 4 how ll the steps required to prse the input string:, ),, ) tck ymbols Input Action 0, ),, )$ shift 0,3, ),, )$ shift 0,3,4, ),, )$ shift 0,3,4,6, ),, )$ reduce 0,3,4,5, ),, )$ reduce 0,3,4,9 L, ),, )$ shift 0,3,4,9,11 L, ),, )$ shift 0,3,4,9,11,6 L, ),, )$ reduce 0,3,4,9,11,12 L, ),, )$ reduce 0,3,4,9 L ),, )$ shift 0,3,4,9,10 L ),, )$ reduce 0,3,5,, )$ reduce 0,3,7 L,, )$ shift 0,3,7,11 L,, )$ shift 0,3,7,11,6 L,, )$ reduce 0,3,7,11,12 L,, )$ reduce 0,3,7 L, )$ shift 0,3,7,11 L, )$ shift 0,3,7,11,6 L, )$ reduce 0,3,7,11,12 L, )$ reduce 0,3,7 L )$ shift 0,3,7,8 L ) $ reduce 0,1 $ ACCEPT! 5