0% found this document useful (0 votes)
6 views40 pages

10 Context-Free Properties

The document discusses the properties of context-free languages, focusing on two pumping lemmas: one for context-free languages and another for linear languages. It explains how these lemmas can be used to demonstrate that certain languages are not context-free or linear by providing examples and counterexamples. Additionally, it covers closure properties of context-free languages, stating that they are closed under union, concatenation, and star-closure.

Uploaded by

ghmpersonal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views40 pages

10 Context-Free Properties

The document discusses the properties of context-free languages, focusing on two pumping lemmas: one for context-free languages and another for linear languages. It explains how these lemmas can be used to demonstrate that certain languages are not context-free or linear by providing examples and counterexamples. Additionally, it covers closure properties of context-free languages, stating that they are closed under union, concatenation, and star-closure.

Uploaded by

ghmpersonal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Properties of Context-Free

Languages
Two Pumping Lemmas

1
Topics

• Two Pumping Lemmas


• A Pumping Lemma for Context-Free Languages
• A Pumping Lemma for Linear Languages
• Closure Properties and Decision Algorithms for Context-Free Languages

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

• Let L be an in nite context-free language.


• Then there exists some positive integer m s.t. any w ∈ L with | w | ≥ m can be
decomposed as

• w = uvxyz, (8.1) with


• | vxy | ≤ m, (8.2) and | vy | ≥ 1, (8.3) such that
i i
• uv xy z ∈ L, (8.4)
• for all i = 0,1,2,⋯.
• This is the pumping lemma for context-free languages.
4
fi
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

• This can be also viewed as a game - to apply the pumping lemma.


• Of course, we have to play both sides.
1. The opponent picks m.

2. Given m, we pick a string w, | w | ≥ m.


3. The opponent chooses a decomposition uvxyz, satisfying (8.2) and (8.3).
i i
• We need to assume that the opponent makes it hardest to us to nd an i, which makes wi = uv xy z
is not in L.

• 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

• Consider the language L = {ww : w ∈ {a, b}*}. Figure 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 .

• Since l ≥ 1, m + 1 − l ≤ m, which means w0 ∉ L, and L is not context-free.


14
A Pumping Lemma for Linear
Languages

15
Linear Languages
De nition 8.1

• A context-free language L is said to be linear if there exists a linear context-


free grammar G such that L = L(G).

• It means that every linear language is context free.


• We don't know yet if there exists some languages which are
• context free languages, but not linear languages.

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

• Let L be an in nite linear language.


• Then there exists some positive integer m s.t. any w ∈ L with | w | ≥ m can be
decomposed as w = uvxyz with

• | uvyz | ≤ m, (8.5) and | vy | ≥ 1, (8.6) such that


i i
• uv xy z ∈ L, (8.7)
• for any i = 0,1,2,⋯.
• Note that (8.5) is di erent from (8.2). Now strings v and y must be located within m
symbols of the left or right ends of w.
18
fi
ff
A Pumping Lemma for Linear Languages
Visualization with a game

1. The opponent picks m.

2. Given m, we pick a string w, | w | ≥ m, to make the opponent di cult to choose a


decomposition.

• 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).

• We need to assume that the opponent makes it hardest to us to nd an i, which


i i
makes wi = uv xy z is not in L.

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

• The language L = {w : na(w) = nb(w)} is not linear.


• To show this, assume that L is linear and apply Theorem 8.2 to
m 2m m
• w=a b a .
• Inequality (8.5) shows that strings u, v, y, z must all consist of a's only in this case.
k l
• For v = a , y = a , k ≥ 1 or l ≥ 1,
m+k 2m m+l
• w2 = a b a is not in L.

• Hence L is not linear.


20
A Pumping Lemma for Linear Languages
Exercise 13

• 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

• L = {w ∈ {a, b, c}* : na(w) + nb(w) = nc(w)}.


• How can we prove that L is context-free?
• It seems to be complicated to nd a context-free grammar.
• We can consider an NPDA for this.
• Every time an input symbol is a or b,
• either push 0 to the stack (if the stack top is not 1), or pop 1 from the stack.
• Every time an input symbol is c,
• either push 1 to the stack (if the stack top is not 0), or pop 0 from the stack.
22
fi
A Pumping Lemma for Linear Languages
Exercise 14

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.

• We can assume that V1 and V2 are disjoint, w/o loss of generality.


• Consider L(G3) generated by G3 = (V1 ∪ V2 ∪ {S3}, T1 ∪ T2, S3, P3), where S3 ∉ V1 ∪ V2.
• P3, the production of G3 is P3 = P1 ∪ P2 ∪ {S3 → S1 | S2}.
• Then P3 is a context-free grammar, so that L(G3) is a context-free language.
• Since L(G3) = L1 ∪ L2, we can conclude that the family of context-free language is closed under
union.

25
Closure of Context-Free Languages
Theorem 8.3

• Now consider G4 = (V1 ∪ V2 ∪ {S4}, T1 ∪ T2, S4, P4).


• P4 = P1 ∪ P2 ∪ {S4 → S1S2}.
• Then L(G4) = L(G1)L(G2).
• Finally, L(G5) with G5 = (V1 ∪ {S5}, T1, S5, P5), and
• P5 = P1 ∪ {S5 → S1S5 | λ}.
• Then L(G5) = L(G1)*.
26
Closure of Context-Free Languages
Theorem 8.4

• The family of context-free languages is not closed under intersection and


complementation.

• Proof: Consider the two languages


n n m
• L1 = {a b c : n ≥ 0, m ≥ 0},
n m m
• L2 = {a b c : n ≥ 0, m ≥ 0}.
• L1 and L2 are context-free languages.

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 }.

• Then due to Theorem 8.3, L1 is also context-free.

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

• Let L1 be a context-free language and L2 be a regular language.


• Then L1 ∩ L2 is context-free.
• Proof: Let M1 = (Q, Σ, Γ, δ1, q0, z, F1) be an NPDA that accepts L1 and
M2 = (P, Σ, δ2, p0, F2) be a DFA that accepts L2.

• We construct a PDA M ̂ = ( Q ,̂ Σ, Γ, δ ,̂ q0 ,̂ z, F )̂ that simulates the parallel action of


M1 and M2:

• 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

• We let Q ̂ = Q × P, q0 ̂ = (q0, p0), F ̂ = F1 × F2,

• and de ne δ ̂ such that

• ((q ,
k lp ), x) ∈ δ ̂
((q ,
i jp ), a, b) , i .

• (qk, x) ∈ δ1(qi, a, b) and δ2(pj, a) = pl.


• We also require that if a = λ, then pj = pl, for the DFA.

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.

• Using induction argument,

• ((q0, p0), w, z) ⊢*M ̂ ((qr, ps), λ, x),

• with qr ∈ F1, and ps ∈ F2 if and only if

• (q0, w, z) ⊢*M1 (qr, λ, x), and δ*(p0, w) = ps.

• 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.

• Therefore, due to closure under regular intersection, L is context-free.


33
fi
Closure of Context-Free Languages
Example 8.8

• Show that the language


• L = {w ∈ {a, b, c}* : na(w) = nb(w) = nc(w)} is not context-free.
• Suppose that L were context free.
n n n
• Then L ∩ L(a*b*c*) = {a b c : n ≥ 0} would be also context-free.
• We already know that this is not true.
• Therefore, we can conclude that L is not context-free.

34
Decidable Properties of Context-Free Languages
Theorem 8.6

• Given a context-free grammar G = (V, T, S, P), there exists an algorithm for


deciding whether or not L(G) is empty.

• Proof: Assume that λ ∉ L(G) for simplicity.


• We use the algorithm for removing useless variables and productions.
• If S is found to be useless, then L(G) is empty;
• Otherwise, L(G) contains at least one element.

35
Decidable Properties of Context-Free Languages
Theorem 8.7

• Given a context-free grammar G = (V, T, S, P), there exists an algorithm for


determining whether or not L(G) is in nite.

• Proof: Assume that G contains no λ-productions, no unit-productions, and no


useless symbols.

• Suppose the grammar has a repeating variable like this.


• There exists some A ∈ V for which there is a derivation A ⇒* xAy.
• Since G is assumed to have no λ- and unit-productions, x and y cannot be
simultaneously empty.
36
fi
Decidable Properties of Context-Free Languages
Theorem 8.7

• Since A is neither nullable nor a useless symbol, we have


• S ⇒* uAv ⇒ w and A ⇒* z,
• where u, v, and z are in T*.
• But then with A ⇒* xAy,
n n n n
• S ⇒* uAv ⇒* ux Ay v ⇒* ux zy v
• is possible for all n, so that L(G) is in nite.
• If no variable can ever repeat, then the length of any derivation is bounded by | V | .
• In this case, L(G) is nite.
37
fi
fi
Decidable Properties of Context-Free Languages
Theorem 8.7

• For the algorithm, we only need to determine whether the grammar has some
repeating variables.

• We can do this by drawing dependency graph for variables, in a way that


• there is an edge (A, B) whenever there is a corresponding production
• A → xBy.
• Then any variable that is at the base of the cycle is a repeating one.
• Using this, we can have an algorithm for determining whether or not L(G) is
in nite.
38
fi
Decidable Properties of Context-Free Languages
Exercise 23

• Show that there exists an algorithm to determine if a context-free language contains


any even-length strings.

• Let L is the given language on Σ, and Le = {w ∈ Σ* : | w | is even}.


• Le is regular: we can easily think of an NFA.
• Using closure under regular intersection, L ∩ Le is context-free.
• Hence, by Theorem 8.6, there is an algorithm to decide whether or not L ∩ Le is empty.
• It means that we can decide whether L contains any even-length strings.

39
Summary

• You must understand


• what are the pumping lemmas for context-free/linear languages, and how to
apply them for a given language.

• Practice to apply the pumping lemmas for a given language.


• Also you have to consider construct a linear grammar to determine whether a
given language is linear or not.

• You also need to know closure properties of context-free languages.


• Practice to use closure properties and decision algorithms for other arguments.
40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy