Solution to CS375 Homework Assignment 11 (40 points) Due date: 4/26/2017 1. Find a Greibach normal form for the following given grammar. (10 points) S bab A BAa a B bb Ʌ Solution: (1) Since S does not occur on the right hand side of any productions, we don t need to create S S in this case. (2) There is a Λ-production in the given grammar B Λ, so we need to remove this Λ-production. Since B is the only non-terminal that derives Λ, after removing B Λ, the grammar is: S bab ba A BAa Aa a B bb b (3) There are no unit productions in the grammar, this step is not needed. (4) There is a direct left recursion in the grammar A Aa a, but no indirect left recursion. After transforming A Aa a to A ac C ac Ʌ we have S bab ba A BAa ac C ac Ʌ B bb b Since the language does not contain Ʌ, we have to eliminate Ʌ-production again. After eliminating Ʌ-production, we have S bab ba A BAa ac a C ac a B bb b (5) Replace B in A BAa ac a with bb b to obtain A bbaa baa ac a. Replace B in S bab ba with bb b to obtain S babb bab ba. We have S babb bab ba A bbaa baa ac a C ac a B bb b
Finally add the new productions D a and E b and make appropriate replacements to obtain the proper form: S baeb bae ba A bbad bad ac a C ac a B bb b D a E b 2. Use the Pumping Lemma to show that the following language is not contextfree. (10 points) L = {a i b j c k 0 < i < j < k} Proof: Given the language L = { a i b j c k 0 < i < j < k }, assume, BWOC, that L is context-free. L is infinite, so Pumping Lemma applies. Choose z= a m b m+1 c m+2 where m is the positive integer from the lemma. Then z= a m b m+1 c m+2 = uvwxy where 1 vx vwx m and uv k wx k y L for all k N. First, note that neither v nor x can contain distinct letters. For example, if v = a b, then we have v 2 = a b a b, which cannot appear as a substring of any string in L. So v and x must be strings of repeated occurrences of a single letter. Second, note that since vwx m, vwx cannot extends from a m into c m+2. For, otherwise, as the following figure shows, the length of vwx would be bigger than or equal to m+3, a contradiction. So we only have 5 cases to consider: (1) v and x both occur in a m (2) v and x occur in a m and b m+1, respectively.
(3) v and x both occur in b m+1 (4) v and x occur in b m+1 and c m+2, respectively. (5) v and x both occur in c m+2 However, each of these would lead to a contradiction. So the given language is not context-free language. In the following, we will only consider the first two cases. The other three cases can be proved similarly. Case (1) is shown in the following figure. Each of u, v, w and x is a substring of a m. Let u = a p p 0 v = a i i > 0 w = a q q 0 x = a j j > 0 y = a m p i q j b m+1 c m+2 But then for k=2, we have uv 2 wx 2 y = a p a 2i a q a 2j a m p i q j b m+1 c m+2 = a m+i+j b m+1 c m+2 L a contradiction! Case (2) is shown in the following figure. Here v is a substring of a m and x is s substring of b m+1. Let v = a i i > 0 x = b j j > 0
y = b q c m+2 q 0 w = a p b m+1 j q p 0 u = a m i p Then for k = 2, we have uv 2 wx 2 y = a m i p a 2i a p b m+1 j q b 2j b q c m+2 = a m+i b m+1+j c m+2 L a contradiction! (Note: if you are careful enough, you would notice that the precise requirement is actually i+j > 0. But even with that requirement, the above proof is still true. Why?) 3. Tell if it is possible to construct a Turing machine to move an input string over {a, b} to the right two cells position. Assume that the tape head is at the left end of the input string if the string is not empty. The rest of the tape cells are blank. The machine is supposed to move the entire string to the right two cells position, leaving all remaining tape cells blank. If your answer is YES, show the instructions for such a machine. Otherwise, explain why it is not possible. (10 points) Solution: The answer is YES. Option 1: First, note that it is possible to build a TM to move an input string over {a, b} to the right one cell position. The instruction set of such a TM is as follows, with 0 being the start state:
So, to move an input string over {a, b} to the right two cells position, we can use the above process twice. Option 2: It is also possible to move a string directly to the right two cells position. The instruction set is as follows.
The following shows the execution of the above TM to move the string ababb to the right two cells position:
4. Construct a Turing machine to add 2 to a natural number represented as a binary string. (2.5 points) Solution: The instruction set of the TM is shown below. The start state is 0.
The state transition diagram of the TM is as follows. In the following, we use examples to show the function of each instruction by showing the instruction on the left (framed in blue), an example before and after the execution of the instruction on the right, and the corresponding state transition diagram at the lower left corner.