CSCI565 - Compiler Deign Spring 6 Due Dte: Fe. 5, 6 t : PM in Cl Prolem [ point]: Regulr Expreion nd Finite Automt Develop regulr expreion (RE) tht detet the longet tring over the lphet {-} with the following propertie:. The tring egin with n hrter nd end with hrter;. After the firt, the tring n inlude n lterntion of two uequene, nmely equene tht egin with hrter followed y ero or more hrter ending with n e hrter nd uequene tht egin with hrter ut doe not hve ny hrter until terminting t hrter.. If t ny point in the uequene there i x hrter, tht peifi equene i onidered terminted, i.e., the x t the e hrter in the firt type of equene nd the t hrter in the eond type of equene. 4. The two uequene nnot e neted ut n e repeted in ny lternting order. A n exmple the tring efghte i to e epted well the tring xfft Quetion: ) [5 point] Develop regulr expreion tht pture the truture of the eptle tring deried ove. Ue hort-hnd to pture uet of hrter in the lphet o tht your deritpion nd the orreponding FA re kept hort. ) [ point] Uing the regulr expreion define in etion ) devie the orreponding Non- Determiniti Finite Automton (NFA) uing the Thompon ontrution deried in l. ) [ point] Convert the NFA in etion ) to DFA uing the uet ontrution. Show the mpping etween the tte in the NFA nd the reulting DFA. d) [5 point] Minimie the DFA derived in etion ) (or how it i lredy miniml) uing the itertive refinement lgorithm deried in l. Solution: ) Among the mny poile olution for regulr expreion the one elow eem to e the mot intuitive. Here we ue the hort-hnd notstx for ll hrter in the lphet exept nd t i.e., notstx = {-r, u, v, y, }. RE =. [ (. ( * ). (x e) ) (. ( notstx * ). (x t) ) ] *. ) The NFA elow reflet the truture of the RE preented ove where we hve lo ompreed ome ε-trnition for revity, in prtiulr the one tht reflet Kleene loure nd lterntion. of 7
5 7 9 5 6 trt 4 /{tx} 6 8 4 ) The uet ontrution of thi NFA i hown elow where gin we hve merged trnition on hrter with nlogou ehvior. trt,,,4 5, 6,8,,,,4,,,5,6,,,4,,4, 5,6 /tx 8, /tx d) The minimition of thi DFA uing the itertive refinement lgorithm i ummried elow. We egin y imple prtitionetween the epting tte nd the non-epting tte. Thi immeditely low u to iolte the epting tte. The eond prtition ued the hrter the dirimintig token howing tht tte,,,4,,,5,6 nd,,,4,,4,5,6 my e equivlent. The lt prtition digrm reflet the ft tht ll tte re dijoint. of 7
of 7 P P P4 P5 P6 P7 trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P P4 P5 P6 P7 trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P8 P trt 5 /tx /tx 8 4 6 P P P4 trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P P trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6
Prolem [ point]: Trnltion from DFA to Regulr Expreion Given the DFA elow over the lphet {,} determine the following:. [5 point] Ue the dynmi-progrmming Kleene lgorithm to derive the regulr expreion tht denote the lnguge epted y it. Mke ure your lelling of the DFA tte i orret nd tht the DFA i ompletely peified, i.e., eh tte h trnition on ll the lphet hrter.. [5 point] Derie uintely wht re the word epted y thi DFA?, trt Solution:.. [5 point] Ue the dynmi-progrmming Kleene lgorithm to derive the regulr expreion tht denote the lnguge epted y it. Mke ure your lelling of the DFA tte i orret nd tht the DFA i ompletely peified, i.e., eh tte h trnition on ll the lphet hrter. Expreion for k = R = (ε) R = () R = () R R = ( ε) R R R = () R = ( ε) Expreion for k = R = R (R )*R R = (ε)(ε) * (ε) (ε) = (ε) R = R (R )*R R = (ε)(ε) * () () = () R = R (R )*R R = (ε)(ε) * () () = () R = R (R )*R R = R R = R (R )*R R = R = ( ε) R = R (R )*R R = R R = R (R )*R R = R 4 of 7
R = R (R )*R R = R = () R = R (R )*R R = R = ( ε) Expreion for k = R = R (R )*R R = (). ( ε) *. Ø (ε) = (ε) R = R (R )*R R = (). ( ε) *. ( ε) () = ( + ) R = R (R )*R R = (). ( ε) *. Ø () = () R = R (R )*R R = ( ε) ( ε)* Ø Ø R = R (R )*R R = ( ε) ( ε)* ( ε) ( ε) = ( * ) R = R (R )*R R = ( ε) ( ε)* Ø Ø R = R (R )*R R = (). ( ε)*. Ø Ø R = R (R )*R R = (). ( ε)*. ( ε) () = (). ()* R = R (R )*R R = (). ( ε)*. Ø ( ε) = ( ε) Expreion for k = R = R (R )*R R = (). ()*. Ø (ε) R = R (R )*R R = (). ()*. (). ()* ( + ) R = R (R )*R R = (). ()*. ( ε) () = (). ()* R = R (R )*R R. ()*. Ø Ø R = R (R )*R R. ()*. (). ()* ( * ) = ( * ) R = R (R )*R R. ()*. ( ε) Ø R = R (R )*R R = ( ε). ()*. Ø Ø R = R (R )*R R = ( ε). ()*. (). ()* (). ()* = ()*. (). ()* R = R (R )*R R = ( ε). ()*. ( ε) ( ε) = ()* The lnguge reognied y thi DFA n e expreed y the regulr expreion R. L = R = (). ( ε)*. ( ε) = (). ( ε)* =.*. [5 point] Derie uintely wht re the word epted y thi DFA? Thi utomton reognie ll inrie tring tht denote integer tht re non-negtive power of two nd uh n e deried y the ompt regulr expreion: *. 5 of 7
Prolem [5 point]: Preditive Top-Down Pring Conider the CFG grmmr G = (N={S, A, B}, T={ }, P, S) where the et of prodution P i given elow: S A A B B A ε B ε Quetion: ) [5 point] Cn thi grmmr e ued preented for pring uing preditive (ktrkfree) lgorithm? Why or why not? ) [ point] Devie n lterntive (ut equivlent) grmmr for the me lnguge tht h the LL() property. ) [5 point] Compute the FIRST nd FOLLOW et for eh prodution RHS nd the nonterminl ymol repetively. Ue thee to how tht the grmmr h in ft the LL() property. d) [ point] Derive the LL() pring tle deried in the leture nd how tht in ft the grmmr i prele uing the LL() pring lgorithm. e) [ point] Show the equene of pring tep nd the orreponding pre tree for thi lgorithm nd the two input w = nd w =. Solution: ) [5 point] Cn thi grmmr e ued preented for pring uing preditive (ktrk-free) lgorithm? Why or why not? While t firt ight thi grmmr doe pper not to uffer from the iue of left-reurion nd ommon prefixe in ft it i not LL(). Thi i eue the prodution for either the non-terminl A nd B llow for the derivtion of the empty tring nd the hrter nd elow to the FOLLOW et of the repetive non-terminl. ) [ point] Devie n lterntive (ut equivlent) grmmr for the me lnguge tht h the LL() property. A wy to void thi iue, nd hene derive n equivlent LL() grmmr will e to eliminte the ε- trnition hown in the revied grmmr elow. Notie tht thi led to n inree in the numer of prodution in the grmmr ll omintion of trnition for the two ourring A nd B nonterminl in the prodution of S need to e explored. S A A A A S B B B B A B Now we hve to del with the iue if left-ftorition in oth et of prodution for S. S A S B A A A A ε A ε B B B B ε B ε 6 of 7
Notie now tht the FOLLOW of ny non-terminl tht derive the empty tring doe not onflit with it FIRST et nd o the grmmr i LL(). ) [5 point] Compute the FIRST nd FOLLOW et for eh prodution RHS nd the non-terminl ymol repetively. Ue thee to how tht the grmmr h in ft the LL() property. FOLLOW(S) = {} FOLLOW(A ) = {} FOLLOW(A ) = {} FOLLOW(A ) = {} FOLLOW(B ) = {} FOLLOW(B ) = {} FOLLOW(B ) = {} FIRST(A ) = {}, FIRST(B ) = {} FIRST + (A ) FIRST + (B ) = FOLLOW(S) = {} FIRST( A ) = {} FIRST( A ) = {}, FIRST(ε) = {ε} eue A derive the empty tring, FIRST + (A A ) = {,} FIRST() = {}, FIRST(ε) = {ε} eue A derive the empty tring, FIRST + (A ) = {,} Similrly, for B prodution we get: FIRST( B ) = {} FIRST( B ) = {}, FIRST(ε) = {ε} eue B derive the empty tring, FIRST + (B B ) = {,} FIRST() = {}, FIRST(ε) = {ε} eue B derive the empty tring, FIRST + (B ) = {,} d) [ point] Derive the LL() pring tle deried in the leture nd how tht in ft the grmmr i prele uing the LL() pring lgorithm. S A S A A A S B A A B B B A A A B B B B B A ε A ε B ε B ε e) [ point] Show the equene of pring tep nd the orreponding pre tree for thi lgorithm nd the two input w = nd w =. Stk Input Prod. S A A A A A S A A A A A A A S A A Stk Input Prod. S B B B S B B B B B S B 7 of 7