CSE 05 Homework 5 Due: Monday November 3, 207 Instructions Upload a single file to Gradescope for each group. should be on each page of the submission. All group members names and PIDs Your assignments in this class will be evaluated not only on the correctness of your answers, but on your ability to present your ideas clearly and logically. You should always explain how you arrived at your conclusions, using mathematically sound reasoning. Whether you use formal proof techniques or write a more informal argument for why something is true, your answers should always be well-supported. Your goal should be to convince the reader that your results and methods are sound. eading Sipser Chapter 2. Key Concepts CFG, PDA (0 points) Let P be a PDA defined as follows. Q = {q 0, q, q 2 }, Σ = {0, }, Γ = {}, F = {q, q 2 } δ(q 0, 0, ɛ) = {(q 0, )} δ(q 0, ɛ, ɛ) = {(q, ɛ)} δ(q 0,, ) = {(q 2, ɛ)} δ(q, ɛ, ) = {(q, ɛ)} δ(q 2,, ) = {(q 2, ɛ)} δ(q 2, ɛ, ) = {(q 2, ɛ)}. Describe the language accepted by P. 2. Give the state diagram of P. 3. Write down the traces of computations of 00, 00 in P. 4. Show that 00 L(P ). Solution:. Describe the language accepted by P. The language is {0 m n 0 n m}. 2. Give the state diagram of P.
3. Write down the traces of computations of 00, 00 in P. Write each computation path of 00 first. 00 00 00 0 0 00 0 q2 q2 00 0 Now we show all the computation paths for 00.
4. Show that 00 L(P ). q 0 00 ɛ q 00 ɛ q 0 00 ɛ q 0 0 q 0 q 0 ɛ q 0 00 ɛ q 0 0 q 2 0 ɛ To show that 00 L(P ), it s enough to give one computation path that halts at an accept state. q 0 00 ɛ q 0 0 q 0 q 2 q 2 ɛ ɛ 2 (8 points) Give context-free grammars for each of the following languages. To get full credit, it s enough to solve four out of six items. More, will be considered bonus points.. L = { 2m 0 m m 0} 2. L = {0, } \{ 2m 0 m m 0} 3. L = {0, } \{ww w {0, } } 4. L = {w Number of 0 s is exactly twice the number of s} 5. L = {wz w, z {0, }, w = z, w z} 6. L = {x i y j z k i, j, k 0 and i + j = k} Solution: In all the solutions the upper case letters are variables and S is the starting variable. The lower case letters are terminals.. L = { 2m 0 m m 0} S ɛ S0 2. L = {0, } \{ 2m 0 m m 0} One can write the language L as the union of two languages L = { 0 } c { m 0 n m 2n}. We write a CFG for each of these two languages separately and finally combine them to get a CFG for L. L = { 0 } c Notice that a regular expression for L is (0 ) 0(0 ). Let S be the starting variable in the CFG.
S X0X X ɛ X 0X Now we write a CFG for L 2 = { m 0 n m 2n}. Let S 2 be the starting variable. S 2 S 2 0 P T P 0 0P T P Now we construct the union of these two CFGs. Let S be the starting variable. 3. L = {0, } \{ww w {0, } } S S S 2 S X0X X ɛ X 0X S 2 S 2 0 P T P 0 0P T P Note that all strings of odd length are in L Any even length string in L can be divided into two odd length strings such that one has a 0 in the center and the other has a in the center. S XY Y X X Y X 0 ZXZ Y ZY Z Z 0 4. L = {w Number of 0 s is exactly twice the number of s} 5. L = {wz w, z {0, }, w = z, w z} S ɛ SS 00S 0SS0 S00 Note that this language is exactly the even length strings in language of item 5. Any string in this language can be written as concatenation of two odd length strings such that one has a 0 at the center and the other has a at the center. 6. L = {x i y j z k i, j, k 0 and i + j = k} S XY Y X X 0 ZXZ Y ZY Z Z 0 S xsz T T ysz ɛ
3 (2 points) Construct push-down automata recognizing the following languages.. L = {2m 03m m 0} 2. L = {w {), (} w is a valid string of parentheses.}. For example ()(()) L, )() / L. 3. L = {xi y j z k i, j, k 0 and i + j = k} 4. L = {xi y j z k i 6= j or j 6= k} Solution:. L = {2m 03m m 0} 2. L = {w {), (} w is a valid string of parentheses.}. For example ()(()) L, )() / L. 3. L = {xi y j z k i, j, k 0 and i + j = k}
4. L = {xi y j z k i 6= j or j 6= k} 4 (0 points) Prove that the class of context-free languages is closed under concatenation. Solution: Suppose we have two context-free languages L and L2. We know that there are CFGs G = {Σ,, S, V } for L and G2 = {Σ2, 2, S2, V2 } for L2. Suppose that the variables V of G are distinct from the set of variables V2 of G2. Moreover assume that the starting variable of G is S and the starting variable of G2 is S2. Then we define a new variable S as the starting variable of our CFG G = {Σ, V, S, } for L = L L2 as follows. = 2 {S S S2 } V = V V2 {S} Σ = Σ Σ2
Now we prove the construction is correct. Let L be the language defined by the CFG above. We have to prove L = L L 2. First, suppose that there is x L. Therefor there is a computation path for constructing x via the CFG G. However, the only rule that would produce x is S S S 2. This means x could be written as x x 2 such that S produces x and S 2 produces x 2. However, since the variables in the rules involving S are disjoint from the variables in the rules involving S 2 ; any output of S is in L and any output of S 2 is in L 2. This means that the x L and x 2 L 2, therefore, x L L 2. Secondly, suppose x L L 2. We show that x L as well. We can write x = x x 2 such that x L, x 2 L 2. Therefor, there is a computation path in the CFG for L such that S produces x and similarly, S 2 produces x 2. Now we can apply the rule S S S 2 to produce the desired string x. 5(bonus) (0 points) ead theorem 2.34 from the book (pumping lemma for context-free languages). Then prove that the following language is not context-free. L = {ww w w {0, } } Solution: Toward contradiction, assume that the language L is context-free. Therefore pumping lemma should apply to L. Let p be the pumping length, and choose the string s = (0 p p )(0 p p ) ((0 p p ) = 0 p 2p 0 2p p. Suppose that we have an arbitrary partition s = uvwxy such that. vwx < p 2. uv > 0 We show that there is an i 0 such that uv i wx i y / L. There are a few possible cases to consider. Case. vx has at least a 0. Since we know vwx < p, the substring vwx can not contain 0 s from both sides of 2p. If vwx contains 0 s from the left side, then pick i = 2. This increases the number of 0 s on left side. We show this implies the new string uv 2 wx 2 y / L. Suppose uv 2 wx 2 y = 0 m 2p+k 0 2p p for m > p and k 0. We show this string can not be written in the form ww w. Note that any suitable w must contain the entire substring 0 m and some s. This would require have in total 3m many 0 s in the entire string. But we simply don t have this many 0 s. On the other case, assume that vwx contains some 0 s on the right side of 2p. Again by a similar argument we have uv 2 wx 2 y / L. Case 2. vx has at least a. In this case the argument is similar to the above.