Mälrdlen University TEN1 DVA337 2015 School of Innovtion, Design nd Engineering Forml lnguges, utomt, nd theory of computtion Thursdy, Novemer 5, 14:10-18:30 Techer: Dniel Hedin, phone 021-107052 The exm hs totl of 40 points nd consists of 3 pges. No ids re llowed. Answers must e given in English nd should e clerly justified. 1. Regulr lnguges (14 p) ) Floting point numers re uilt y n optionl sign followed y digits seprted y deciml point nd end with n optionl exponent prt. Exmples of floting point numers re 1.35e+10 +2.54-5.2 245.3E-2 Write regulr expression tht recognizes floting point numers. (2 p) Solution: (- +)?[0-9]+.[0-9]+( (e E)(+ -)[0-9]+ )? ) Convert the following NFA to n equivlent miniml DFA. (6 p) q1 q2,, q3 Solution: We first convert the NFA to DFA using the suset construction lgorithm. For ook keeping we stte the trnsition function δ q 1 {q 2 } {q 1 } q 2 {q 2,q 3 } {q 1,q 3 } q 3 {q 1 } {q 1,q 3 } Strting in stte {q 1 } (the set of λ-rechle sttes from the strting stte of the NFA), we cn systemticlly construct the following DFA, where Q 1 = {q 1 }, Q 2 = {q 2 }, Q 3 = {q 2,q 3 }, Q 4 = {q 1,q 2,q 3 }, Q 5 = {q 1,q 3 }, Q 6 = {q 1,q 2 } with Q 2, Q 3, Q 4 nd Q 5 re finl sttes, since they contin the t lest one finl stte from the NFA. 1
Q1 Q2 Q3 Q4 Q5 Q6 To minimize this DFA we pply stte prtitioning strting with two sets of sttes A = {Q 1,Q 5 } nd B = {Q 2,Q 3,Q 4,Q 6 } corresponding to the set of non-finl nd the set of finl sttes respectively nd see tht the trnsitions of the originl DFA do not prtition these sets further. A Q 1 Q 5 B B A A B Q 2 Q 3 Q 4 Q 6 B B B B A A A A Bsed on this we get the following mininl DFA. A B c) Stte the pumping lemm for regulr lnguges nd use it to prove tht L = { n n n 0} is not regulr. (4 p) Solution: The pumping lemm for regulr lnguges sttes tht for ll regulr lnguges L, there exists positive numer m such tht for ll strings w L with w m it holds tht the exists decomposition of w = xyz suject to xy m nd y 1 for which it holds tht xy i z L for ny i 0. To prove tht L = { n n n 0} is not regulr we proceed with proof y contrdiction. Assume tht L is regulr, i.e., tht the pumping lemm holds for L nd show how to construct string w in terms of m chosen so tht we cn show tht for ny decomposition w = xyz suject to xy m nd y 1 there is t lest one i 0 for which xy i z L. This shows tht the pumping lemm does not hold for L nd we hve reched contrdiction. In this prticulr prolem we cn chose w = m m. Due to xy m we hve tht x = j, y = k, for k 1, nd z = l m for j + k + l = m. According to the pumping lemm if L is regulr then for every i 0 it should hold tht xy i z L. But for ny i 1 we cn show tht xy i z L. Consider for instnce i = 0 then xy i z = xy = j+l m, ut 2
we hve tht j +l m, since k 1, nd we cn drw the conclusion tht j+l m L. Hence, L is not regulr. d) Consider the prolem of nested C-like comments. A comment is well-formed if the numer of strting symols /* mtches the numer of ending symols */ nd in ny given prefix the numer of strting symols is greter thn or equl to the numer of ending symols. To illustrte consider the following exmples of well-formed nested comments /* Well-formed comment */ /* Also /* well-formed nested comment */!! */ nd the following exmples of mlformed nested comments /* Not */ well-formed comment */ /* Also not /* well-formed comment */ Give convincing rgument tht it is not possile to crete DFA tht ccepts precisely the well-formed nested comments. (2 p) Solution: Recll tht regulr lnguges re closed under intersection, i.e., tht if L 1 nd L 2 re regulr lnguges then so is L 1 L 2. Thus, if it cn e shown tht L 1 L 2 is not regulr nd tht, e.g, L 2 is regulr then we cn drw the conclusion tht L 1 is not regulr. Let L 1 denote the lnguge of well formed nested C-like comments nd let L 2 = L((/*) (*/) ), i.e., the regulr lnguge ny numer of /* followed y ny numer of /*. Now consider the intersection of L 1 nd L 2. It is cler tht L 3 = L 1 L 2 = {(/*) n (*/) n n 1}, ut we hve lredy proved (in 1.c) tht L = { n n n 0} is not regulr for ny nd. This implies tht L 3 is not regulr, which together with the fct tht L 2 is regulr implies tht L 1 is not regulr. 2. Context-free lnguges (14 p) ) Explin wht it mens for grmmr to e miguous. (2 p) Solution: A grmmr is miguous if there exists string with more thn one left-most derivtion (or, equivlently, more thn one rightmost derivtion). Alterntively, grmmr is miguous if there exist string with more thn one prse tree. 3
) Consider the following smll grmmr for n expression lnguge E ::= E + E E E NUM where NUM represents numers. Show tht this grmmr is miguous. (2 p) Solution: Numer the production rules 1, 2 nd 3 in order from the left. Consider the string 1 + 2 3 with the following two left-most derivtions 1) E 1 E + E 3 1 + E 2 1 + E E 3 1 + 2 E 3 1 + 2 3 2) E 2 E E 1 E + E E 3 1 + E E 3 1 + 2 E 3 1 + 2 3 corresponding to 1 + (2 3) nd (1 + 2) 3 respectively. c) Rewrite the ove grmmr to e unmiguous nd show y exmple tht your grmmr follows the stndrd precedence rules for ddition nd multipliction. (2 p) Solution: We need to encode tht multipliction inds hrder thn ddition. This cn e done in the following wy E ::= E + T T T ::= T NUM NUM Now, numering the production rules E 1, E 2, T 1 nd T 2 from left to right the string 1 + 2 3 hs unique left-most derivtion E E 1 E + T E 2 T + T T 2 1 + T T 1 1 + T 3 T 2 1 + 2 3 d) Consider the following grmmr of well-formed nested comments S ::= λ / A / A ::= λ A SA where,, nd / re terminls. Crete deterministic pushdown utomton tht recognizes the ove grmmr, nd show the sequence of instntneous description corresponding to running the utomton on 1) /*/**/*/ nd 2) /**/ (6 p) Solution: Note tht the methods for utomticlly trnslting context-free grmmrs to push-down utomt generte non-deterministic push-down utomt. By studying the lnguge generted y S, however, it is reltively esy to crete DPDA. The key ide is to use the stck to count the numer of strting symols /* nd to mke sure they mtch up with the numer of ending symols */. Between these symols ny numer of s cn occur. A key thing to note is tht once the outermost strting symol hs een mtched nothing else cn e mtched. Below it is ssumed tht the stck is initilly empty nd use $ s the distinguished symol to signl the end of the stck. 4
q1 /, λ $ q2 *, λ X, λ λ q3 /, λ λ *, λ X *, λ λ q4 q5 /, X X /, X λ *, X X q6 λ, $ λ q7, X X 1) For /*/**/*/ we get the following derivtion (q 1,/*/**/*/,$) (q 2,*/**/*/,$) (q 3,/**/*/,X$) (q 3,/**/*/,X$) (q 4,**/*/,X$) (q 3,*/*/,XX$) (q 5,/*/,XX$) (q 6,*/,X$) (q 5,/,X$) (q 6,λ,$) (q 7,λ,$) ccept 2) For /**/ we get the following derivtion. (q 1,/**/,$) (q 2,**/,$) (q 3,*/,X$) (q 5,/,X$) (q 6,,$) (q 7,,$) reject - input string not empty e) Do deterministic pushdown utomt hve the sme computtionl power s nondeterministic pushdown utomt? Give convincing rgument for or ginst. (2 p) Solution: Nondeterministic pushdown utomt hve re strictly more powerful thn deterministic pushdown utomt. As n exmple, consider the lnguge of even length plindromes {ww R w Σ } for Σ = {,}. In order for deterministic pushdown utomton to check if given string w is plindrome it would hve to know when it hs reched the middle of the string in order to strt mtching. However, unlike the lnguge { n n n 0}, where the trnsition from the lst to the first signifies the middle there is no wy for deterministic pushdown utomton to know when it hs reched the middle of the plindrome. A nondeterministic pushdown utomton, however, is le to do this, since the non-determinism llows it to serch ll possile splits. 5
3. Restriction-free lnguges nd theory of computtion (12 p) ) Consider the Turing mchine descried y M = ({q 1,q 2,q 2,q 3,q 4,q 5,q 6,q 7 },{,},{,, },δ,q 1,,{q 7 }) with δ defined s follows δ(q 1,) = (q 2,,R) δ(q 1,) = (q 3,,R) δ(q 1, ) = (q 7,,L) δ(q 4,) = (q 6,,L) δ(q 2,) = (q 2,,R) δ(q 2,) = (q 2,,R) δ(q 2, ) = (q 4,,L) δ(q 5,) = (q 6,,L) δ(q 3,) = (q 3,,R) δ(q 3,) = (q 3,,R) δ(q 3, ) = (q 5,,L) δ(q 6,) = (q 6,,L) δ(q 6,) = (q 6,,L) δ(q 6, ) = (q 1,,R) 1. Drw the trnsition grph of M. (2 p) 2. Wht lnguge does M ccept? (2 p) 3. Select string w {,} 4 tht is ccepted y M nd show the execution of M on w s sequence of instntneous descriptions. (2 p) Solution: 1. The stte mchine corresponding to M is the following.,,r;,,r q2,,l,,r,,r;,,r q4,,l,,l;,,l q1,,r q3,,l q5,,l q6,,l,,r q7 2. M recognizes the lnguge of even length plindromes, {ww R w Σ }. To see this consider how M opertes. First, M ssume tht M is strted with some input w, i.e., corresponding to the instntneous description q 1 w. First, the empty string is ccepted, since if the first chrcter is the lnk symol, M ccepts. Otherwise, the first symol is either n, or. In oth cses, M overwrites it with the nd serches for the lst non-lnk symol if it exists. If it does not exist or if it does not mtch the overwritten symol the mchine hlts. Otherwise, M overwrites the mtching symol nd serches for the first non-lnk symol. If it exists, the process repet until no more symols remin. At this point M ccepts. Phrsed more distinctly, M shves off mtching symols in oth ends until only the empty string remins, which is ccepted. 6
3. For w = we hve the following derivtion q 1 q 2 q 2 q 2 q 2 q 4 q 6 q 6 q 6 q 1 q 3 q 3 q 5 q 6 q 1 q 7, hlt nd ccept ) Explin the concept of reduction proof in the context of theory of computtion, i.e., proving undecidility y reducing one prolem to nother. (2 p) Solution: A proof y reduction is proof where we reduce known undecidle prolem A to nother prolem B. The reduction is performed showing how to use decision procedure for B s decision procedure for A. This mens tht the existence of decision procedures for B implies the existence of decision procedure for A. However, since we know tht there cnnot exist ny decision procedures for prolem A we drw the conclusion tht there cnnot exist ny decision procedures for B. c) Recll the stte-entry prolem, i.e., given Turing mchine M = (Q,Σ,Γ,δ,q 0,,F) nd ny q Q, nd w Σ +, decide whether or not the stte q is entered y M when it is pplied to w. Prove tht the stte-entry prolem is undecidle. (4 p) Solution: Proof y reducing the hlting prolem into the stte-entry prolem. We do this y creting Turing mchine ˆM tht hlts in stte ˆq if nd only if M hlts. Recll tht Turing mchine hlts in stte q i for input when δ(q i,) is undefined (we demnd tht finl sttes re hlting for ll input). Thus, we cn crete ˆδ y mpping ny undefined trnsitions in to ˆq. ˆδ(q i,) = { δ(q i,) if δ(q i,) defined ( ˆq,, R) otherwise Now, the stte-entry prolem for ˆM, ˆq, nd w is equivlent to the hlting prolem for M nd w. 7