You are on page 1of 4

CS 360 Introduction to the Theory of Computing

Spring 2008

Assignment 2 Solutions
1. Prove that the following languages are not regular: 1. A = {x {0, 1} : the length of x is odd, and its middle symbol is 1} Solution: We will use the pumping lemma to show A is non-regular. Assume A is regular. Then there exists a pumping length n 1 such that every string w A, with |w| n, can be written as w = xyz, where |xy| n, |y| > 0, and xy i z A for all i 0. Let w = 0n 10n , |w| = 2n + 1 > n. Since w has odd length, and its middle symbol is 1, its in A. Then consider the decomposition of w as described in the pumping lemma: y is a nonempty susbstring of the rst n characters of w. Since w starts with n zeroes, we are guaranteed that y = 0k for some 1 k n. Then xy 0 z = xz = 0nk 10n must be in A. However, n > n k (since k > 0), and therefore, the middle symbol of the string is not 1 (or, if k = 1, then the string is not of the odd length), so x A. / Contradiction. A is not regular. 2. B = {0n 1m : n, m 0 are integers with n = m} Solution 1: We will use the pumping lemma to show B is non-regular. Assume B is regular. Let w = 0n 1n+n! , |w| = 2n + n! > n. Since n + n! = n, w is in B. Then consider the decomposition of w as described in the pumping lemma: y is a nonempty susbstring of the rst n characters of w. Since w starts with n zeroes, we are guaranteed that y = 0k for n! some 1 k n. Then xy k +1 z = 0n+n! 1n+n! must be in A. However, this string contains the same number of 0s and 1s, so its not in the language. Contradiction. B is not regular. Solution 2: Assume B is regular. Since regular languages are closed under complement, B is regular. Consider the regular language L = 0 1 (it has a regular expression). Consider the language Lnn = B L = {0n 1n : n 0}. Since regular languages are closed under intersection, it should also be regular. However, in class it was shown that Lnn is not regular, due to the pumping lemma. Contradiction. B is not a regular language.

3. C = 0n : n = 2k for some integer k 0 Solution: We will use the pumping lemma to show C is non-regular. Assume C is regular. Let w = 02 , w C, |w| = 2n > n, for n 1. Then consider the decomposition of w as described in the pumping lemma: y is a nonempty susbstring of the rst n characters of w. Since w starts with n zeroes, we are guaranteed that y = 0k for some 1 k n. n Then xy 2 z = 02 +k must be in C. However, 2n + k must be a power of 2. Smallest number that is a power of 2, and is greater than 2n , is 2n+1 . Therefore, k must be at least 2n , but k n < 2n . Contradiction. C is not regular. 2. Prove that the following languages are context-free: 1. B = {0n 1m : n, m 0 are integers with n = m} Solution: S XY | Y Z X 0X | 0 Z 1Z | 1 Y 0Y 1 | 2. D = B Solution: SX|Y Y 0Y 1 | X Z10Z Z 0Z | 1Z | 3. E = {x { ( , ) } : the parentheses in x are not balanced} Solution: S B)A | A(B B (S) | SS | A (A | )A |
n

3. In the lecture, we saw the following example of a CFG, which we will call G: S 0S1 | 1S0 | SS | It is the case that L(G) = {x {0, 1} : x has an equal number of 0s and 1s} . 2

It is also the case that G is ambiguous. (There are multiple parse trees for , for instance.) Give an unambiguous CFG H such that L(G) = L(H). You do not need to give a formal proof that your grammar H is unambiguous, but you should include a couple of sentences explaining why, at an intuitive level, you believe it to be unambiguous. Solution: S 0A | 1B | A 1S | 0AA B 0S | 1BB

The idea is to generate minimal strings with equal number of 0s and 1s. Each such string either starts with 0 and must end with 1, or it starts with 1 and must end with 0. This grammar is unambiguous since its LL(1). 4. Convert the following CFG to Chomsky Normal Form: S ASA | A | 00 A 01 | A0 | B1 B AAB | Solution: Eliminating -productions: S ASA | A | 00 A 01 | A0 | B1 | 1 B AAB | AA Eliminating unit-productions: S ASA | 01 | A0 | B1 | 1 | 00 A 01 | A0 | B1 | 1 B AAB | AA Adding rules for the terminals that appear inside of right hand-side productions of length more than one: S ASA | X0 X1 | AX0 | BX1 | 1 | X0 X0 A X0 X1 | AX0 | BX1 | 1 B AAB | AA X0 0 X1 1

Adding intermediate rules for productions of length 3: S AY | X0 X1 | AX0 | BX1 | 1 | X0 X0 A X0 X1 | AX0 | BX1 | 1 B AZ | AA X0 0 X1 1 Y SA Z AB

You might also like