The University of Nottinghm SCHOOL OF COMPUTR SCINC AND INFORMATION TCHNOLOGY A LVL 1 MODUL, SPRING SMSTR 2004-2005 MACHINS AND THIR LANGUAGS Time llowed TWO hours Cndidtes must NOT strt writing their nswers until told to do so Answer QUSTION ON nd ny THR other questions Mrks ville for sections of questions re shown in rckets in the right-hnd mrgin. No clcultors re permitted in this exmintion. Dictionries re not llowed with one exception. Those whose first lnguge is not nglish my use dictionry to trnslte etween tht lnguge nd nglish provided tht neither lnguge is the suject of this exmintion. No electronic devices cple of storing nd retrieving text, including electronic dictionries, my e used. DO NOT turn exmintion pper over until instructed to do so Turn Over
2 Question 1 (Compulsory) The following questions re multiple choice. There is t lest one correct choice ut there my e severl. To get ll the mrks you hve to list ll the correct nswers nd none of the wrong ones. Note: The nswers elow provide some explntions, minly for the incorrect lterntives. This is just for clrifiction. The nswer should just e list of lterntives. () Which of the following sttements re correct? (i) An lphet is finite set of symols. (ii) A word is possily infinite sequence of symols over given lphet. (iii) A lnguge is the set of ll possile words over given lphet. (iv) A regulr lnguge is lwys finite. (v) A finite lnguge is lwys regulr. Correct: i, v Incorrect: ii A word must e finite. iii A lnguge is suset of the possile words. iv Σ is n exmple of n infinite regulr lnguge for ny nonempty lphet Σ. () Which of the following sttements re correct? (i) The empty word ɛ elongs to ll lnguges. (ii) The empty word ɛ is the only word in the empty lnguge. (iii) ɛ {} (iv) ɛ (v) If L is lnguge contining t lest one non-empty word, then L is n infinite lnguge. Correct: iii, iv, v Incorrect: i A lnguge is n ritrry suset of the possile words over given lphet. This my or my not include the empty word. ii The empty lnguge contins no words, not even the empty one. (5) (5)
3 (c) Consider the following finite utomton A over Σ = {, }: 0 1 3 2 Which of the following sttements out A re correct? (i) The utomton A is Deterministic Finite Automton (DFA). (ii) ɛ L(A) (iii) L(A) (iv) All words ccepted y A contin eqully mny s nd s. (v) The utomton A ccepts ll non-empty words over Σ tht contin eqully mny s nd s. (5) Correct: iii, iv Incorrect: i NFA since the trnsition function is not totl for the sttes 1 nd 2. ii Stte 0 is not n ccepting stte (nd there is no wy to get to n ccepting stte on the empty input). v No, it does not ccept, for exmple. (The utomton ccepts ( + )( + ).) (d) Consider the following set W of words: W = {ɛ,, c, } Which of the following regulr expressions denote lnguge tht contins ll words in W? (But not necessrily only the words in W : it is OK if the lnguge denoted y the regulr expression contins more words.) (i) (ɛ + + c)(ɛ + ) (ii) (ɛ + + c)( + ) (iii) (ɛ + + c) (iv) ( + c) (v) ( + c) (vi) () + c Turn Over
4 Correct: i, iii, iv Incorrect: (5) ii The empty word ɛ is not in the lnguge. To see this, recll tht conctention with the empty lnguge yields the empty lnguge nd simplify: (ɛ + + c)( + ) = (ɛ + + c) + (ɛ + + c) = (ɛ + + c) = + c + v Conctention with the empty lnguge yields the empty lnguge, thus the r.e. cn e simplified s follows: ( + c) = ( + c) = c vi The word c is not in the lnguge. (Six rther thn five lterntives is intentionl.) (e) Consider the following Context-Free Grmmr (CFG) G: S X Y C X Xc B Y Y ɛ B B ɛ C cc ɛ S, X, Y, B, C re nonterminl symols, S is the strt symol, nd,, c re terminl symols. Which of the following sttements out the lnguge L(G) generted y G re correct? (i) cc L(G) (ii) { i j c k (iii) { i i c j (iv) { i i c i i, j, k N} L(G) i, j N} L(G) i N} L(G) (v) L(G) = { i i c i i N} The lnguge denoted y the CFG is (5) Thus, correct: i, iii, iv Incorrect: L(G) = { i j c i } { i i c j }
5 Question 2 ii.g. ccc is not in L(G). v.g. ccc is in L(G). () Given the following NFA N over the lphet Σ = {,, c}, construct DFA D(N) tht ccepts the sme lnguge s N y pplying the suset construction:,, c,, c 0 1 2 3 To sve work, consider only the rechle prt of D(N). Clerly show your clcultions, e.g. in stte-trnsition tle. Do not forget to indicte the initil stte nd the finl sttes of the resulting DFA D(N). (12) The DFA sttes re sets of NFA sttes. Any DFA stte contining n ccepting NFA stte is ccepting. mrks n initil stte, finl one. δ D(N) c {0} = X {0, 1} {0} {0} {0, 1} = Y {0, 1} {0, 2} {0} {0, 2} = Z {0, 1, 3} {0, 3} {0, 3} {0, 1, 3} = U {0, 1} {0, 2} {0} {0, 3} = V {0, 1} {0} {0} Now we cn drw the trnsition digrm: U c, c X Y Z c, c, c V Turn Over
6 () Construct finite utomton (DFA or NFA) tht ccepts the lnguge of correct inry (se 2) dditions (nd no other strings) ccording to the following. We consider ddition of two inry numers of equl ut ritrry length. For exmple: 1001 +0011 1100 We represent n ddition y the string of inry digits (0 or 1) otined y reding the digits top-down, column y column, from left to right. The exmple ove is thus represented y the string 101001010110. We ssume tht the summnds hve een pdded with 0 s to the left to mke them s long s the result, if necessry. For exmple, the ddition 01 +11 100 is represented y the string 001010110. To simplify the prolem slightly, the empty string ɛ is considered representing correct inry ddition (two zero-length summnds yield zero-length result). (13) Key ide: When we encounter digit from the sum, it will e cler whether or not the ddition in the next column must result in crry if the ddition is to e correct. If not, we just go ck to the strt stte (which is ccepting). Otherwise, we go to n expect crry stte which is like the strt stte, except tht it is not ccepting nd tht wht constitutes correct sequence of digits is djusted to reflect the expected crry. The following trnsition digrm represents 10-stte NFA tht implements this ide. A is the initil nd only ccepting stte. F is the expect crry stte.
7 0 0 B 1 G 0 1 0 1 A 1 1 0 D F 1 0 0 I C 1 H 0 J 1 Question 3 () Clssify the following lnguges s regulr, context-free, or neither. Justify your nswer y providing, where possile nd resonle, regulr expression or context-free grmmr denoting the lnguge in question. Otherwise justify y giving short (informl ut convincing) rgument. (i) All words over Σ = {,, c} in which every is immeditely followed y. (ii) All words over Σ = {,, c} in which every is eventully followed y. (iii) { i i c j d j i, j N} (iv) { i j c i d j i, j N} (v) All legl sequences of moves in the gme of Chess. (i) Regulr: ( + + c) (ii) Regulr: (( + c) + + c) (iii) Context free: S AB A A ɛ B cbd ɛ (10) Turn Over
8 (iv) Not context free (nd thus not regulr). A production for gurnteeing the lnce etween the s nd the c s will necessrily look something like A Ac B. But once the B is reched, there is no wy to enforce tht the numer of s mtches the numer of d s tht follow the string derived from A. The sitution is similr if one sets out to initilly mintin the lnce etween the s nd the d s. Another rgument: PDA hs only one stck nd thus cnnot count two different things simultneously nd independently. (v) Regulr since the numer of chess sttes (wys of plcing pieces on the ord) is finite nd since the legl moves is uniquely determined y the current stte. () Systemticlly construct n NFA ccepting the lnguge denoted y the following regulr expression y following the grphicl construction descried in the lectures/lecture notes: The lphet is Σ = {,, c, d}. ( + ) (c + d) Your nswer should clerly show wht you re doing. In prticulr, in ddition to the finl NFA, the nswer should include t lest two intermedite stges of the construction. However, sttes only hve to e nmed in the finl NFA. Also, feel free to tidy up the finl NFA y removing ded ends, ut e sure to explin wht you re doing. (5) Work structurlly, from the smllest constituent suexpressions towrds to overll regulr expression. Since they re simple enough, we strt with NFAs for ( + ): q 1 q 3 q 2 q 4 nd (c + d): q 5 c q 7 q 6 d q 8
9 Then form n NFA for ( + ) : q 1 q 3 q 2 q 4 q 9 Conctente the NFAs for ( + ) nd (c + d), keeping in mind tht there is strt stte tht is lso finl in the first NFA (since ɛ elong to the lnguge), mening tht the initil sttes of the second NFA re kept s initil sttes. Don t forget to chnge the ccepting sttes of first the NFA to non-ccepting ones: q 1 q 2 q 3 q 4 q 5 q 6 c d q 7 q 8 q 9 Finlly, remove ded ends : Turn Over
10 q 1 q 2 q 5 q 6 c d q 7 q 8 (c) Use the Pumping Lemm for regulr lnguges to show tht the following lnguge is not regulr: { i j c k i, j, k N, k = min(i, j)} (10) Cll the given lnguge L. Assume it is regulr. Then, ccording to the pumping lemm for regulr expressions, there is constnt n such tht ny string w L which hs length t lest n ( w n) cn e divided into three prts w = xyz s follows: 1. xy n 2. y > 0 3. xy i z L for ny nturl numer i Consider string w = n n c n. As min(n, n) = n, we clerly hve w L. Moreover, the length of w is w = 3n n. The pumping lemm for regulr lnguges therefore pplies, nd our w, s ny sufficiently long string in the lnguge, cn e divided into three prts w = xyz ccordingly. Since xy n, it must e the cse tht y = k for 0 < k n due to the wy w ws chosen nd condition 2 on the division into prts. Now, consider condition 3. It should hold for ny i. Pick i = 0 for exmple. xy 0 z = (n k) n c n. Since k > 0, n k < n. Therefore min(n k, n) = n k < n. But the string xy 0 z hs n c s. Tht is too mny, nd thus it cnnot elong to L. We hve contrdiction, nd thus our initil ssumption tht L is regulr must e wrong. Thus L is not regulr, QD. Question 4
11 () The following is context-free grmmr (CFG) for Boolen expressions: () t f is nonterminl nd the strt symol,,,, (, ), t, nd f re terminls. Show tht this grmmr is miguous. (5) A CFG is miguous if t lest one word in the descried lnguge hs more thn one prse tree. To show tht grmmr is miguous pick word in the lnguge tht hs two prse trees nd show these two trees. For the given lnguge, t t t is word tht hs two prse trees: t t t t t t An equivlent wy is to show tht the word in question either hs two leftmost derivtions or two rightmost derivtions. It is NOT enough to just show two different derivtions, s merely permuting the order in which non-terminls re expnded does not ffect the structure of the corresponding prse tree. Here re two different leftmost derivtions. The first one, corresponding to the first tree: t t t t t t Turn Over
12 The second one, corresponding to the second tree: t t t t t t t () Construct n unmiguous version of the context-free grmmr for Boolen expressions given ove y mking it reflect the following opertor precedence conventions: hs the highest precedence hs the next highest precedence hs the lowest precedence For exmple, t f t should e interpreted s t (( f) t). As long s the grmmr is unmiguous, you cn choose whether or not to ccept expressions tht would need conventions out opertor ssocitivity to dismigute them, like t t t. (10) Here is version tht ssumes tht the inry opertors re nonssocitive. (Thus the lnguge ccepted is not quite the sme s for the miguous grmmr. But tht s OK ccording to the prolem sttement.) 1 1 1 1 2 2 2 2 2 ( ) t f The prolem does not stte whether using mny logicl negtions immeditely fter one nother should e OK or not (e.g. t). The ove grmmr does llow tht. The following grmmr does not: 1 1 1 1 2 2 2 2 3 3 3 ( ) t f The grmmr is unmiguous either wy, so oth versions re fine.
13 (c) Drw the derivtion trees ccording to your unmiguous grmmr for the following two expressions: (i) t f (ii) (f t) f t Prse trees ccording to the first grmmr ove. Prse tree for t f: (5) 1 1 2 2 2 f t Prse tree for (f t) f t: 1 1 2 2 2 ( ) 2 t 1 1 f 2 2 f t (d) The inry opertors nd cn e considered to e: left-ssocitive; i.e. n expression like t t t would e interpreted s (t t) t right-ssocitive; i.e. n expression like t t t would e interpreted s t (t t) non-ssocitive; i.e. ruling out expressions like t t t Turn Over
14 xplin wht is the cse for your grmmr nd why, nd how to chnge your grmmr for the other possiilities. (5) Left-ssocitive: mke the productions for the inry opertors left recursive: 1 1 1 2 Right-ssocitive: mke the productions for the inry opertors right recursive: 1 1 2 1 Non-ssocitive: do not mke the productions for the inry opertors directly recursive, s in the originl grmmr. Question 5 Consider the following Pushdown Automton (PDA) P : P = (Q = {q 0, q 1 }, Σ = {,, c}, Γ = {, #}, δ, q 0, Z 0 = #, F = {q 1 }) where the trnsition function δ is given y Acceptnce is y finl stte. δ(q 0,, #) = {(q 0, #)} δ(q 0, c, #) = {(q 0, #)} δ(q 0,, ) = {(q 0, )} δ(q 0,, ) = {(q 0, ɛ)} δ(q 0, c, ) = {(q 0, )} δ(q 0, ɛ, #) = {(q 1, #)} δ(q, w, z) = everywhere else () Which of the following words re ccepted y the PDA P? (i) cc (ii) ccc (iii) ɛ For those words tht re ccepted, provide sequence of Instntneous Descriptions (IDs) leding to n ccepting configurtion s evidence. For those words tht re not ccepted, explin why there is no sequence of IDs leding to n ccepting configurtion. (12)
15 (i) The word cc is ccepted. ID sequence: (q 0, cc, #) (q0, cc, #) (q0, c, #) (q0, c, #) (q0, c, #) (q0, c, #) (q0, ɛ, #) (q1, ɛ, #) The word is ccepted ecuse q 1 is n ccepting stte nd since ll input hs een red. (Mrking: 5 points.) (ii) The word ccc is not ccepted. On seeing c in stte q 0 with # on top of the stck, there re two possiilities. The PDA cn either red nd discrd the c, stying in q 0, or it cn move to q 1 without reding the c. We oserve tht the mchine gets stuck s soon s stte q 1 is reched. Thus, if we wnt to ccept string strting with c, the first trnsition must e to red nd discrd tht c: (q 0, ccc, #) (q 0, cc, #) We re now in stte q 0, reding, with # on top of the stck. The only possiility here is to move to q 1 without reding ny input: (q 1, cc, #) But this is stuck configurtion! And since ll input hs not een red, it is not n ccepting configurtion. Thus there re no sequences of IDs leding to n ccepting configurtion. (Mrking: 5 points.) (iii) The word ɛ is ccepted: (q 0, ɛ, #) (q 1, ɛ, #) This is n ccepting configurtion since q 1 is n ccepting stte nd since ll input (none!) hs een red. (Mrking: 2 points.) () Descrie the lnguge ccepted y P in nglish in one sentence. (5) Strings over Σ = {,, c} where cts s n opening prenthesis, s closing prenthesis, nd prentheses hs to e lnced in the usul fshion. Turn Over
16 (c) xplin how to modify P to mke it ccept y empty stck insted of ccepting y finl stte (without chnging the ccepted lnguge). (2) One possiility is to replce the eqution with or even (getting rid of one stte). δ(q 0, ɛ, #) = δ(q 1, #) δ(q 0, ɛ, #) = δ(q 1, ɛ) δ(q 0, ɛ, #) = δ(q 0, ɛ) ither wy, this gives the PDA the possiility to completely empty the stck when it is in stte where the prentheses seen so fr hs lnced out. If the PDA mkes tht move in stte where ll input hs een red it will enter n ccepting configurtion (nd lock), just s the given PDA does when entering stte q 1. (d) Wht does it men for PDA to e deterministic? Stte the forml condition nd explin wht it mens. Is P deterministic? Justify your nswer! (6) Tht the PDA never hs ny choice. Formlly: for ll q Q, x Σ, nd z Γ. δ(q, x, z) + δ(q, ɛ, z) 1 P is not deterministic s it hs choice in stte q 0 when # is the top stck symol for input symols nd c. Formlly, we hve e.g. δ(q 0,, #) + δ(q 0, ɛ, #) = 1 + 1 = 2 which is not less thn or equl to 1. Question 6 () Wht is Turing Mchine (TM)? Your ccount does not hve to e forml, ut it should e comprehensive nd clerly outline the centrl ides. (5) A Turing Mchine is mthemticl model of generl computer. It consists of finite control plus n infinite tpe for storge of the input, the output, nd ny other dt needed during computtion. The tpe is divided into cells, ech cell is cple of storing one symol. A red/write hed scns one cell of the tpe, nd depending on the symol in tht cell nd the stte of the finite control, the TM updtes the scnned symol with new symol (possily the sme s efore), moves the hed one step left or right, nd chnges stte.
17 () Outline strtegy for constructing TM tht ccepts the lnguge { m n c m d n m, n N} If n is eing red, overwrite it with mrking symol not in the input lphet, sy X, move to the right until corresponding c is found, ut only moving cross first s, then s, then X s if ny, mrk tht s well, nd then move left until we find the first remining, nd repet. If t ny point c cnnot e found, stop in non-ccepting stte. If there ws no to egin with, or once there re no remining s, we do s ove for nd d. If the mchine hs reched stte where the lst remining nd hs een mtched with corresponding c or d, the TM is in stte where it is scnning left for remining or. But insted of finding one, it is going to encounter lnk symol to the left of the portion of the tpe where the input initilly ws written. At this point, the mchine strts scnning right. If it only reds X s efore the first lnk symol to the right of the portion of the tpe where the input ws stored is found, the TM moves to n ccepting stte nd stops. Otherwise it stops in non-ccepting stte. (c) xplin nd relte the following terms in the context of Turing Mchines: recursive, recursively-enumerle, decidle, undecidle. (5) A recursive lnguge is lnguge tht is ccepted y Turing Mchine tht lwys hlts. This is the sme s sying tht the lnguge is decidle (or tht the prolem represented y the lnguge is decidle). A recursively enumerle lnguge is lnguge tht is ccepted y Turing Mchine tht does not necessrily hlt for input not elonging to the lnguge. A lnguge tht is not recursive is undecidle. The undecidle lnguges thus includes the recursively enumerle (or semi-decidle) lnguges nd those lnguges which re not even recursively-enumerle. (5) (d) Wht is the Church-Turing thesis? Give rief explntion. (5) The unproven ssumption tht ny resonle notion of wht computtion mens is equivlent to wht Turing mchine cn compute. (e) One could rgue tht computer is relly finite stte utomton since ny computer only hs finite memory. Is this useful chrcteriztion? Provide good rgument for your position. (5) Turn Over
18 Arguing either wy is fine s long s the rgument is good! Here is my position: ven very modest rel computer hs fr too mny sttes for n FA chrcteriztion to provide ny useful model of its ehviour. If one does not ccept tht, then note tht it would e possile to design nd progrm computer in such wy tht it dynmiclly cn sk for more memory should the need rise (e.g. y mking dditionl storge resources ville over network). Then, given given some time, the computer would lwys hve s much memory s it needs, which for prcticl purposes is the sme s n unounded mount of memory. Agin, finite utomton is not useful chrcteriztion. (If one wnts to push the rgument, one could sy tht the resources on the rth re limited, nd thus it is conceivle tht progrm might need more memory thn ctully could e constructed using the resources ville on the rth. But nothing in principle stops us from moving eyond the rth. So ultimtely wht is going to set the limit is wht we re prepred/cn fford to do to extend the memory, nd for how long we cn wit for the finl nswer, not tht it is theoreticlly impossile to extend the memory.) nd