241 Week 6 Tutoril olutions Lnguges: nning & ontext-free Grmmrs Winter 2018 1 nning Exerises 1. 0x0x0xd HEXINT 0x0 I x0xd 2. 0xend--- HEXINT 0xe I nd ER -- MINU - 3. 1234-120x INT 1234 INT -120 I x 4. end--en-3 I end ER -- ERROR en- 5. 01end-end10 ZERO 0 INT 1 EN end MINU - I end10 1
2 ontext-free Grmmr Exerises 2.1 erivtions Let G e the following grmmr: 1. how tht the word is in L(G) y giving derivtion. 2. Whih of the following re vlid derivtions of words in L(G)? () () () (d) (e) () is invlid sine there is no rule in G. () is invlid sine there is no rule in G. () is vlid. (d) is invlid sine is not the strt symol of the grmmr. (e) is invlid sine there is no rule. However, it would e vlid if we sid =, sine. 2
2.2 esigning Grmmrs 1. Write ontext-free grmmrs for the following lnguges: () The lnguge L = {my, nme, is, inigo, montoy}. () The lnguge of ll words over Σ = {,, } not eginning with. () The lnguge defined y the regulr expression (0 1) ((00) 1) 010. (d) The lnguge L = { n n m d m : n, m N}, where n mens n opies of in row. (e) The lnguge of plindromes over Σ = {, }. plindrome is word or phrse whih is spelled the sme forwrds nd kwrds, for exmple, rer, or le ws I ere I sw El. () This is just union, whih is very esy to enfore in grmmr. my nme is inigo montoy () We need to e reful here: does not egin with! Otherwise, we simply llow to hve n or followed y, where represents nything. () It turns out every regulr expression n e onverted vi simple lgorithm to regulr expression. We n do tht to get grmmr tht looks firly similr to this one, ut it my help to try to enode,,, nd s FGs nd then think out how you might omine them. 010 0 1 1 00 3
(d) This is simply two opies of the mthing prentheses grmmr, where nd d re our prentheses in the two prts of the prolem. We n simply write down those two grmmrs then omine them with. d (e) Even-length plindromes ren t too d: we just need to enfore tht we dd n or to oth the strt nd end t the sme time. However, we need to e reful: is plindrome, nd some (odd-length) plindromes hve single opy of or in the ext entre. omining these ides we get: 2. rgue tht every regulr lnguge is ontext-free y desriing how to onvert ny regulr expression into ontext-free grmmr. Rell tht regulr expression tkes one of five forms. We will present grmmr whih hndles eh se: then y indution on the omplexity of the regulr expression we see tht ny regulr expression n e enoded in FG, nd thus every regulr lnguge is ontext-free. R =, the empty word. The orresponding prodution is simply. R =, single symol. The orresponding prodution is simply. R = R 1 R 2, the ontention of two regulr expressions. The orresponding prodution is 1 2, where 1, 2 re the rules orresponding to R 1, R 2. R = R 1 R 2, the union of two regulr expressions. 1 nd 2. R = R1, the Kleene losure of regulr expression. 1 nd. This hs two orresponding produtions: One gin, we need two produtions: ine ll regulr expressions tke one of these four forms nd every regulr lnguge n e enoded y regulr expression, every regulr lnguge is ontext-free. 4
3 Prse Tree Exerises Let G e the following grmmr: 1. Find prse trees for the following words: () () () For ll of these, we simply need to find derivtion, fter whih the prse tree omes firly nturlly. Note tht in the pth we enfore tht there re the sme numer of s nd s, ut ny numer of s, wheres in the pth we enfore tht there re the sme numer of s nd s, ut ny numer of s. s result, we simply need to pik the pproprite strting point given the numer of s, s, nd s in the word, nd fter tht there is only one hoie t every step. () () 5
() 2. Prove tht G is miguous y finding two prse trees for the word. The key oservtion here is tht L(G) = { i j k : i = j or j = k}, nd hs i = j = k, so we n hoose to enfore either the i = j or the j = k onditions in the grmmr. Enforing i = j we get: Enforing j = k we get: 6