ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK 5 april 23 Master Edition
CONTEXT FREE LANGUAGES & PUSH-DOWN AUTOMATA CONTEXT-FREE GRAMMARS, CFG Problems Sudkamp Problem. (3.2.) Which language generates the grammar G given by the productions S asa aba B bb b Problem 2. (3.2.2) Find a CFG that generates the language: Problem 3. (3.2.4) Find a CFG that generates the language Problem 4. (3.2.5) Consider the grammar What language does it generate? L(G) = { a n b m c m d 2n n, m > }. L(G) = { a n b m n m 2n}. S abscb λ B bb b Problems Lewis-Papadimitriou Construct context free grammars to accept the following languages. Problem 5. Problem 6. (2.4b) {w w starts and ends with the same symbol} (2.4c) {w w is odd} Problem 7. (2.4d) {w w is odd and its middle symbol is } 2
Problem 8. {w#x w R is a substring of x, where w, x {a, b} * } Problem 9. { n n n>} U { n 2n n>} Problem. { i j 2 k i j or j k} Problem. Binary strings with twice as many s as s. Problems Linz Problem 2. (34/8c): Find a Context-Free Grammar for the following language: Problem 3. (34/8c): Ambiguity L = {a n b m c k : k = n + m } L = {a n b m c k : k n + m } (Variation on a theme). Problem 4. Explain why the grammar below is ambiguous. S A B A AA S B BB S Problem 5. Given the following ambiguous context free grammar S Ab aab A a Aa B b (a) Find the string s generated by the grammar that has two leftmost derivations. Show the derivations. (b) Show the two derivation trees for the string s. (c) (d) Find an equivalent unambiguous context-free grammar. Give the unique leftmost derivation and derivation tree for the string s generated from the unambiguous grammar above. ATT REDOVISA ORDINARIE REDOVISNING Problem 4 &Problem 8 ATT REDOVISA EXTRA REDOVISNING Problem 9 &Problem 5 3
PUSH-DOWN AUTOMATA, PDA Sudkamp Problem. CH8 8..3 Let M be the PDA that accepts even-length palindromes over {a, b}. That is, L(M) = { w w R w {a, b}*} Where non-determinism allows the machine to guess when the middle of the string has been reached. b λ/b a λ/a b B/λ a A/λ q λλ/λ q which in Linz notation looks like this: b, λ B a, λ A b, B λ a, A λ λ, λ λ q q (a, b/c stands for a, b c) a) Give the transition table of M. b) Trace all computation of the strings ab, abb, abbbb in M. c) Show that aaaa, baab L (M). d) Show that aaa, ab L (M). 4
Problem 2. CH8 3k Construct a PDA that accepts the set of palindromes over {a, b}. Problem 3. CH8 5a Let M be the PDA in example 8... the PDA that accepts the language R L = { wcw w { a, b} }. The stack is used to record the string w as it is processed. Stack symbols A and B represent input a and b respectively. b λ/b a λ/a q c λ/λ b B/λ a A/λ q A successful computation records the string w on the stack as it is processed. Once the c is encountered, the accepting state q is entered and the stack contains a string representing w R. The computation is completed by matching the remaining input with the elements of the stack. Trace the computation that accepts bbcbb. 5
NPDA EXAMPLES Sipser Construct non-deterministic pushdown automata to accept the following languages. (Note: ε has the same meaning as λ) Example. { n n n > } Example 2. { n 2n n } Example 3. { n n n>} U { n 2n n } Example 4. (+)* - {ww w in {,}*} (complement of ww) Linz Example 5. L = {a n b 2n : n } Example 6. Example 7. L = {ωσω R : ω {a,b}*, σ {a,b}} Construct the NPDA corresponding to the grammar S aabb aaa A abb a B bbb A (Problem 5 on page 94, Linz) DPDA EXAMPLES Sipser Construct deterministic pushdown automata to accept the following languages. Example 8. { n n n>} U { n 2n n>} Example 9. Binary strings that contain an equal number of s and s. Example. Binary strings with twice as many s as s. 6
Example. Binary strings that start and end with the same symbol and have the same number of s as s. Linz Example 2. Show that L = {a n b m : m n + 2} is deterministic. (Problem 2 on page 99.) Problems Linz Problem 6. (34/8c): Find a Context-Free Grammar for the following language: L = {a n b m c k : k = n + m } Problem 7. (34/8c): L = {a n b m c k : k n + m } (Variation on a theme). Ambiguity Problem 8. Explain why the grammar below is ambiguous. S A B A AA S B BB S ATT REDOVISA ORDINARIE REDOVISNING Example 6 & Example 8 ATT REDOVISA EXTRA REDOVISNING Problem 2 &Problem 7
PUMPING LEMMA: CONTEXT-FREE OR NOT? Problem. Show that the following language on Σ = {a, b, c} is not context-free. L = {a n b j c k : k = jn}. Problem 2. Show that the following language on Σ = {a, b} is not context-free: L = { ww R a w : w {a, b}*} Problem 3. (CH8 7c Sudkamp) Using the pumping lemma prove that the language below is not context-free. L i 2i = { a b a i i } Problem 4. Problem 5. (CH8 8a Sudkamp) Prove that the language i 2i j L = { a b c i, j } is context-free. (7 page 22, Linz) We want to prove that the language L = {a n b n : n and n is not a multiple of 5} is a context-free language. Problem 6. { k i i j j k i, j, k > } Problem 7. {w#x w is a substring of x, where w, x are in {,}*}. Problem 8. { i i j i i, j > } Problem 9. The complement of {( n n ) m m, n > }. Problem. 2.8 a. (Lewis-Papadimitriou): A = { n n n n n >= }. Problem. 2.8 b (Lewis-Papadimitriou): B = { n # 2n # 3n n >= }. Problem 2. 2.8 c (Lewis-Papadimitriou): C = { w#x w is a substring of x, where w, x {a, b}*}. ATT REDOVISA ORDINARIE REDOVISNING Problem 4 & Problem ATT REDOVISA EXTRA REDOVISNING Problem 2 &Problem 8
SOLUTIONS CONTEXT-FREE GRAMMARS, CFG Sudkamp examples Problem. 3.2. Which language generates the grammar G given by the productions S asa aba B bb b L(G) = { a n b m a n n >, m > }. The rule S asa recursively builds an equal number of a s on each end of the string. The recursion is terminated by the application of the rule S aba, ensuring at least one leading and one trailing a. The recursive B rule then generates any number of b s. To remove the variable B from the string and obtain a sentence of the language, the rule B b must be applied, forcing the presence of at least one b. Problem 2. 3.2.2 Find a CFG that generates the language L(G) = { a n b m c m d 2n n, m > }. The relationship between the number of leading a s and trailing d s in the language indicates that the recursive rule is needed to generate them. The same is true for b s and c s. Derivations in the grammar S asdd A A bac bc 9
generate strings in an outside-to-inside manner. The S rules produce the a s and d s while the A rules generate b s and c s. The rule A bc, whose application terminates the recursion, ensures the presence of the substring bc in every string in the language. Problem 3. 3.2.4 Find a CFG that generates the language L(G) = { a n b m n m 2n}. S asb asbb λ The first recursive rule of G generates a trailing b for every a, while the second generates two b s for each a. Thus there is at least one b for every a and at most two, as specified in the language. Problem 4. 3.2.5 Consider the grammar S abscb λ B bb b What language does it generate? The recursive S rule generates an equal number of ab s and cb s. The B rules generate b +. In a derivation, each occurrence of B may produce a different number of b s. For example in the derivation S abscb ababscbcb ababcbcb ababcbcb ababcbcbb ababcbcbb, the first occurrence of B generates a single b and the second occurrence produces bb. The language of the grammar consists of the set L(G) = { (ab) n (cb m ) n n, m > }.
Problems Lewis-Papadimitriou Construct context free grammars to accept the following languages. Σ = {, } Problem 5. (2.4b) {w w starts and ends with the same symbol} S A A A A A λ Problem 6. (2.4c) {w w is odd} S A A S S λ Problem 7. (2.4d) {w w is odd and its middle symbol is } S S S S S Problem 8. {w#x w R is a substring of x, where w, x {a, b} * } The following grammar generates language L, where S is the start variable. S AT A aaa bab #T T at bt λ The strings in the language have the form w#uw R v, where u and v are strings of the form (a + b)* (any string made from symbols a and b). The variable T generates the strings u and v, while variable A generates the string w#uw R and the variable S generates the desired string w#uw R v.
Problem 9. { n n n>} U { n 2n n>} S A B A A λ B B λ Problem. { i j 2 k i j or j k} S AC BC DE DF A A A B B B C 2 2C D D E E E2 F 2 F2 F2 Problem. Binary strings with twice as many s as s. S λ SSS SSS SSS Problem 2. Find a Context-Free Grammar for the following language: L = {a n b m c k : k = n + m } 2
Let G be the grammar with productions: S asc B B bbc λ Claim: L(G) = L Proof: Consider the following derivation: S * a n Sc n a n Bc n * a n b m Bc m c n a n b m (n + m) c (where the first * applies S asc n times, the second B bbc m times) Since all words in L(G) must follow this pattern in their derivations, it is clear that L(G) L Consider w L, w = a n b m c (n+m) for some n, m The derivation S * a n Sc n a n Bc n * a n b m Bc m c n a n b m (n + m) c clearly produces w for any n, m. L L(G) L = L(G) G is a CFG for L Problem 3. L = {a n b m c k : k n + m } (Variation on a theme). Let G be the grammar with productions: S EcC aae AU A aa λ B bb λ C cc λ E aec F F bfc λ 3
U auc V V bvc bb Note: L(E) = {a n b m c k : k = n + m } (from 4(a) in Linz book) Let L = {a n b m c k : k < n + m }, L2 = {a n b m c k : k > n + m } Claim: L(G) = L = L L2 Proof: Consider the leftmost productions following S EcC S EcC * a n b m c (n+m) cc * a n b m c (n+m) cc k C a n b m c (n+m) cc k = a n b m c (n+m+k+) (where the first * follows from 4(a), the second from k repetitions of { C cc }) Since (n+m) < (n+m+k+), L(EcC) L2 L 4(a)) Consider the leftmost productions following S aae S aae * aa k AE aa k E * aa k a n b m c (n+m) c = a (n+k+) b m c (n+m) (where the first * follows from k repetitions of { A aa }, the second from Since (n+m+k+) > (n+m), L(aAE) L L Consider the leftmost productions following S AU S AU * a k AU a k U * a k a n Uc n a k a n Vc n * a k a n b m Vc m c n a k a n b m bbc m c n * a k a n b m bb j Bc m c n a k a n b m bb j c m c n = a (k+n) b (m+j+) c (m+n) (where the first, second, third and fourth * are k, n, m, and j repetitions of { A aa }, { U auc }, { V bvc }, and { B bb }, respectively) Since ((k+n)+(m+j+)) > (m+n), L(AU) L L 4
L(G) = L(S) = L(EcC) L(aAE) L(AU) L L2 = L Consider w L, w = a n b m c (n+m+k) for some n, m, k, with k > Then w has the leftmost derivation: S EcC * a n b m c (n+m) cc * a n b m c (n+m) cc (k-) C a n b m c (n+m) cc k(k-) = a n b m c (n+m+k) = w L2 L(G) Consider w L2, w = a (n+j) b (m+k) c (n+m) for some j, k, m, n, with j+k > If k =, w has the leftmost derivation: S aae * aa (j-) AE aa (j-) E * aa (j-) a n Ec n aa (j-) a n Fc n * aa (j- ) a n b m Fc m c n aa (j-) a n b m c m c n = a (j+n) b (+m) c (m+n) = w L (k = ) L(G) If k >, then w has the leftmost derivation: S AU * a j AU a j U * a j a n Uc n a j a n Vc n * a j a n b m Vc m c n a j a n b m bbc m c n a j a n b m bb (k-) Bc m c n a j a n b m bb (k-) c m c n = a (j+n) b (m+k) c (m+n) = w L (k > ) L(G) L L(G) L = L L2 L(G) L = L(G) Ambiguity Problem 4. Explain why the grammar below is ambiguous. 5
S A B A AA S B BB S The grammar is ambiguous because we can find strings which have multiple derivations: S A AA S B S A AA S A Problem 5. Given the following ambiguous context free grammar S Ab aab A a Aa B b (a) Find the string s generated by the grammar that has two leftmost derivations. Show the derivations. The string s = aab has the following two leftmost derivations S aab aab S AB AaB aab aab (b) Show the two derivation trees for the string s. The two derivation trees of string aab are shown below. 6
(c) Find an equivalent unambiguous context-free grammar. The equivalent unambiguous grammar is following S Ab A a Aa This grammar is not ambiguous because at any derivation step there is only one choice to make. This grammar is equivalent to the previous one, because both grammars generate the same language: all the strings that start with one or more a, and end with a single b. (d) Give the unique leftmost derivation and derivation tree for the string s generated from the unambiguous grammar above. With the new grammar the unique leftmost derivation and derivation tree of the string aab are shown below. 7
PUSH-DOWN AUTOMATA, PDA Problem. CH8 8..3 Sudkamp Let M be the PDA that accepts even-length palindromes over {a, b}. That is, L(M) = { w w R w {a, b}*} Where non-determinism allows the machine to guess when the middle of the string has been reached. b λ/b a λ/a b B/λ a A/λ q λλ/λ q which in Linz notation looks like this: b, λ B a, λ A b, B λ a, A λ λ, λ λ q q (a, b/c stands for a, b c) a) Give the transition table of M. b) Trace all computation of the strings ab, abb, abbbb in M. c) Show that aaaa, baab L (M). d) Show that aaa, ab L (M). 8
: a) Give the transition table of M. δ ( q δ ( q δ ( q, a, λ) = {[ q, b, λ) = {[ q, A]}, B]}, λ, λ) = {[ q, } δ ( q, a, A) = {[ q, } δ ( q, b, B) = {[ q, } δ a b λ q q λ /A q λ /B q λ / λ q q A/ λ q B/ λ φ Notation: The PDA consults the current state, input symbol, and the symbol on the top of the stack to determine the machine transition. The transition function δ lists all possible transitions for a given state, symbol, and stack top combination. The value of the transition function δ ( qi, a, A) = {[ q j, B],[ qk, C]} indicates that two transitions are possible when automaton is in state qi scanning an A with A on top of the stack. The transition [ q j, B] δ ( qi, a, A) [new state, new stack top] δ(current state, current input, current stack top) q i a, A B q j 9
b) Trace all computation of the strings ab, abb, abbbb in M. Computations for ab: [ q, ab, [ q, ab, [ q, ab, - [ q, b, A] - [ q, b, A], ab, - [ q, λ, BA], b, A] Rejected., λ, BA] Rejected. Rejected. Computations for abb: [ q, abb, [ q, abb, [ q, abb, [ q, abb, - [ q, bb, A] - [ q, bb, A] - [ q, bb, A], abb, - [ q, b, BA] - [ q, b, BA], bb, A] Rejected. - [ q, λ, BBA], b, BA] Rejected., λ, BBA], λ, A] Rejected. Rejected. Computations for abbb: c) Show that aaaa, baab L (M). Both aaaa, baab are accepted by this PDA, so they are in L (M). [ q, aaaa, [ q, baab, - [ q, aaa, A] - [ q, aa, AA], aa, AA], a, A], λ, Accepted. - [ q, aab, B] - [ q, ab, AB], ab, AB], b, B], λ, Accepted. 2
d) Show that aaa, ab L (M). The computations for string aaa: [ q, aaa, [ q, aaa, [ q, aaa, [ q, aaa, - [ q, aa, A] - [ q, aa, A] - [ q, aa, A], aaa, - [ q, a, AA] - [ q, a, AA], aa, A] Rejected. - [ q, λ, AAA], λ, AAA] Rejected., a, AA], λ, A] Rejected., a, Rejected. The computations for ab are shown in part b). There is none of the computations accepting the strings, so they are not in L (M). 2
Problem 2. CH8 3k Sudkamp p. 252 Construct a PDA that accepts the set of palindromes over {a, b}. : Construct the PDA: M: Q = q, q } F = { { = { a, b} Γ = { A, B} q } b λ/b a λ/a q b λ/λ a λ/λ λλ/λ b B/λ a A/λ q δ ( q δ ( q δ ( q, a, λ) = {[ q, b, λ) = {[ q, λ, λ) = {[ q, } δ ( q, a, A) = {[ q, } δ ( q, b, B) = {[ q, }, A],[ q, }, B],[ q, } 22
Problem 3. CH8 5a Let M be the PDA in example 8... the PDA that accepts the language R L = { wcw w { a, b} }. The stack is used to record the string w as it is processed. Stack symbols A and B represent input a and b respectively. b λ/b a λ/a q c λ/λ b B/λ a A/λ q A successful computation records the string w on the stack as it is processed. Once the c is encountered, the accepting state q is entered and the stack contains a string representing w R. The computation is completed by matching the remaining input with the elements of the stack. Trace the computation that accepts bbcbb. : [ q, bbcbb, - [ q, bcbb, B] - [ q, cbb, BB], bb, BB], b, B], λ, Accepted. 23
NPDA EXAMPLES (Sipser) Construct non-deterministic pushdown automata to accept the following languages. (Note: ε has the same meaning as λ) Example. { n n n>} Example 2. { n 2n n>=} Example 3. { n n n>} U { n 2n n>=} 24
Example 4. (+)* - {ww w in {,}*} (complement of ww) Linz Equivalent with Sipser Example 2, in different notation. Remark: The notation a, b/c stands for a, b c Example 5. L = {a n b 2n : n } a,a/aaa b,a/λ a,$/aa b,a/λ λ,$/$ Example 6. L = {ωσω R : ω {a,b}*, σ {a,b}} a,λ/a; b,λ/b a,λ/λ; b,λ/λ a,a/λ; b,b/λ λ,$/$ 25
Example 7. Construct the NPDA corresponding to the grammar (Problem 5 one page 94, Linz) S aabb aaa A abb a B bbb A λ,$/s λ,s/aabb λ,s/aaa λ,a/abb λ,a/a a,a/λ λ,b/bbb b,b/λ λ,$/$ DPDA EXAMPLES (Sipser) Construct deterministic pushdown automata to accept the following languages. Example 8. { n n n>} U { n 2n n>} 26
Example 9. Binary strings that contain an equal number of s and s. Example. Binary strings with twice as many s as s. 27
Example. Binary strings that start and end with the same symbol and have the same number of s as s. Example 2. Show that L = {a n b m : m n + 2} is deterministic. (Problem 2 on page 99, Linz a,λ/a b,a/λ b,a/λ b,$/$ b,$/$ b,$/$ b,$/$ Since there is a DPDA that accepts L, L is deterministic. 28
PUMPING LEMMA: CONTEXT-FREE OR NOT? Problem. Show that the following language on Σ = {a, b, c} is not context-free. L = {a n b j c k : k = jn}. Assume that L = {a n b j c k : k = jn} is a context free language. Let m m m w = a b c, w L 2 By the Pumping Lemma w can be decomposed as w = uvxyz with vxz m and vy such that uv xy z L, i i i case If i=, aaa 4243 K aab 4243 L bc L c uvxy z uv xy z = a m 2 vy b m c m L case 2 aaa 4243 K aab {{{ KbcK uv x y z If i=, uv xy z = a m 2 v b m y c m L case 3 If i=, aaa 4243 K abb{ K bbc 2 K 3c u vxy z uv xy z = a m b m 2 vy c m L case 4 aaa 4243 K abb{{{ KbbccKc u v x yz 29
If i=, uv xy z = a m b m v c m 2 y L case 5 aaa 42 4 K ab43 K4 bcc{ Kc u vxyz If i=, m uv xy z = a b m c 2 m vy L case 6 v or y containing ab or bc If I >, uv i xy i z would be a ab ba ab b c c Or a ab bc cb c Contrary to the assumption. Language is not context free. Problem 2. Show that the following language on Σ = {a, b} is not context-free: L = { ww R a w : w {a, b}*} case Assume that L = { ww R a w : w {a, b}*}is a context free language. Let w = a m b m R m m m m 2m, then ww a = a b b a a L w By the Pumping Lemma w can be decomposed as w = uvxyz with vxz m i i and vy such that uv xy z L, i a K 4243 ab K bb K4ba 4 K 24aa4 K3a uvxy z If i=, w = 2m - vy is less than w R. So uv xy z L. case 2 3
a 4243 K ab K b K4ba 4 K 24aa4K 3a uvxy If i=, w R = 2m - vy is less than w. So uv xy z L. z case 3 If i=, a K 4ab 44 Kbb 2 K 4ba 444 K3aab 4243 KK a u vxyz a w = 2 m vx < w. So uv xy z L. case 4 a K 23 abb 4243 KK bba 4243 KaaKa u vxy z If i=, ww R w = 4 m vy < 2 a = 4m. So uv xy z L. case 5 If i=, a K 4ab 4 K 2bb 4K4 3ba K a 4243 KK aaa23 K a w u w R a = 4 m vy < 2 w = 4 m. So uv xy z L. vxy z This is contrary to the assumption. Language L is therefore not context free. Problem 3. CH8 7c Sudkamp 3
Using the pumping lemma prove that the language below is not context-free. i 2i L = { a b a i i } Proof: k 2k k Assume that the language is context-free. By the pumping lemma, the string w = a b a, where k is the number specified by the pumping lemma, can be decomposed into sub-string uvwxy that i satisfy the condition length (vwx) k, length (v) + length (x) >, and uv wx Since the length (vwx) k, the vwx can have the following possibilities: Case : It contains a-s and b-s ( a i b j or b i a j, i j k i y L, ( i ). < +. Since length (vwx) k, it cannot have a-s at both side of b-s,). Then Case 2: uv 2 wx 2 y contains a b a b a. It will not be in L any more. It is contained in one of k a, k b 2 or second k a. Since the repetition in uv 2 wx 2 y will increase one of the string s lengths of a k k, b 2 or second a k, the ratio of :2: will not be satisfied. Then uv 2 wx 2 y L. There is no decomposition L that satisfies the condition of pumping lemma. Therefore, L is not context free. 32
Problem 4. CH8 8a i 2i j Prove that the language L = { a b c i, j } is context-free. Proof: A context-free language can be expressed by a context-free grammar. The context-free grammar below expresses the language L. G: S AB A aabb λ B cb λ Also, if a language L can be accepted by a PDA, it will be a context-free language. We can construct a PDA to accept language L as below. a λ/a c λ/λ λλ/λ q q λλ/λ q 3 b A/λ b λ/λ q 2 Therefore, the language L is context-free. Problem 5. 7 page 22, Linz We want to prove that the language L = {a n b n : n and n is not a multiple of 5} is a contextfree language. Consider the following two languages: L ={w : w is made from a s and b s and the length of w is a multiple of ten} 33
L 2 = {a n b n : n } Let L denote the complement of L. We have that L = L L 2. It is easy to see that L is a regular language, since we can easily build a finite automaton with states that accepts any string in this language. Since L is regular it must be that L is regular too, since regular languages are closed under complement (Theorem 4.). The language L 2 is context-free ( the grammar is: S->aSb λ). Therefore, the language L = L L 2 is also context-free, since context-free languages are closed under regular intersection (Theorem 8.5). Problem 6. { k i i j j k i, j, k > } CF. Here is a grammar that will generate the language. S A A A B B CC C D D D λ Problem 7. {w#x w is a substring of x, where w, x are in {,}*}. Not CF. Assume for the purpose of contradiction that it is. Then let the pumping length be p. Consider the string s= p p # p p, which is in the language. If we decompose s into s=uvwxy as in the statement of the pumping lemma, there are three cases to consider. If vwx is contained in the first half of the string s, then pumping up even once (that is, taking the string uv 2 wx 2 y) will give us a string where the first half is 34
longer than then second half, which means it can't be a substring of the second half. If vwx is contained completely in the second half, then when we pump down (that is, take the string uwy). Then the second half will be shorter than the first half (since vwx >=) and so the pumped down string will not be in the language. If vwx overlaps with the symbol #, then it must be the case that the # is contained in w, or else pumping up would give too many # symbols. So either the v will be a string of 's or the x will be a string of 's or both (We need the fact that vwx <= p here). If v is a nonempty string of 's then pumping up will give the left half more 's then the right side, so the left side will not be a subset of the right side. If x is a nonempty string of zeros then pumping down will give the right side fewer zeros than the left so the left side will not be a subset of the right side. In any case we come to the conclusion that no matter how we choose the decomposition, the conditions of the pumping lemma will be violated. So the language cannot be regular. Problem 8. { i i j i i, j > } Not CF. Suppose it is. Then let the pumping length be p. Consider the string s= p p p, which is in the language. If we decompose s into s=uvwxy as in the statement of the pumping lemma, then an argument like the previous one shows that if vwx is anything but the lone zero between the ones, then pumping up will give you something that is not in the language. On the other hand if vwx is the lone zero, then pumping down will give you. p p p, which is not in the language. This contradicts the pumping lemma, so therefore the language is not context free. Problem 9. The complement of {( n n ) m m, n > }. 35
CF. Here is a grammar that generates the language, with some comments on the side to explain what each non-terminal represents. S ABA A λ AC C C; n n B B D F; m n m n D D; one or more s F F; one or more s Why this works: Since R=(**)(**)* is regular, the complement ~R includes all strings which are not even of the correct form. We can Union ** complement... nondeterministically guess which pair mismatches. Problem. 2.8 a. (Lewis-Papadimitriou) A = { n n n n n >= }. Not CF. Let p be the pumping length given by the pumping lemma. We show that s = p p p p cannot be pumped. Let s = uvxyz. If either v or y contain more than one type of alphabet symbol, s` = uv 2 xy 2 z does not contain the symbols in the correct order. Hence s` A. If both v and y contain (at most) one type of alphabet symbol, uv 2 xy 2 z contains runs of s and s of unequal length. Hence s` A. Because s cannot be pumped in any way without violating the pumping lemma, A is not CF. 36
Problem. 2.8 b. B = { n # 2n # 3n n >= }. Not CF. Let p be the pumping length given by the pumping lemma. We show that s = p # 2p # 3p cannot be pumped. Let s = uvxyz. Neither v nor y can contain #, otherwise s` = uv 2 xy 2 z contains more than two #s. Therefore, if we divide x into three segments by #s: p, 2p, 3p, at least one of the segments is not contained within either v or y. Hence, s` = uv 2 xy 2 z is not in B because the : 2 : 3 length ratio of the segments is not maintained. Hence s` B. Because s cannot be pumped in any way without violating the pumping lemma, B is not CF. Problem 2. 2.8 c. C = { w#x w is a substring of x, where w, x {a, b}*}. Not CF. Let p be the pumping length given by the pumping lemma. We show that s = a p b p #a p b p cannot be pumped. Let s = uvxyz. Neither v nor y can contain #, otherwise s` = uv xy z does not contain # and therefore s` C. If both v and y are nonempty and occur on the LHS of the #, the string s` = uv 2 xy 2 z C because it is longer on the LHS than on the RHS. Similarly, if both strings occur on the RHS, the string s` = uv xy z C because, again, the LHS is longer than the RHS. If only one of v and y is nonempty ( vy >, therefore v and y can not be empty at the same time), treat them as if they both occurred on the same side of the # as above. The only remaining case will be where both v and y are nonempty and v is on the LHS, y on the RHS. However, vxy <= p, therefore, v can only contain b s and y can only contain a s. Hence s` = uv 2 xy 2 z C because it contains more b s on the LHS than on the RHS. 37
Since s cannot be pumped in any way without violating the pumping lemma, C is not CF. Referenser. Linz, An Introduction to Formal Languages and Automata, Jones & Bartlett 2 2. Sipser, Introduction to the Theory of Computation, PWS 997 3. Sudkamp, Languages and Machines, Addison Wesley 998 4. Lewis-Papadimitriou, Elements of the Theory of Computation, Prentice Hall 998 38