Deprtment of Computer Science, Austrlin Ntionl University COMP2600 Forml Methods for Softwre Engineering Semester 2, 206 Assignment Automt, Lnguges, nd Computility Smple Solutions Finite Stte Automt nd Regulr Lnguges Prolem. Consider the following NFA A over {, }: strt s 0, s s 2 s 3 A is intended to recognise the lnguge L = {zα z {, }, α {} } of strings over the lphet {, } tht strt with either or, end with nd hve n ritrry numer of s etween the first nd the lst letter. (i) The following trnsition tle shows DFA A D tht is otined from A y the method of lzy suset construction: {s 0 } {s, s 2 } {s 2 } {s, s 2 } {s, s 2 } {s 3 } {s 2 } {s, s 2 } {s 3 } {s 3 } Assignment Automt, Lnguges, nd Computility
For the ske of convenience, let s define DFA A D tht is exctly like A D ut sttes re renmed. The following tle shows A D : The trnsition digrm of A D is s follows: q 4 q 4 q 4 q 0 q q 2 q q q 3 q 2 q q 3 q 3 q 4 q 4 strt q 0 q q 2 q, 3 q 4, (ii) The initil split differentites non-finl nd finl sttes: [ [ q 0, q, q 2, q 4 ], [ q 3 ] ] splits [ q 0, q, q 2, q 4 ]: [ [ q 0, q 4 ], [ q, q 2 ], [ q 3 ] ] (s well s ) splits [ q 0, q 4 ]: [ [ q 0 ], [ q 4 ], [ q, q 2 ], [ q 3 ] ] Neither nor splits the only remining non-singleton group [ q, q 2 ]. Every other group is singleton. Therefore, the lgorithm termintes. Sttes q nd q 2 re equivlent. (iii) A m is s follows Assignment Automt, Lnguges, nd Computility 2
strt q 0, q q, 3 q 4, (iv) Prove tht A m recognises L. Proof: We need to prove the following two sttements () for ll w, if δ (q 0, w) = q 3 then w L () for ll w, if w L then δ (q 0, w) = q 3 Ech of these sttements cn e reformulted s follows () for ll w, if δ (q 0, w) = q 3 then either w = α or w = α, where α {} () for ll α, if α {} then δ (q 0, α) = q 3 nd δ (q 0, α) = q 3 Below we prove ech of these sttements () w = 0 Hence, w = ɛ. δ (q 0, ɛ) = q 0. Stte q 0 is stte distinct from q 3, therefore the ntecedent of the conditionl sttement is flse. Therefore, the whole sttement is true. w = Hence, either w = or w =. In oth cses δ (q 0, w) = q. Stte q is stte distinct from q 3, therefore the ntecedent of the conditionl sttement is flse. Therefore, the whole sttement is true. w > Hence, w is of the form xαy, where x, y {, } nd α {, }. Assume δ (q 0, xαy) = q 3. By definition of δ the following holds δ (q 0, xαy) = δ (δ(q 0, x), αy) δ(q 0, x) is defined for x {, } s follows: δ(q 0, ) = q δ(q 0, ) = q Therefore, regrdless whether x is or, δ(q 0, x) = q. Hence, δ (q 0, xαy) = δ (q, αy) Assignment Automt, Lnguges, nd Computility 3
δ (q, αy) = δ(δ (q, α), y) = q 3 There is only one one-step trnsition tht leds to q 3. It is lelled. Therefore y must e nd δ (q, α) must e q. Consequently, it is sufficient to prove the following sttement: for ll α {, }, if δ (q, α) = q then α {} Lemm proves this sttement. () Assume α {}. We need to prove tht δ (q 0, α) = q 3 nd δ (q 0, α) = q 3. We consider δ (q 0, α) nd δ (q 0, α) seprtely: δ (q 0, α) = δ (δ(q 0, ), α) = δ (q, α) = δ(δ (q, α), ). δ (q 0, α) = δ (δ(q 0, ), α) = δ (q, α) = δ(δ (q, α), ). In oth cses we reched δ(δ (q, α), ). By Lemm 2, δ (q, α) = q. Therefore, δ(δ (q, α), ) = δ(q, ) = q 3 Lemm For ll β {, }, if δ (q, β) = q then β {} Proof: β = 0. β = ɛ nd β {}. β > 0. This mens tht β = xα where x {, } nd α {, }. We need to prove the following: if δ (q, xα) = q then xα {} Assume δ (q, xα) = q () By the definition of δ the following holds: δ (q, xα) = δ (δ(q, x), α) (2) For the ske of contrdiction ssume tht x =, then δ(q, x) = δ(q, ) = q 3. Then δ (q, xα) = δ (q 3, α) = q This mens tht from stte q 3 we rech stte q 3 fter reding α. But ll trnsitions from q 3 led to the sink stte q 4. Therefore δ (q 3, α) = q is impossile. We reched contrdiction. Therefore, x nd x cn only e equl to, since is the only remining symol of the lphet. Hence we cn rewrite δ (δ(q, x), α) in (2) s follows: δ (δ(q, ), α) Since δ(q, ) = q nd y ssumption () the following holds: δ (δ(q, ), α) = δ (q, α) = q The only one possile wy to rech q from q is trnsition δ(q, ) or sequence of them. Therefore α {}. Hence, xα {}. Assignment Automt, Lnguges, nd Computility 4
Lemm 2 For ll α {}, δ (q, α) = q Proof: Induction on the length of α. Bse cse: α = 0. δ (q, ɛ) = q Induction: α > 0. Induction hypothesis: δ (q, n ) = q, where n is string consisting of sequence of n s. We need to prove δ (q, n+ ) = q. δ (q, n+ ) = δ(δ (q, n ), ) By induction hypothesis δ (q, n ) = q, hence Prolem 2. Let L e the lnguge δ (q, n+ ) = δ(q, ) = q L = {w {0, } w {0} or w {} } Construct deterministic finite utomton tht ccepts ll strings tht re in L, nd rejects ll strings tht re not in L. 0 strt q 0 0 q q 3 0, 0 q 2 Prolem 4. Let L e the lnguge L = {w {0, } w hs in the third position from the right} Construct non-deterministic finite utomton tht ccepts ll strings tht re in L, nd rejects ll strings tht re not in L. 0, strt q 0 q 0, q 0, 2 q 3 Assignment Automt, Lnguges, nd Computility 5
Prolem 3. Let L e the lnguge L = {w {0, } ech lock of 5 consecutive symols contins t lest two 0 s} Construct deterministic finite utomton tht ccepts ll strings tht re in L, nd rejects ll strings tht re not in L. Explin in plin English the intuition ehind the constructed utomton. We first define non-miniml DFA A n tht recognizes L. The nme of stte conveys the informtion out the sequence of 0s nd s of the lst 4-symols lock tht the utomton hs processed. Thus, if the sequence of the lst four processed symols ws 000, the utomton is in stte nmed 000. Then δ(000, 0) = 000 nd δ(000, ) = 00. We define the only non-ccepting stte sink. Whenever the utomton is in stte nmed with sequence of four symols nd t lest three of these symols re s, the utomton goes to the rejecting stte sink on input. The only exception is the string, s discussed elow. Since the utomton hs to ccept ll strings of length smller thn 5, there re sttes for these strings. The only string contining four consecutive s tht is ccepted is the string of length 4. Stte is the ccepting stte corresponding to strings. If the first four s of string re followed y symol, the utomton performs trnsition to sink. The initil stte is nmed ɛ. It is the stte for the empty string. Thus the utomton is s follows: Assignment Automt, Lnguges, nd Computility 6
0 *ɛ 0 *0 00 0 * 0 *00 000 00 *0 00 0 *0 00 0 * 0 *000 0000 000 *00 000 00 *00 000 00 *0 00 0 *00 000 00 *0 00 0 *0 00 0 * 0 *0000 0000 000 *000 000 00 *000 000 00 *00 00 0 *000 000 00 *00 00 0 *00 00 0 *0 0 sink *000 0000 000 *00 000 00 *00 000 00 *0 00 sink *00 000 00 *0 00 sink *0 00 sink * sink sink sink sink sink A n cn e minimized. The equivlence clsses of sttes re s follows: [ɛ] [] [sink] [] [0] [0] [] [0] [0] [] [00, 000, 00, 0] [0, 00] [0, 00] [0, 00] [0, 00, 000, 00] [0, 00, 000, 00, 0000, 000, 000, 00] Among equivlent sttes, we keep the sttes tht re emphsized in old ove. This gives us the following miniml utomton A m : Assignment Automt, Lnguges, nd Computility 7
0 *ɛ 0000 * 00 * 00 * 0 *0000 0000 000 *000 00 00 *00 00 0 *00 00 0 *00 0000 0 *0 0 sink *00 0000 00 *0 00 sink *0 00 sink *0 0000 sink * sink sink sink sink sink Here is A m s trnsition digrm: strt ɛ 0 0 0 0 0, 0 0000 000 00 0 sink 0, 0 0 0 0 0 0 00 00 0 0 0 00 0 0 0 Assignment Automt, Lnguges, nd Computility 8
2 Grmmr Prolem 5. From the NFA A given in Prolem, derive right-liner grmmr using the lgorithm given in the lectures. The grmmr is s follows G = ({, }, {S 0, S, S 2, S 3 }, S 0, P ) where the set P consists of the following production rules S 0 S S 0 S 2 S 0 S 2 S S 2 S S 3 S 2 S S 2 S 2 S 2 S 3 S 3 ɛ Prolem 2.6. Consider the grmmr G = ({S, T }, {0, }, S, P ), where P consists of the following productions S 0S T 0 T T Show tht no string in the lnguge L(G) contins the sustring 0. Proof: Let α e sententil form derived using G. We show y induction on the length n of the derivtion of α tht α does not contin ny of the sustrings 0, S, S0, SS, T 0 or T S. Bse cse: n=. The possile derivtions of length of sententil forms for G re S 0S, S T, nd S 0. Neither 0S, nor T, nor 0 contin 0, S, S0, SS, T 0 or T S s sustring. Inductive cse: Let S α β e derivtion of β of length n +. By inductive hypothesis, α does not contin 0, S, S0, SS, T 0 or T S s sustring. If the lst derivtion step uses S 0S, since α does not contin S, SS or T S, the derivtion step cnnot introduce 0, S0 or T 0 in β. If the lst derivtion step uses S T, since α does not contin S0 or SS, the derivtion step cnnot introduce T 0 or T S in β. If the lst derivtion step uses S 0, since α does not contin S, SS or T S, the derivtion step cnnot introduce 0, S0 or T 0 in β. If the lst derivtion step uses T T, since α does not contin T 0 or T S, the derivtion step cnnot introduce T 0 or T S in β. If the lst derivtion step uses T, since α does not contin T 0 or T S, the derivtion step cnnot introduce 0 or S in β. Assignment Automt, Lnguges, nd Computility 9
3 Context Free Lnguges nd Pushdown Automt Prolem 3.6. Show tht the lnguge {uw u, w {, }, with u = w } is context free y exhiiting context free grmmr tht genertes it. A CFG grmmr tht genertes this lnguge is G = ({, }, {S, T, X}, S, P ), where P consists of the following productions: S T T XT X X Prolem 7. Consider the context-free grmmr G = ({S, A, B}, {,, c}, P, S), where P consists of the following productions S A A BA B S cs Construct PDA M tht ccepts L(G) y empty stck. Drw the prse three of G for the string, nd show the corresponding execution trce for M. The prse tree for the string : A A A S S B M = ({q 0, q, q 2 }, q 0, {q 2 }, {,, c}, {,, c, S, A, B, Z}, Z, δ) with δ defined s follows: δ(q 0, ɛ, Z) = {(q, SZ)} δ(q, ɛ, S) = {(q, A)} δ(q, ɛ, A) = {(q, BA), (q, )} δ(q, ɛ, B) = {(q, S), (q, cs)} δ(q,, ) = {(q, ɛ)} δ(q,, ) = {(q, ɛ)} δ(q, c, c) = {(q, ɛ)} δ(q, ɛ, Z) = {(q 2, ɛ)} Assignment Automt, Lnguges, nd Computility 0
The execution trce of M for the string is s follows: (q 0,, Z) (q,, SZ) (q,, AZ) (q,, AZ) (q,, BAZ) (q,, SAZ) (q,, SAZ) (q,, AAZ) (q,, AAZ) (q,, AZ) (q,, AZ) (q,, Z) (q, ɛ, Z) (q 2, ɛ, ɛ) 4 OPTIONAL: Turing Mchine nd Computility Prolem 8. Design 2-tpe Turing mchine ccepting the lnguge of ll strings over {0, } tht hve n equl numer of 0 s nd s. The first tpe contins the input, nd is scnned from left to right. The second tpe is used to keep trck of the difference etween the numer of 0 s nd s in the prt of the input seen so fr. M = ({q 0, q, q 2 }, q 0, {q 2 }, {Λ, 0,, #}, {0, }, Λ, δ) The mchine strts in stte q 0. If the input string is empty, it goes directly to ccepting stte q 2. If the input string is non-empty, the mchine writes # on the second tpe nd goes to stte q. for ech 0 on tpe, the mchine moves left on tpe 2 for ech on tpe, the mchine moves right on tpe 2 If fter the whole word is scnned on tpe, the hed of tpe 2 is on symol #, the mchine ccepts y going to ccepting stte q 2 ; otherwise it rejects. δ(q 0, [Λ, Λ]) = (q 2, (Λ, R), (Λ, R)) δ(q 0, [0, Λ]) = (q, (0, R), (#, L)) δ(q 0, [, Λ]) = (q, (, R), (#, R)) δ(q, [0, Λ]) = (q, (0, R), (Λ, L)) δ(q, [, Λ]) = (q, (, R), (Λ, R)) δ(q, [0, #]) = (q, (0, R), (#, L)) δ(q, [, #]) = (q, (, R), (#, R)) δ(q, [Λ, #]) = (q 2, (Λ, R), (#, R)) Prolem 9. For TM M, let E(M) denote the encoding of M. Consider the lnguge L = {E(M) M, when strted on lnk tpe eventully writes somewhere on the tpe}. (i) Show tht L is recursively enumerle. Assignment Automt, Lnguges, nd Computility
(ii) Show tht L is not recursive. Let s use nottion M from the lectures for E(M). (i) We cn construct TM M L tht recognizes L s follows. M L first trnsforms its input M to M, ɛ. M L simultes universl TM U except the following cses: s soon s U writes on the tpe, M L moves to n ccepting stte if U hlts in n ccepting stte, M L hlts in non-ccepting stte (ii) L lnkhlt = { M lnkhlt M lnkhlt (ɛ) hlts} is hlting lnguge. It is the lnguge of encodings of TMs tht hlt on the lnk tpe. L lnkhlt is not recursive (s ws proven on lectures). We show how to reduce L lnkhlt to L. Assume there is decider D such tht D( M ) hlts in n ccepting stte if nd only if M L nd otherwise hlts in non-ccepting stte. We show tht using D we cn construct D lnkhlt such tht D lnkhlt ( M lnkhlt ) hlts in n ccepting stte if nd only if M lnkhlt L lnkhlt nd otherwise hlts in non-ccepting stte. We define converter R such tht for ll mchines M lnkhlt such tht M lnkhlt L lnkhlt, it returns mchine M such tht M L. R constructs M from M lnkhlt in the following wy: All occurences of symol in the trnsitions of M lnkhlt (red nd written) re replced in M with #, where # is new symol not occuring in the tpe lphet of M. The finl sttes of M lnkhlt re not finl in M. Insted, for ech finl stte q f of M lnkhlt, for every tpe symol x, R dds trnsition δ(q f, x) = (q f,, R) to M. For ech stte q nd tpe symol x for which δ(q, x) is undefined in M lnkhlt (i.e. M lnkhlt hlts on x), R dds trnsition δ(q, x) = (q f,, R) to M. M lnkhlt eventully hlts when strted on lnk tpe if nd only if M = R(M lnkhlt ) eventully writes somewhere on the tpe. Then we cn tke D lnkhlt = D( R(M lnkhlt ) ). We know such decider does not exists ecuse L lnkhlt is not recursive. Assignment Automt, Lnguges, nd Computility 2