10 Context-Free Properties
10 Context-Free Properties
Languages
Two Pumping Lemmas
1
Topics
2
Pumping Lemmas
• The pumping lemma for regular languages was given in Theorem 4.8.
• It is a tool for showing certain languages are not regular.
• There exist similar pumping lemmas for other languages.
• We will learn pumping lemmas for
• context-free languages, and
• linear languages, which are more restricted form of context-free languages.
• We can use them to show that certain languages are not context-free.
3
A Pumping Lemma for Context-Free Languages
Theorem 8.1
i i
• w = uvxyz (8.1) | vxy | ≤ m (8.2), | vy | ≥ 1 (8.3) uv xy z ∈ L (8.4) for all i = 0,1,2,⋯.
• If a given language L is a context-free language,
• every string w ∈ L satis es the above condition for some positive integer m.
• We don't know which m and uvxyz will satisfy the condition for w.
i i
• However, if m and uvxyz are satisfying the condition, then uv xy z for all i must be in L.
i i
• It means that for every m and uvxyz, if we show that uv xy z is not in L for a
certain i, then L is not context-free.
5
fi
A Pumping Lemma for Context-Free Languages
Theorem 8.1
• wi ∉ L for a certain decomposition does not mean that there's no decomposition for wi ∈ L.
4. We try to pick i which makes wi ∉ L. If we can, we win the game, and L is not context-free.
6 fi
A Pumping Lemma for Context-Free Languages
Example 8.1
n n n
• Show that the language L = {a b c : n ≥ 0} is not context-free.
m m m
• Once the opponent X picks m, we pick w = a b c .
• Now X has several choices for decompositions.
• Make vxy to contain only a's: wi will obviously not in L.
• v and y contain an equal number of a's and b's:
k k m
• then pumped strings in the form a b c , k ≠ m, which is not in L.
• The only way that X can win is to select a decomposition with vxy contain an equal number of a's, b's, and
c's, which is not possible due to | vxy | ≤ m.
• Therefore, L is not context-free.
7
A Pumping Lemma for Context-Free Languages
Example 8.1
n n
• The same argument will be resulted in failure for L = {a b }.
m m k k
• If we pick a b , the opponent can always choose v = a and y = b .
• Then regardless of i we pick, wi will always belong to L.
• Note that this doesn't prove that L is context-free.
• It's not like to show that L is not context-free.
• We only showed that there is an example satisfying the pumping lemma.
• To prove that L is context-free, then we should use other methods such as nd a context-
free grammar for L.
fi
A Pumping Lemma for Context-Free Languages
Example 8.1
̂ n n n 2n n n n
• We claimed that L = {a b } ∪ {a b } ∪ {a b c } is not context-free, in
Example 7.11.
m m m
• We already showed that we can win the game using a b c , which is also in
L .̂
• L
If ̂ is a context-free language, then the pumping lemma must hold for any
w ∈ L .̂
• Hence the claim was proved by our previous argument.
9
A Pumping Lemma for Context-Free Languages
Example 8.2
R
• This looks very similar to ww , but it's not context-free.
m m m m
• Take a b a b .
• For any decomposition the opponent picks, we have a winning countermove.
• For instance, for the choice in Figure 8.2, we can use i = 0,
k j m m
• a b a b , k < m or j < m, which is not in L.
• For other choices of decompositions, we can argue similarly, hence L is not context-free.
10
A Pumping Lemma for Context-Free Languages
Example 8.3
n!
• Show that the language L = {a : n ≥ 0} is not context free.
m!
• For given m, we pick w = a .
k l
• Any decomposition for it always of the form v = a , y = a .
• Then w0 = uxz has length m! − (k + l). Then w0 ∈ L only if
• m! − (k + l) = j! for some j.
• This is impossible since k + l ≤ m, m! − (k + l) > (m − 1)!.
• Therefore, the language is not context free.
11
A Pumping Lemma for Context-Free Languages
Example 8.4
n j 2
• Show that the language L = {a b : n = j } is not context free.
2
m m
• For given m, we pick a b .
• Now the opponent has several choices.
• All a's, a's and b's, all b's. Figure 8.3
• We need to think about a's and b's with certain numbers, like Figure 8.3.
2
• Pumping i times, wi will be with m + (i − 1)k1 a's and m + (i − 1)k2 b's.
12
A Pumping Lemma for Context-Free Languages
Example 8.4
2
• Pumping i times, wi will be with m + (i − 1)k1 a's and m + (i − 1)k2 b's.
m 2−k1 m−k2
• If the opponent picks k1 ≠ 0, k2 ≠ 0, we can pick w0 = a b .
2 2 2 2
• (m − k2) ≤ (m − 1) = m − 2m + 1 < m − k1,
• the result is not in L.
• If the opponent picks k1 = 0, k2 ≠ 0 or k1 ≠ 0, k2 = 0,
• then with i = 0, the pumped string is not in L.
• Hence we can conclude that L is not a context-free language.
13
A Pumping Lemma for Context-Free Languages
Exercise 7 (d)
n j k
• Show that L = {a b c : k > n, k > j} on Σ = {a, b, c} is not context-free.
m m m+1
• For given m, we pick a b c which is in L.
k l
• The only possibility that the opponent wins the game is choosing v = b , y = c
for k ≠ 0, l ≠ 0.
m m+(i−1)k m+1+(i−1)l
• Then the pumped string wi = a b c .
m m−k m+1−l
• We choose i = 0, then w0 = a b c .
15
Linear Languages
De nition 8.1
16
fi
Linear Languages
Example 8.5
n n
• The language L = {a b : n ≥ 0} is a linear language.
• G = ({S}, {a, b}, S, P) with productions S → aSb | λ.
• A linear grammar is a grammar in which at most one variable can occur on the right side of any
production, without restriction on the position of the variable (Example 3.14).
• L1 = {w : na(w) = nb(w)}.
• We know that the grammar for L1 is S → SS | aSb | bSa | λ, which is not linear.
• Hence L1 is not necessarily linear, but giving one non-linear grammar for L1 is not enough to prove
that.
• It is possible that there exists a linear grammar for L1 which we have not found yet.
17
A Pumping Lemma for Linear Languages
Theorem 8.2
• So far we've seen the cases that the choice of w is quite obvious.
3. The opponent chooses a decomposition uvxyz, satisfying (8.5) and (8.6).
4. We try to pick i which makes wi ∉ L. If we can, we win the game, and L is not linear.
19
ffi
fi
A Pumping Lemma for Linear Languages
Example 8.6
• Show that L = {w ∈ {a, b, c}* : na(w) + nb(w) = nc(w)} is context-free, but not linear.
m 2m m
• Given m, we pick w = a c b .
k l
• Whatever the decomposition is picked, v = a , y = b .
m+(i−1)k 2m m+(i−1)l
• Then wi = a c b ,1 ≤ k + l ≤ m.
m−k 2m m−l
• We pick i = 0, then w0 = a c b .
• Since k + l ≥ 1, m − k + m − l = 2m − (k + l) ≤ 2m.
• Hence w0 ∉ L, so L is not linear.
21
A Pumping Lemma for Linear Languages
Exercise 13
n j
• Determine whether or not L = {a b : j ≤ n ≤ 2j − 1} is linear.
• If it looks linear, we can come up with a linear grammar.
• If it doesn't look like linear, we can apply the pumping lemma.
• We can add more a's, but that number cannot exceed the twice of b's.
• Make it twice: S → aaSb.
• We can also make the same number: S → aSb.
• In the end, we cannot have n = 2j, hence S → ab.
• S → aaSb | aSb | ab is a linear grammar for L, hence L is linear.
23
Closure Properties and Decision
Algorithms for Context-Free
Languages
24
Closure of Context-Free Languages
Theorem 8.3
• The family of context-free languages is closed under union, concatenation, and star-closure.
• Proof: Let L1 and L2 be two context-free languages generated by the context-free grammars
G1 = (V1, T1, S1, P1) and G2 = (V2, T2, S2, P2), respectively.
25
Closure of Context-Free Languages
Theorem 8.3
27
Closure of Context-Free Languages
Theorem 8.4
n n m n m m
• L1 = {a b c : n ≥ 0, m ≥ 0}, L2 = {a b c : n ≥ 0, m ≥ 0}.
• A context-free grammar for L1 is
• S → S1S2, S1 → aS1b | λ, S2 → cS2 | λ.
• Or, we can use the fact that L1 is a concatenation of two context-free
n n m
languages, say {a b } and {c }.
28
Closure of Context-Free Languages
Theorem 8.4
n n n
• Now, consider the intersection L1 ∩ L2 = {a b c : n ≥ 0}.
• We have already shown that this is not context-free.
• Thus the family of context-free languages is not closed under intersection.
• For complementation, we consider
C C C
• L1 ∩ L2 = (L1 ∪ L2 ) .
• If context-free languages are closed under complementation, the right side of the expression
would be a context-free language.
• This contradicts that L1 ∩ L2 is not context-free, hence the family of context-free languages are
not closed under complementation.
29
Closure of Context-Free Languages
Theorem 8.5
• Whenever a symbol is read from the input string, M ̂ simultaneously executes the
moves of M1 and M2.
30
Closure of Context-Free Languages
Theorem 8.5
• ((q ,
k lp ), x) ∈ δ ̂
((q ,
i jp ), a, b) , i .
31
fi
ff
Closure of Context-Free Languages
Theorem 8.5
• The states of M ̂ are labeled with pairs (qi, pj), representing the respective states in which M1 and M2 can
be after reading a certain input string.
• Therefore, a string is accepted by M ̂ i . it is accepted by both M1 and M2, which means that
L(M1) ∩ L(M2) = L1 ∩ L2.
• This property is called closure under regular intersection.
32
ff
Closure of Context-Free Languages
Example 8.7
n n
• Show that the language L = {a b : n ≥ 0,n ≠ 100} is context-free.
• We can prove it more neatly and concisely with Theorem 8.5, compared to
construct a PDA or a context-free grammar.
100 100
• Let L1 = {a b }, which is nite, and thus it is also regular.
n n C
• It is easy to see that L = {a b : n ≥ 0} ∩ L1 .
C n n
• We know L1 is regular since L1 is regular, and {a b } is context-free.
34
Decidable Properties of Context-Free Languages
Theorem 8.6
35
Decidable Properties of Context-Free Languages
Theorem 8.7
• For the algorithm, we only need to determine whether the grammar has some
repeating variables.
39
Summary