Solutions to Assignment 5 - Computer Science to Assignment 5 October 27, 2000 Exercise 1 (40 points)...
Click here to load reader
Transcript of Solutions to Assignment 5 - Computer Science to Assignment 5 October 27, 2000 Exercise 1 (40 points)...
Solutions to Assignment 5
October 27, 2000
Exercise 1 (40 points) Give context-free grammars generating the following languages.
1. The set of strings over the alphabet Σ = {a, b} with twice as many a’s as b’s.
2. The complement of the language {anbn | n ≥ 0}.
3. {w#x | wR is a substring of x for w, x ∈ {0, 1}}.
4. {w | w = wR for w ∈ {0, 1}∗}.
Solution We present the context-free grammar rules for each case.
1.
S → SS | aaSb | abSa | baSa | ε
2.
S → T | aU | V bT → aT | Ta | bT | Tb | baU → aU |WV → V b |WW → aWb | ε
3.
S → S0 | S1S → TT → 0T0 | 1T1T → UU → U0 | U1 | #
4.
S → 0S0 | 1S1 | 0 | 1 | ε
Exercise 2 (15 points) Give a context-free grammar that generates the language.
A = {aibjck | i, j, k ≥ 0 and either i = j or j = k}
Is your grammar ambiguous? Why or why not?
1
Solution Let G = (V,Σ, S, R), where V = {S,U, V,X, Y }, Σ = {a, b, c} and R consists of thefollowing rules.
S → U | VU → aU | XV → V c | YX → bXc | εY → aY b | ε
The context-free grammar G is ambiguous. For instance, we have S ⇒ U ⇒ aU ⇒ abUc ⇒ abcand S ⇒ V ⇒ V c⇒ aY bc⇒ abc, which are two different derivations of abc.
Exercise 4 (35 points) Use the pumping lemma to show that the following languages are notcontext-free.
1. (15 pts) L1 = {0n#02n#03n | n ≥ 0}
2. (20 pts) L2 = {w#x | w is a substring of x for w, x ∈ {0, 1}}.
Solution Assume L1 is context-free. Let p be its pumping length. and s = 0p#02p#03p. By thepumping lemma for context-free languages, we have s = uvxyz satisfying the following.
• uvixyiz ∈ L1 for i = 0, 1, 2, . . ., and
• |vy| > 0, and
• |vxy| ≤ p.
We do a case analysis here.
• Either v or y contains #. Then uvvxyyz contains more than two #’s, which contradictsuvvxyyz ∈ L1.
• v = 0k1 and y = 0k2 for some k1, k2 ∈ N . We have k1 + k2 > 0 since |vy| > 0.
– vxy occurs before the second # in s. Then there are 3p+ k1 + k2 0’s in uvvxyyz beforethe second # and only 3p 0′s after, implying uvvxyyz 6∈ L1.
– vxy occurs after the first # in s. Then there are 5p + k1 + k2 0’s in uvvxyyz after thefirst # and only p 0′s before, implying uvvxyyz 6∈ L1.
Therefore, L1 is not context-free.Assume that L2 is context-free. Let p be the pumping length of L2 and s = 10p#10p. By the
pumping lemma for context-free languages, we have s = uvxyz satisfying the following.
• uvixyiz ∈ L1 for i = 0, 1, 2, . . ., and
• |vy| > 0, and
• |vxy| ≤ p.
We do a case analysis here.
• Either v or y contains #. Then uvvxyyz contains more than one #’s, which contradictsuvvxyyz ∈ L1.
2
• vxy occurs before #. Then uvvxyyz = w1#w2 for some w1, w2 such that w1 is longer thanw2. Therefore, w1 cannot be a substring of w2, contradicting uvvxyyz ∈ L2.
• vxy occurs after #. Then uxz = w1#w2 for some w1, w2 such that w1 is longer than w2.Therefore, w1 cannot be a substring of w2, contradicting uxz ∈ L2.
• x straddles #. We have two subcases.
– y begins with 1. Then uxz = w1#w2 for some w1, w2 such that w1 begins with 1 and w2
consists of 0’s. Therefore, w1 cannot be a substring of w2, contradicting uxz ∈ L2.
– y = ε. Then uvvxyyz = w1#w2 for some w1, w2 such that w1 is longer than w2. There-fore, w1 cannot be a substring of w2, contradicting uvvxyyz ∈ L2.
Therefore, L2 is not context-free.
3