VIEWS: 2 PAGES: 5 POSTED ON: 2/13/2012 Public Domain
Midterm Examination 2006/12/1 Formal Language and Automata Theory 姓名:________________ 學號:_____________________ 分數:___________ 1. (6 pts) Let = {a, b}. Then 3 = {aaa,aab,aba,abb,baa,bab,bba,bbb}; + - 3* = {a,b,aa,cb,bb,ba}. 2. (10pts) Let A and B be two languages. Then which of the followings always hold? (a) (A+)* = A+ (b) (A*)+ = A+ (c) (A U A2 )⊆B => A*⊆ B. (d) A(BC) = AB AC (e) A(BC) = AB AC ANS:_________d___________________________ 3. (45 pts) Given the following NFA machine M allowing transitions : M a b c p {} p q r q p q r {} rF q r {} p (a) Draw a state transition diagram for the NFA M. [8 pts] (b) Let M = (Q, , , S, F). Then what are the contents of Q, , , S and F, respectively. [7 pts] Q = _____________________________ = _____________________ S = {p} F =__________________________ = (c) List all the strings of length ≤ 3 accepted by M. [5 pts] ANS: _c,ac,ca,bc,bb,cb,...______________________________ 1 (d) Find a right linear grammar equivalent to M. Remember to designate the start symbol; otherwise it is assumed to be the LHS of the first rule. [10 pts] Ans: p ap | bq | cr q br | aq | p r ar | cp | q | e (e) Let pe denote -closure({p}). It is known that for the machine M, pe = {p} is the -closure of p. What are the -closures of the other two states? [4pts] ANS: qe = ____{p,q}__________ re = _____{p,q,r}________ (f) Convert the automata M to a DFA M' by filling in the following table: [11pts] M' a b c Se = pe = {p} p pq pqr pq pq pqr pqr pqrF pqr pqr pqr Notes: 1. Useless states of new machine must not be listed in the table. 2. The table rows are provided to you for your convenience; you may probably need additional rows or may not use up all table rows. 3. S is the set of initial states of M and you must have known that its -closure Se is the initial state of M'. 4. Remember to mark final states. 4. [14 pts] Consider the following NFA. M: a b p q r q r p,q rF p r (a) [7pts] construct a strongly left linear grammar equivalent to M. Ans: Sr p ra | qb | e q pa | qb r pb | qa | rb 2 (b) Give a regular expression equivalent to M. [7 pts] Ans: [ab*b + (ab*a + b)b* a]* [ab*a + b] b* 5. [20pts] For any n ≥ 1, let An = { x 1 an-1 | x ∈ {0,1}* , a ∈ {0,1} } be the set of all strings of which the n-th bit counted from the right is 1. (a) Find a NFA with n+1 states accepting An. [5pts] Let M be any DFA accepting An. We want to show that M must have at least 2n states. Thus the exponential explosion of states while converting a NFA to an equivalent DFA in worst case is inevitable. The proof proceeds as follows. Let x = xn xn-1… x1 and y= yn yn-1… y1 be any two distinct strings of length n, and let p and q be the states of M that are reached when x and y are consumed by M, respectively. In other words, (s,x) = p and (s,y)=q, where s in the initial state of M. (b) Show why it must be the case that p ≠ q. [10pts] Pf: Since x ≠ y, there exist an index k such that xk ≠ yk. Without loss of generality, let xk = 0 and yk = 1. Now the string x 0n-k ∉ An since the last n-th bit of it = xk = 0 and y 0n-k ∈ An since the last n-th bit of it = yk = 1. But if p = q, we would have (s,x0n-k) = (p,0n-k) =(q,0n-k) =(s,y0n-k) and hence either both accept or both reject. This is impossible. Hence p could not be equal to q. (c) According to (b), why must M have at least 2n states? [5pts] Since there are 2n distinct strings z of length n, each reaching a distinct state (s,z) on any machine M recognizing An. As a result, M must contain at least 2n states. 6. [20pts] Let A = {x | x {a,b}* and contains as many a’s as b’s.}, and the grammar G given by: S | aSb | bSa | SS. (a) Give a parse tree of the input 'ababba'. [5pts] 3 Show that L(G) = A in two steps: (b) L(G)⊆A. hint: by induction on n that if S n then contains as many a's as b's. (c) A ⊆ L(G). hint: show that if x∈A, then there exists a derivation of it. [7pts] Ans: (b) We will show that if S * then #a( ) = #b(). As a result, if ∈ L(G), then S * and must be a terminal string, and hence belongs to A. The proof is by ind. on n such that Sn For the case that n = 0: S0 iff = S. Then #a( ) = #b() = 0. For the case n = k+1. By ind.hyp, #a(') = #b(') if Sk ' Now if Sn , It must be one of the following cases: S k S SS = . S k S aSb = . S k S bSa = . S k S = . In all cases, we can find that #a() = #b() given the ind.hyp. that #a(S) = #b(S) (c) The proof is by induction on the length of x ∈ A. If |x| = 0, then x = and S . (ok) If x = x1x2…,xn is a string of length n > 0. Then there are 3 cases to consider depending on the values of x1 and xn. Case 1: x1xn = ab. Then x2…xn-1 is a string < n and also belongs to A. Hence, by ind.hyp., S * x2 … xn-1. As a result S aSb * x1…xn = x. Case 2: x1xn = ba. Similar to Case 1. Case3: x1xn = aa or bb. Then there must exist a split of x into two nonempty substrings yz such that x = yz and both y and z are members of A. Since y and z are shorter than x, by ind.hyp., there exist derivations for either string: S * y and S * z. As a result, we can form a derivation of x as follows: SSS*yS * yz = x. 4 7. [20 pts] Let A2 = {x | x {a,b}* and contains twice as many a’s as b’s.}. (a) Using [the game-theoretical argument of ] the pumping lemma to show that A2 is not regular. Ans: The game proceeds as follows: 1. Suppose D pick a number k > 0. 2. Y choose a string xyz ∈ A2 with x = a2k, y = bk and z = . 3. Suppose D decompose y into u v w with |v| = t ≠ 0. 4. Y let i = 0. Now the resulting string xuviwz = a 2k bk - t ∉ A2, and Y wins. Since Y has a winning strategy in this game, by pumping lemma, A2 is not regular. (b) Design a context free grammar G without more than 5 rules to generate the language A2, i.e., L(G) = A2. Ans: S e | aaSb | abSa | baSa | SS. 5