SFWR ENG 2FA3. Solution to the Assignment #4 Total = 131, 100%= 115 The solutions below are often very detailed on purpose. Such level of details is not required from students solutions. Some questions have more than one solution. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. 1.[20] (a)[2] In the left automaton the states 7,8 are not accessible, in the right automaton all states are accessible. (b)[10] Left automaton: 1 2 3 4 5 2 x 3 x x 4 x x 5 x x x 6 x x x x 1
Hence the equivalence classes are {1,6},{2,5},{3,4}. Right automaton: 1 2 3 4 5 6 7 2 3 x x 4 x x 5 x x x x 6 x x x x 7 x x x x 8 x x x x x Hence the equivalence classes are {1, 2},{3, 4, 8},{5, 6, 7}. (c)[8] Minimum state left automaton Minimum state right automaton 2.[16] Consider the following context-free grammar G: R XRX S S at b bta T XT X X ε X a b Answer the following questions: 1. [1] What are the variables/nonterminals of G? Answer: N = {R, X, S, T }. 2. [1] What are the terminals of G? Answer: Σ = {a,b}. 3. [1] What is the start variable of G? Answer: This is ambiguous question with two correct answers. One is S as most textbooks say that S denotes the start 2
symbol. The other is R as customarily the start symbol is usually the top right variable. Those who will take 3RA3 next term will hear more on the requirements ambiguity problem. 4. [1] Give three strings in L(G). Answer. If S is the start symbol: ab,ba,aab; if R is the start symbol: ab,ba,aab. 5. [1] Give three strings not in L(G). Answer. If S is the start symbol: a,b,ε; if R is the start symbol: a,b,ε. 6. [1] True or false: T aba. Answer: No. 7. [1] True or false: T aba. Answer: Yes, as T XT X at X axx abx aba. 8. [1] True or false: T T. Answer: Yes, as includes the identity relation. 9. [1] True or false: XXX aba. Answer: Yes, as XXX axx abx aba. 10. [1] True or false: X aba. Answer: No. 11. [1] True or false: T XX. Answer: Yes, as T XT X XX. 12. [1] True or false: T XXX. Answer: Yes, as T XT X XXX. 13. [1] True or false: S ε. Answer: No. 14. [3] Give a description in English of L(G). Answer: If S is the start symbol, R is useless symbol and L(G) is the set of all strings over a,b that have length bigger or equal 2, and the first symbol is always different than the last one. If R is the start symbol, L(G) consists of all strings over a,b that are not palindromes. 3.[10] Give context-free grammars generating the following languages: (a) [5] (b) [5] The set of strings over the alphabet {a,b} with more a s than b s. S TaT T T T at b bta a ε T generates all strings with at least as many a s as b s, and S forces an extra a. {a m b n c p d q m + n = p + q}. S asd A B A aac C B bbd C C bcc ε For all productions, we generate one a or b on left side, and one c or d on the right side, which can guarantee that m + n = p + q. Variable A cover cases that m q, while variable B cover cases that m q. 3
4.[10] Exercise 3 (page 306 of the Kozen textbook). The simplest and most straightforward grammar G is: S ε (S) [S] SS. We have to show that L(G) = PAREN 2. First we show that L(G) PAREN 2. The proof is by induction on the length of derivation. If S x, then x = ε PAREN 2. Suppose that for every x {(,),[,]} such that if S k x, where 1 k n, then x PAREN 2 (strong induction!). Consider the case S n+1 y, where x {(,),[,]}. We have two cases: S n v(s)w y = v()w, or S n v[s]w z = v[]w, where v,w {(,),[,]}, and S k v, S m w where k < n and m < n. Hence, by inductive assumption v,w PAREN 2. If v,w PAREN 2, then by (ii) and (iii) of the definition of PAREN 2, we immediately get v()w PAREN 2 and v[]w PAREN 2. This means L(G) PAREN 2. Now we have to show that PAREN 2 L(G). The proof by the inductive definition of PAREN 2. Clearly ε L(G) so we are done with (i) of the definition of PAREN 2. Let x L(G), i.e S x {(,),[,]}. Consider (x) and [x]. We have S (S) (x), so (x) L(G). Similarly S (S) [x], so [x] L(G). Hence we are done with (ii) of the definition of PAREN 2. Let x,y L(G), i.e S x {(,),[,]} and S y {(,),[,]}. In this case: S SS xs xy, so xy L(G). So we are done with (iii) of the definition of PAREN 2. Hence PAREN 2 L(G), i.e. L(G) = PAREN 2. 5.[5] Eliminate all useless productions from the below grammar. S a aa B C A ab ε B Aa C ccd D d What language does this grammar generate? Step 1. There is only one variable from which we cannot derive a sequence of terminals, namely: C After erasing it we get: S a aa B A ab ε B Aa D d 4
Step 2. There is only one variable that does not appear in any string derived from S, namely: D After erasing it we get: S a aa B A ab ε B Aa The grammar generates the language containing only odd number of a s, i.e. {a 2k+1 k 0}. 6.[5] Eliminate all ε-productions from the below grammar. S ABaC A BC B b ε C D ε D d In the first step we conclude that B, C can eventually generate ε (they are often called nullable). Then, since A BC we conclude that A can also generate ε. It is easy to observe that only A,B,C can generate ε. Hence: S ABaC BaC AaC ABa ac Ba Aa a A BC B C B b C D D d 7.[5] Eliminate all unit-productions from the below grammar. S Aa A a bc B B A bb We have A B and B A, so actually A is the same as B. Therefore we can change: A a bc B B A bb into just 5
A a bc bb Hence the final grammar is: S Aa A a bc bb 8.[10] Convert the below grammar into Chomsky Normal Form. S abab A bab ε B BAa A ε First we have to get rid of all ε-productions. The obtained grammar is: S abab aba abb ab A bab ba bb b B BAa Aa Ba a A Next we have to get rid of all unit-productions. The result is: S abab aba abb ab A bab ba bb b B BAa Aa Ba a bab ba bb b Now, we may produce a Chomsky normal form: S X a X bab X a X ba X a X bb X a X b X a a X b b X bab X a X AB X ba X b A X bb X b B X AB AB A X b X AB X b A X b B b B X BA X a AX a BX a a X b X AB X a A X b B b X BA BA 8a.[10] Convert the below grammar into Graibach Normal Form. S ABb a A aaa b B bab bbb ABB a 6
First substitute A by aaa b in B ABB. The result is B bab bbb a aaabb bbb, i.e. B bab bbb a aaabb. Now we substitute A by aaa b in S ABb. The result is S aaabb baab a Hence now we have: S aaabb baab a A aaa b B bab bbb a aaabb which is almost Graibach. We just need to add C a a, C b b, and replace a and b that are not first by C a and C b, respectively. Final result: S ac a ABC b baac b a A ac a A b B bac b bbb a ac a ABB C a a C b b 9.[10] Construct a pushdown automaton accepting: {a n b m 2n = 3m n 0 m 0} You can choose either acceptance with the empty stack of final state. The below solution has in fact both. Note that the language in question is {ε,a 3 b 2,a 6 b 4,...}, i.e. {a 3k b 2k k 0} Thus, for each symbol a of the input, we push two copies of symbol a into the stack (this is done when in state s). Then, for each symbol b we read from the input, we match it with three a s in the stack (using states q 1,q 2,q 3,q 4 ). The automaton looks as follows: M = (Q,Σ,Γ,δ,s,,F), where Q = {s,q 1,q 2,q 3,q 4,r}, Σ = {a,b}, Γ = {a,b, }, F = {r}, and the relation δ is the following: 7
{(s, ε, ),(r, ε), just to accept empty string (s,a, ),(s,aa ), first push of aa (s,a,a),(s,aa), remaining pushes of aa, note that for example aa on the input tape results in aaaa on the stack! (s,b,a),(q 1,ε), first encounter of b (q 1,ε,a),(q 2,ε), the remaining 3 steps deal with matching 3 a s with 2 b s (q 2,ε,a),(q 3,ε), (q 3,b,a),(q 4,ε), (q 4,b,a),(q 1,ε), (q 3,ε, ),(r,ε)} just to go to the final state 10.[10] Construct a pushdown automaton accepting: {a i b i c k i, j,k 0 i + k = j} You can choose either acceptance with the empty stack of final state. The below solution has in fact both. The basic idea for this problem is to use different states to enforce the order of the occurrences of the letters a,b and c, and use the stack to meet the requirement of i + k = j.the automaton looks as follows: M = (Q,Σ,Γ,δ,s,,F), where Q = {s,q, p,r}, Σ = {a,b,c}, Γ = {a,b,c, }, F = {r}, and the relation δ is the following: {(s,a, ),(s,a ), (s,b, ),(s,b ), (s,a,a),(s,aa), (s,b,a),(q,ε), (q,b,a),(q,ε), (q,b, ),(q,b ), (q,b,b),(p,bb), (p,c,b),(p,ε), (p,ε, ),(r,ε)} 11.[10] Show that the following language is not context-free: {a i b i2 i > 0} Assume L = {a n b i2 i > 0} is context free, then pumping lemma hold. Let p be the number in pumping lemma. We take z = a p2 b p. Clearly, z p and z L. Therefore, z = uvwxy and vx 1, vwx p, and for all i 0, uv i wx i y L. Let us consider two cases, Case 1. vx = a k, i.e. it contains no b. Then, z = uv 2 wx 2 y = a q+k b p, and k1. Therefore, z / L, a contradiction. 8
Case 2. vx = a j b k, and k1, i.e. it contains at least one b. Then, consider z = uv 0 wx 0 y. We have: # a (z ) = q j = p 2 j, and 1 j p 1 (# a (z ) is the number of a in z), and # b (z ) = p k, and 1 k p. So, (# b (z)) 2 = (p k) 2 = p 2 2kp + k 2. Clearly kp j > k 2 kp, because kp j p (p 1) = 1, and k 2 kp = k(k p) 0. Now, it is easy to verify that, p 2 j > p 2 2kp + k 2. Therefore, z / L, contradiction. 12.[10] Describe a Turing Machine that accept {a 2n n 0}. Solution. The idea is the following: for each input string w 1. Sweep left to right across the tape, crossing off every other a. 2. If in stage 1 the tape contained a single a, accept. 3. If in stage 1 the tape contained more than a single a and the number of a was odd, reject. 4. Return the head to the left-hand end of the tape. 5. Go to 1. The formal description could be the following: M = (Q,Σ,Γ,,,δ,q 1,q accept,q re ject ), where Q = (q 1,q 2,q 3,q 4,q 5,t,r) Σ = {a} Γ = {a,x,, } q 1 is the start state. and the δ is given by the below table δ a x q 1 (q 1,,R) (q 1,,R) (q re ject,,r) (q re ject,x,r) q 2 (q re ject,,r) (q 3,x,R) (q accept,,r) (q 2,x,R) q 3 (q re ject,,r) (q 4,a,R) (q 5,,L) (q 3,x,R) q 4 (q re ject,,r) (q 3,x,R) (q re ject,,r) (q 4,x,R) q 5 (q re ject,,r) (q 5,a,L) (q 2,,L) (q 5,x,L) 9