HW 3 Solutions Tommy November 27, 2012 5.1.1 (a) Online solution: S 0S1 ɛ. (b) Similar to online solution: S AY XC A aa ɛ b ɛ C cc ɛ X axb aa b Y by c b cc (c) S X A A A V AV a V V b V a b X V V X V (d) S 0S01 0S10 1S00 00S1 01S0 10S0 SS ɛ 5.1.3 First, I will show that every regular expression r has an equivalent contextfree grammar. The proof is by induction on the complexity of the expression r. asis Suppose r =. Then an equivalent grammar is S r. Suppose r = ɛ. Then an equivalent grammar is S r ɛ. Suppose r = a for some alphabet symbol a. Then an equivalent grammar is S r a. Induction As our inductive hypothesis, we assume that there are equivalent context-free grammars for all regular expressions that are strictly less complex than r. 1
5.2.1 (a) Suppose r = r 1 r 2 for some regular expressions r 1 and r 2. Then an equivalent grammar for r can be constructed by combining the grammars for r 1 and r 2 with the following extra rule: S r S r1 S r2. Suppose r = r 1 +r 2 for some regular expressions r 1 and r 2. Then an equivalent grammar for r can be constructed by combining the grammars for r 1 and r 2 with the following extra rule: S r S r1 S r2. Suppose r = t for some regular expression t. Then an equivalent grammar for r can be constructed by adding the following extra rule to the grammar for t: S r S t S r ɛ. Since all regular expressions fall into one of the above six cases, we conclude that all regular expressions have equivalent context-free grammars. To see that all regular languages L are context-free, simply take a regular expression for L (guaranteed to exist) and convert it to a context-free grammar. 2
(b) (c) 5.3.1 Let w be a string of balanced parentheses. I will show by induction (on the length of w) that w is generated by the given grammar. asis Suppose w = ɛ. Since the grammar has the rule ɛ, we see that it generates w. Induction Our inductive hypothesis is that the grammar generates all 3
5.4.1 (a) strings of balanced parentheses that are strictly shorter than w. Suppose w is the concatenation of two non-empty strings w 1, w 2 of balanced parentheses. The inductive hypothesis tells us that the grammar generates both w 1 and w 2, so we see that w is generated by the rule. Suppose w = (t) for some string t of balanced parentheses. Since t is shorter than w, our inductive hypothesis tells us that t is generated by the grammar. Therefore, w is generated by the rule (). Since all strings of balanced parentheses fall into one of the above three cases, we conclude that all strings of balanced parentheses are generated by the grammar. (b) (c) and and S asbs aasbs aabs aab S as aasbs aabs aab. S asbs asb aasb aab S as aasbs aasb aab. 6.1.1 (a) Online solution: (q, 01, Z 0 ) (q, 1, XZ 0 ) (q, ɛ, XZ 0 ), (p, 1, Z 0 ) (p, ɛ, Z 0 ), (p, ɛ, ɛ) 4
(b) (q, 0011, Z 0 ) (q, 011, XZ 0 ) (q, 11, XXZ 0 ), (p, 011, Z 0 ) (q, 1, XXZ 0 ), (p, 11, XZ 0 ) (q, ɛ, XXZ 0 ), (p, 1, XZ 0 ), (p, 11, Z 0 ), (p, 1, XXZ 0 ) (p, ɛ, XZ 0 ), (p, ɛ, XXZ 0 ), (p, 1, Z 0 ), (p, 1, ɛ), (p, 1, XZ 0 ), (p, ɛ, XXXZ 0 ) (p, ɛ, Z 0 ), (p, ɛ, ɛ) (c) (q, 010, Z 0 ) (q, 10, XZ 0 ) (q, 0, XZ 0 ), (p, 10, Z 0 ) (q, ɛ, XXZ 0 ), (p, 0, Z 0 ), (p, 0, ɛ) (p, ɛ, XZ 0 ) (p, ɛ, Z 0 ) 6.3.2 The PDA has just one state q and its transition function is given by δ(q, ɛ, S) = {(q, aaa)} δ(q, ɛ, A) = {(q, as), (q, bs), (q, a)} δ(q, a, a) = {(q, ɛ)} δ(q, b, b) = {(q, ɛ)} 6.4.2 (a) (b) 5
(c) 7.1.2 (a) Ṡ AS A A aas a aa SbS A bb bs Sb b (b) Ṡ AS A A aas a aa SbS aas a aa bb bs Sb b (c) All symbols are productive and reachable. (d) Now, we Chomskylize it: Ṡ AZ A A XY a XA SV XY a XA W W W S SW b Z S Y AS X a W b V W S 6
7.1.3 (a) Ṡ 0A0 11 00 11 A C S A C S (b) Ṡ 0S0 1S1 SS 00 11 A 0S0 1S1 SS 00 11 0S0 1S1 SS 00 11 C 0S0 1S1 SS 00 11 (c) Ṡ 0S0 1S1 SS 00 11 (d) Now, we Chomskylize it: Ṡ AC D SS AA A 0 1 C SA D S 7.2.1 (a) We choose a k b k+1 c k+2 to be our string that s in the language. The adversary selects one or two places to pump (if he picks two, they have to be no more than k symbols apart): If the adversary picks... a string containing more than one type of letter, a string containing b, and possibly a string containing a a string containing b, and possibly a string containing c string(s) containing only as string(s) containing only cs pump... times, so that... Example 2 the letters aaabbbbccccc get all mixed aaababbbbccccc up. 2 #(b) #(c) aaabbbbccccc aaaabbbbbccccc 0 #(a) #(b) aaabbbbccccc aaabbb c ccc 2 #(a) #(b) aaabbbbccccc aaaaaabbbbccccc 0 #(b) #(c) aaabbbbccccc aaabbbbc c 7
(b) We choose a k b k c k to be our string that s in the language. The adversary picks one or two places to pump: If he picks a string containing more than one type of letter, we can pump it twice to mix up the order of the letters. E.g., aaabbbccc aaababbbccc. If he picks string(s) containing only cs, we pump it twice to make #(c) > #(b). E.g., aaabbbccc aaabbbccccc. If he picks one string to contain some number of as and the other string to contain an equal number of bs, then we pump zero times to make #(c) > #(b). E.g., aaabbbccc aa b bccc. In any remaining cases, the adversary will have selected a differing number of as and bs, so we pump twice (or any number of times except once) to disequalize the as and bs. E.g., aaabbbccc aaabbbbcccc. (c) For this language, we use the PLL. Since the prime numbers grow weirdly, there are no infinite subsets of this language whose strings are a constant length apart. Therefore, the language is not contextfree. (d) We can use the PLL again. In this language, every string has length equal to i + i 2 for some integer i. Since the squares grow faster than linearly, there is no infinite subset of this language whose strings are a constant length apart. Therefore, the language is not context-free. (e) We choose our string to be a k b k c 2k and repeat the case analysis from part (b). (f) First, please note that every string in this language is divided into three parts: w, w R, and then w again. Notice that each of these three parts must contain exactly the same number of 0s and exactly the same number of 1s as each other. Every string in the language has this property. Now we choose our string t = 0 k 1 2k 0 2k 1 k, which has the form ww R w if we let w = 0 k 1 k. The adversary then selects one or two places to pump. Notice that the adversary cannot select portions from all three parts of t (by three parts, I mean w and w R and w). At best, he can affect only two of them. Assume that the adversary s selection is confined to the first two parts, leaving the third part unaffected. In this case, we pump zero times, creating a new shorter string t. Since t is shorter, each of its three parts must also be shorter. This means that the number of 1s in the last part of t is still exactly k, but the number of 0s in it is now strictly less than k. Now recall the fact that each of the three parts of t must have an equal number of 0s and an equal number of 1s. In order to balance out the numbers of 0s and 1s in all three parts of t, the adversary would have needed to select some 0s in both of the first two parts without selecting any 1s. However, this is impossible because the relevant 0s are too far apart (they are separated by 1 2k ). We win. 8
What if the adversary s selection is confined to the last two parts instead of the first two parts? Don t worry; the situation is completely symmetric. What if neither of the above two cases hold? Don t worry; the adversary can t make a selection that escapes both of the previous cases. 7.2.2 (a) If the adversary picks k > 2, then we cannot pick a long enough string in the language. (b) The adversary can pick k > 2. Then we ll have to pick a string in the language, and it must have the form 0 n 1 n for some n (simply because all strings in the language have this form). Then the adversary can select two places to pump: the first will be a 0, and the second will be a 1 (e.g. 00001111). Then, no matter how many times we pump, the number of 0s will remain equal to the number of 1s and the string will remain in the language. (c) The adversary can pick k > 2. Then we ll have to pick a palindrome w. If w has odd length, then the adversary can simply pick the middle digit; no matter how many times we pump, the string remains a palindrome. If w has even length, then the adversary can pick the two middle digits, which of course must be the same digit if w is a palindrome. Again, no matter how many times we pump, the string remains a palindrome. 7.4.3 Here s the grammar again: S A C A A a CC b C A a (a) Online solution in a different format: (b) Substring a b ab ba aba bab abab baba ababa Symbols A C S A S A C Since the last line contains the letter S, we see that ababa is in the language. 9
(c) Substring a b ba aa ab baa aaa aab baaa aaab baaab Symbols A C S A S A C S C Since the last line contains the letter S, we see that baaab is in the language. Substring a b aa ab ba aab aba bab aaba abab aabab Symbols A C S A S A C Since the last line contains the letter S, we see that aabab is in the language. 10