Final Examination Formal Language and Automata Theory Name :________________ Reg.No.:__________________ 2011/12/23 Score:______________ 1. [20pts] Let L2 = { x {a,b}* | #a(x) mod 2 = 1 and #b(x) mod 2 = 0 }. (a) Find a Strongly right linear grammar G1 to generate L2. (b) Find a strongly left linear grammar to G2 generate L2. Hint: You can draw a FA for L2 first and then translates the FA to G1 and G2. 2. [15 pts] Let G be the context free grammar : S | SS | a S b | b S a (a) Show that for all sentential form x ∈{a,b,S}*, if S * x then #a(x) = #b(x). Hence, if x ∈ L(G), then #a(x) = #b(x). (b) Show that for all string x ∈{a,b}*, if #a(x) = #b(x) then S * x . Hence L(G) = {x | #a(x) = #b(x) }. Hint: Both can be proven by induction. ANS: (a) pf: The proof is by induction on the number k such that S k x. Basis case: k = 0. Then x = S (not ) and #a(x) = 0 = #b(x). Ind. case: k = t + 1 > 0. Then we must have S t S = x) where S is a grammar rule. By ind.hyp., we have #a(S) = #b(S). In other words, #a() = #b() There are 4 cases to consider depending on the last rule S applied. case1,2: = or SS, then #a(x) = #a() = #a() =#b() = #b() = #b(x) case3,4: = aSb or bSa, then #a(x) = #a() = 1+ #a() =1+ #b() = #b() = #b(x). (b). The proof is by induction on the length |x| of x. Basis case: |x| = 0. Then x = and we have S1x by applying the rule S . Ind. case: |x| = t + 1 > 0. The there are 4 cases to consider: case 0: #a(x) #b(x). Then the implication trivially holds. case 1: #a(x) = #b(x) and x = ayb or bya, starting and ending with different symbol. Then we must have |y| < |x| and #a(y) = #b(y). Hence, by ind. hyp., S * y. So we have S a S b * ayb = x if x = ayb or S b S a * bya = x if x = bya. case 2: #a(x) = #b(x) and x = aya. Now for each prefix z of y, let d(z) = #b(z) - #a(z). 1 Obviously, d(z) = 0 if z = and d(z) = 2 if z = y. Hence there must exist a prefix u of y= uv such that d(u) = 1. But then we have x = auva with #a(au) = #b(au), #a(vu) = #b(vu), |au|< |x| and |va| < |x|. Hence, by ind. hyp., S * au and S * va. As a result, we have S SS * au S * au va = x. case 3: #a(x) = #b(x) and x = byb. Similar to case 2. Omitted here. 3. [20 pts] Give context free grammars for each of the following languages: (a) { x ∈ {a,b}* | x is a palindrome, i.e., x = rev(x) .} (b) { x ∈ {a,b}* | #a(x) = 2 #b(x) } . (c) { xy ∈ {a,b}* | |x| = |y| but x y } [10pts] . ANS; (a) S | a | b | aSb | bSa (b) S e | b S a S a | a S b S a | a S a S b (c) Note that a string z belongs to the set iff z = uavsbt or ubvsat for some u,v,s,t with |u|=|v| and |s|=|t|. We thus have S A B | B A A a | TAT B b | TBT T a | b. 4. (15 pts) We want to apply the game-theoretic version of the pumping lemma for CFL to show that the language L = {xy ∈ {0,1}* | x ∈ {0,1}* and y is the complement of x } is not context-free. Fill in the following procedure to derive a winning strategy for Y: (a) Suppose D pick a number k > 0. (b) Y choose a string z = _1K0K1K0K1K0K__. What is the restriction the selected z must obey? _________z ∈ L and |z| k.________ (c) Suppose D decompose z into uvwxy. What are the constraints that decomposition uvwxy must satisfy : ___|vwx| ≤ k and |vx| 0___ (d) How should Y choose i : _______let i = 0_______ Under what condition will we say that Y wins the game? __________uviwxiy L.__________________________ Explain why Y always win the game according to the strategy you wrote at step (b) and (d). ANS: If vwx contains only one kind of symbols (all 0’s or all 1’s), then #0(uviwxiy) #1(uviwxiy). Hence uviwxiy L. Reconsider the string z = 1K A 0K B 1K C 0K D 1K E 0K__where A ~ E are boundary positions the neighbors of which are distinct symbols. If vwx contains both 0 and 1, then it must cross one and only one of the five boundary positions. case 1: if vwx crosses A, then the neighboring blocks of A of 1’s and 0’s both diminish, but the corresponding neighboring blocks of D of 0’s and 1’ does not diminish simultaneously. Hence the resulting string uviwxiy L. case 2: vwx corsses D. Similar to case 1. case 3: if vwx crosses E, then the neighboring blocks of E of 1’s and 0’s both diminish, but the corresponding neighboring blocks of B of 0’s and 1’ does not diminish simultaneously. Hence the 2 resulting string uviwxiy L. case 4: vwx corsses B. Similar to case 3. case 5: vwx corsses C. Then the neighboring blocks of C of 1’s and 0’s both diminish, but the corresponding first blocks of 0’s and corresponding last block of 1’ does not diminish. Hence the resulting string uviwxiy L. Since in all cases, we have uviwxiy L, Y always wins the game. 5. [15 pts] Let M = ({q}, {a, b}, {A, B, S}, , q, S, {q}) be a single-state NPDA which accept by empty stack. PDA M has q as the start symbol and S as the bottom stack symbol and the transition relation is given in the following table: top of stack S A B (state, input) (q,a) (q,B) (q,S) (q,),(q,BA) (q,b) (q,A) (q,),(q,AB) (q,S) Find a context free grammar G which is equivalent to M, ANS: S aB | bA A aS | b | bAB B a | aBA | S. 6. [10pts] Given the following Grammar G3: SASA | aB | a AB|S| (a) List all symbols which are nullable: ___A,B_______________________ (b) Find a grammar G4 containing no -rule such that L(G4) = L(G3) –{}. (c) Find a grammar G5 containing no unit rule such that L(G5) = L(G4) ANS; (b) S ASA | aB | a | AS | SA | S | b (c) S ASA | aB | a | AS | SA | b A ASA | aB | a | AS | SA | b B b. A B | S | b B b. 7. [5 pts] Let G be the CFG : S aAc | aSc A b | bA Find a grammar equivalent to G which is in Chomsky normal form (cnf). aA □ c | □ aS □ c ANS: S □ b A Ab | □ aA □ aS □ a □ b □ c □ a A □ a S □ a b c 8. [20 pts] Consider the grammar: SAB | BC A BA | a B CC | b C Ab | a (a) The input ‘baaba’ is a member of L(G). Draw a parse tree for it. [5pts] 3 Bb| (b) Give a left-most derivation for the input ‘baaba’. [5pts] (c) Run the CKY algorithm on the input string ‘aabab’. Fill the results in the following table. 0a1a2b3a4b5 4 b,B 3 a,A,C 2 b,B 1 a,A,C 0 a,A,C 1 2 3 5 5 4