Context-Free and Noncontext-Free Languages
description
Transcript of Context-Free and Noncontext-Free Languages
![Page 1: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/1.jpg)
Context-Free and Noncontext-Free Languages
Chapter 13 1
![Page 2: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/2.jpg)
Languages That Are and Are Not Context-Free
a*b* is regular.
AnBn = {anbn : n 0} is context-free but not regular.
AnBnCn = {anbncn : n 0} is not context-free.
2
![Page 3: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/3.jpg)
Of Languages and Machines
3
![Page 4: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/4.jpg)
The Regular and the CF LanguagesTheorem: The regular languages are a proper subset of
the context-free languages.
Proof: In two parts:
• Every regular language is CF.
• There exists at least one language that is CF but not regular.
4
![Page 5: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/5.jpg)
The Regular and the CF LanguagesLemma: Every regular language is CF.
Proof: Every FSM is (trivially) a PDA:
Given an FSM M = (K, , , s, A) and elements of of the form: ( p, c, q )
old state, input, new state
Construct a PDA M' = (K, , {}, , s, A). Each (p, c, q) becomes:
(( p, c, ), (q, ))old state, input, don't new state don't
look at push on stack stack
In other words, we just don’t use the stack. 5
![Page 6: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/6.jpg)
There Exists at Least One Language that is CF but Not Regular
Lemma: There exists at least one language that is CF but not regular
Proof: {anbn, n 0} is context-free but not regular.
So the regular languages are a proper subset of the context-free languages.
6
![Page 7: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/7.jpg)
How Many Context-Free Languages Are There?
Theorem: There is a countably infinite number of CFLs.
Proof: ● Upper bound: we can lexicographically enumerate
all the CFGs.
● Lower bound: {a}, {aa}, {aaa}, … are all CFLs.
7
![Page 8: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/8.jpg)
How Many Context-Free Languages Are There?
There is an uncountable number of languages.
Thus there are more languages than there are context-free languages.
So there must exist some languages that are not context-free.
Example: {anbncn : n 0}
8
![Page 9: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/9.jpg)
Showing that L is Context-FreeTechniques for showing that a language L is context-free:
1. Exhibit a context-free grammar for L.
2. Exhibit a PDA for L.
3. Use the closure properties of context-free languages.
Unfortunately, these are weaker than they are forregular languages.
9
![Page 10: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/10.jpg)
Showing that L is Not Context-Free
Remember the pumping argument for regular languages:
10
![Page 11: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/11.jpg)
A Review of Parse Trees A parse tree, derived by a grammar G = (V, , R, S), is a
rooted, ordered tree in which:
● Every leaf node is labeled with an element of {},
● The root node is labeled S,
● Every other node is labeled with some element of V - , ● If m is a nonleaf node labeled X and the children of m
are labeled x1, x2, …, xn, then the rule X x1, x2, …, xn is in R.
11
![Page 12: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/12.jpg)
Some Tree Basics The height of a tree is the length of the longest path from the root to
any leaf. The branching factor of a tree is the largest number of daughter
nodes associated with any node in the tree.
Theorem: The length of the yield of any tree T with height h and branching factor b is bh.
12number of leaf nodes
![Page 13: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/13.jpg)
From Grammars to Trees Given a context-free grammar G:
● Let n be the number of nonterminal symbols in G. ● Let b be the branching factor of G
Suppose that T is generated by G and no nonterminal appears more than once on any path:
The maximum height of T is:
The maximum length of T’s yield is: 13
![Page 14: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/14.jpg)
The Context-Free Pumping Theorem This time we use parse trees, not automata as the basis for our
argument.
If w is “long”, then its parse trees must look like:
Choose one such tree such that there’s no other with fewer nodes.14
![Page 15: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/15.jpg)
The Context-Free Pumping Theorem
There is another derivation in G:
S * uXz * uxz,
in which, at the point labeled [1], the nonrecursive rule2 is used.
So uxz is also in L(G).15
![Page 16: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/16.jpg)
The Context-Free Pumping Theorem
There are infinitely many derivations in G, such as:
S * uXz * uvXyz * uvvXyyz * uvvxyyz
Those derivations produce the strings: uv2xy2z, uv3xy3z, …
So all of those strings are also in L(G). 16
![Page 17: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/17.jpg)
The Context-Free Pumping Theorem
If rule1 = X Xa, we could get v = .
If rule1 = X aX, we could get y = .
But it is not possible that both v and y are . If they were, then the derivation S * uXz * uxz would also yield w and it would create a parse tree with fewer nodes. But that contradicts the assumption that we started with a tree with the smallest possible number of nodes.
17
![Page 18: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/18.jpg)
The Context-Free Pumping Theorem
The height of the subtree rooted at [1] is at most:
18
![Page 19: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/19.jpg)
The Context-Free Pumping Theorem
The height of the subtree rooted at [1] is at most: n + 1
So |vxy| bn + 1.
19
![Page 20: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/20.jpg)
The Context-Free Pumping Theorem
If L is a context-free language, then k 1 ( strings w L, where |w| k
(u, v, x, y, z (w = uvxyz, vy , |vxy| k and q 0 (uvqxyqz is in L)))).
20
![Page 21: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/21.jpg)
k serves two roles: ● How long must w be to guarantee it is pumpable? ● What’s the bound on |vxy|?
Let n be the number of nonterminals in G.Let b be the branching factor of G.
What Is k?
21
![Page 22: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/22.jpg)
If height(T) > n, then some nonterminal occurs more than once on some path. So T is pumpable.
If height(T) n, then |uvxyz| bn.
So if |uvxyz| > bn, w = uvxyz must be pumpable.
How Long Must w be?
22
![Page 23: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/23.jpg)
Assume that we are considering the bottom-most two instances of a repeated nonterminal. Then the yield of the upper one has length at most bn+1.
Assuming b 2, bn+1 > bn.
So let k = bn+1.
What’s the Bound on |vxy|?
23
![Page 24: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/24.jpg)
The Context-Free Pumping Theorem If L is a context-free language, then k 1, such that
strings w L, where |w| k, u, v, x, y, z, such that: w = uvxyz,and
vy , and |vxy| k, andq 0, uvqxyqz is in L.
Proof: L is generated by some CFG G = (V, , R, S) with n nonterminal symbols and branching factor b. Let k be bn + 1. The longest string that can be generated by G with no repeated nonterminals in the resulting parse tree has length bn. Assuming that b 2, it must be the case that bn + 1 > bn. So let w be any string in L(G) where |w| k. Let T be any smallest parse tree for w. T must have height at least n + 1. Choose some path in T of length at least n + 1. Let X be the bottom-most repeated nonterminal along that path. Then w can be rewritten as uvxyz. The tree rooted at [1] has height at most n + 1. Thus its yield, vxy, has length less than or equal to bn + 1, which is k. vy since if vy were then there would be a smaller parse tree for w and we chose T so that that wasn’t so. uxz must be in L because rule2 could have been used immediately at [1]. For any q 1, uvqxyqz must be in L because rule1 could have been used q times before finally using rule2.
24
![Page 25: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/25.jpg)
Regular vs CF Pumping Theorems Similarities:
● We choose w, the string to be pumped.
● We choose a value for q that shows that w isn’t pumpable.
● We may apply closure theorems before we start.
Differences:
● Two regions, v and y, must be pumped in tandem.
● We don’t know anything about where in the strings v and y will fall. All we know is that they are reasonably “close together”, i.e., |vxy| k.
● Either v or y could be empty, although not both. 25
![Page 26: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/26.jpg)
An Example of Pumping: AnBnCn
AnBnCn = {anbncn, n 0}
26
![Page 27: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/27.jpg)
An Example of Pumping: AnBnCn
AnBnCn = {anbncn, n 0}
Choose w = ak bk ck
1 | 2 | 3
27
![Page 28: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/28.jpg)
An Example of Pumping: AnBnCn
AnBnCn = {anbncn, n 0}
Choose w = ak bk ck
1 | 2 | 3
If either v or y spans regions, then let q = 2 (i.e., pump in once). The resulting string will have letters out of order and thus not be in AnBnCn.
If both v and y each contain only one distinct character then set q to 2. Additional copies of at most two different characters are added, leaving the third unchanged. There are no longer equal numbers of the three letters, so the resulting string is not in AnBnCn.
28
![Page 29: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/29.jpg)
An Example of Pumping: { , n 0} L = { , n 0}
The elements of L:
2na2na
n w0
1 a1
2 a4
3 a9
4 a16
5 a25
6 a36
29
![Page 30: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/30.jpg)
An Example of Pumping: { , n 0} L = { , n 0}
If n = k2, then n2 = k4. Let w = . 4ka
30
2na2na
![Page 31: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/31.jpg)
An Example of Pumping: { : n 0} L = { , n 0}. If n = k2, then n2 = k4. Let w = .
vy = ap, for some nonzero p.
Set q to 2. The resulting string, s, is . It must be in L. But it isn’t because it is too short:
w: next longer string in L: (k2)2 a’s (k2 + 1)2 a’s k4 a’s k4 + 2k2 + 1 a’s
For s to be in L, p = |vy| would have to be at least 2k2 + 1.
But |vxy| k, so p can’t be that large. Thus s is not in L and L is not context-free.
4ka
pka 4
31
2na2na
![Page 32: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/32.jpg)
Another Example of PumpingL = {anbman, n, m 0 and n m}.
Let w =
32
![Page 33: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/33.jpg)
Another Example of PumpingL = {anbman, n, m 0 and n m}.
Let w = akbkak
aaa … aaabbb … bbbaaa … aaa | 1 | 2 | 3 |
33
![Page 34: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/34.jpg)
Nested and Cross-Serial Dependencies PalEven = {wwR : w {a, b}*}
a a b b a a
The dependencies are nested.
WcW = {wcw : w {a, b}*}
a a b c a a b
Cross-serial dependencies.
34
![Page 35: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/35.jpg)
WcW = {wcw : w {a, b}*}
35
![Page 36: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/36.jpg)
Let w = akbkcakbk.
aaa … aaabbb … bbbcaaa … aaabbb … bbb| 1 | 2 |3| 4 | 5 |
Call the part before c the left side and the part after c the right side.
● If v or y overlaps region 3, set q to 0. The resulting string will no longer contain a c. ● If both v and y occur before region 3 or they both occur after region 3, then set q to 2. One side will be longer than the other. ● If either v or y overlaps region 1, then set q to 2. In order to make the right side match, something would have to be pumped into region 4. Violates |vxy| k. ● If either v or y overlaps region 2, then set q to 2. In order to make the right side match, something would have to be pumped into region 5. Violates |vxy| k.
WcW = {wcw : w {a, b}*}
36
![Page 37: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/37.jpg)
Variable Declaration and Use WcW = {wcw : w {a, b}*}.
string winniethepooh;winniethepooh = “bearofverylittlebrain”;
37
![Page 38: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/38.jpg)
Closure Theorems for Context-Free Languages
The context-free languages are closed under:
● Union
● Concatenation
● Kleene star
● Reverse
● Letter substitution
38
![Page 39: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/39.jpg)
Closure Under Union
Let G1 = (V1, 1, R1, S1), and G2 = (V2, 2, R2, S2).
Assume that G1 and G2 have disjoint sets of nonterminals,not including S.
Let L = L(G1) L(G2).
We can show that L is CF by exhibiting a CFG forit:
39
![Page 40: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/40.jpg)
Closure Under Union
Let G1 = (V1, 1, R1, S1), and G2 = (V2, 2, R2, S2).
Assume that G1 and G2 have disjoint sets of nonterminals,not including S.
Let L = L(G1) L(G2).
We can show that L is CF by exhibiting a CFG forit: G = (V1 V2 {S}, 1 2, R1 R2 {S S1, S S2}, S)
40
![Page 41: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/41.jpg)
Closure Under Concatenation Let G1 = (V1, 1, R1, S1), and G2 = (V2, 2, R2, S2).
Assume that G1 and G2 have disjoint sets of nonterminals,not including S.
Let L = L(G1)L(G2).
We can show that L is CF by exhibiting a CFG for it:
41
![Page 42: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/42.jpg)
Closure Under Concatenation Let G1 = (V1, 1, R1, S1), and G2 = (V2, 2, R2, S2).
Assume that G1 and G2 have disjoint sets of nonterminals,not including S.
Let L = L(G1)L(G2).
We can show that L is CF by exhibiting a CFG for it:
G = (V1 V2 {S}, 1 2, R1 R2 {S S1 S2}, S)
42
![Page 43: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/43.jpg)
Closure Under Kleene Star Let G = (V, , R, S1).
Assume that G does not have the nonterminal S.
Let L = L(G)*.
We can show that L is CF by exhibiting a CFG for it:
43
![Page 44: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/44.jpg)
Closure Under Kleene Star Let G = (V, , R, S1).
Assume that G does not have the nonterminal S.
Let L = L(G)*.
We can show that L is CF by exhibiting a CFG for it:
G = (V1 {S}, 1, R1 {S , S S S1}, S)
44
![Page 45: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/45.jpg)
Closure Under Reverse LR= {w * : w = xR for some x L}.
Let G = (V, , R, S) be in Chomsky normal form.
Every rule in G is of the form X BC or X a, where X, B, and C are elements of V - and a .
● X a: L(X) = {a}. {a}R = {a}.
● X BC: L(X) = L(B)L(C). (L(B)L(C))R = L(C)RL(B)R.
Construct, from G, a new grammar G, such that L(G) = LR: G = (VG, G, R, SG), where R is constructed as follows:
● For every rule in G of the form X BC, add to R the rule X CB.
● For every rule in G of the form X a, add to R the rule X a.45
![Page 46: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/46.jpg)
What About Intersection and Complement?
Closure under complement implies closure under intersection, since:
L1 L2 = (L1 L2)
But are the CFLs closed under either complement or intersection?
We proved closure for regular languages two different ways:1. Given a DFSM for L, construct a DFSM for L by swapping
accepting and rejecting states. If closed under complement and union, must be closed under intersection.
2. Given automata for L1 and L2, construct an automaton for L1 L2 by simulating the parallel operation of the two original machines,
using states that are the Cartesian product of the sets of states of the two original machines.
Does either work here?46
![Page 47: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/47.jpg)
Closure Under Intersection The context-free languages are not closed under intersection:
The proof is by counterexample. Let:
L1 = {anbncm: n, m 0} /* equal a’s and b’s.L2 = {ambncn: n, m 0} /* equal b’s and c’s.
Both L1 and L2 are context-free, since there exist straightforward context-free grammars for them.
But now consider: L = L1 L2
= 47
![Page 48: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/48.jpg)
Closure Under Intersection The context-free languages are not closed under intersection:
The proof is by counterexample. Let:
L1 = {anbncm: n, m 0} /* equal a’s and b’s.L2 = {ambncn: n, m 0} /* equal b’s and c’s.
Both L1 and L2 are context-free, since there exist straightforward context-free grammars for them.
But now consider: L = L1 L2
= {anbncn: n 0}48
![Page 49: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/49.jpg)
Closure Under Complement
L1 L2 = (L1 L2)
The context-free languages are closed under union, so if they were closed under complement, they would be closed under intersection (which they are not).
49
![Page 50: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/50.jpg)
Closure Under ComplementAn Example
AnBnCn is context-free:
But (AnBnCn) = AnBnCn is not context-free. 50
![Page 51: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/51.jpg)
Closure Under Difference Are the context-free languages closed under difference?
51
![Page 52: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/52.jpg)
Closure Under Difference Are the context-free languages closed under difference?
L = * - L.
* is context-free. So, if the context-free languages were closed under difference, the complement of any context-free language would necessarily be context-free. But we just showed that that is not so.
52
![Page 53: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/53.jpg)
The Intersection of a Context-Free Language and a Regular Language is Context-Free
L = L(M1), a PDA = (K1, , 1, 1, s1, A1).R = L(M2), a deterministic FSM = (K2, , , s2, A2).
We construct a new PDA, M3, that accepts L R by simulating the parallel execution of M1 and M2.
M = (K1 K2, , 1, , (s1, s2), A1 A2).
Insert into :
For each rule (( q1, a, ), ( p1, )) in 1,and each rule ( q2, a, p2) in , ((( q1, q2), a, ), (( p1, p2), )).
For each rule ((q1, , ), (p1, ) in 1,and each state q2 in K2, (((q1, q2), , ), ((p1, q2), )).
This works because: we can get away with only one stack.53
![Page 54: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/54.jpg)
Theorem: The difference (L1 – L2) between a context-free language L1 and a regular language L2 is context-free.
Proof: L1 – L2 = L1 L2.
If L2 is regular then so is L2.
If L1 is context-free, so is L1 L2.
The Difference between a Context-Free Language and a Regular Language is Context-Free
54
![Page 55: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/55.jpg)
Let:
L = {anbn: n 0 and n 1776}.
Alternatively:
L = {anbn: n 0} – {a1776b1776}.
{anbn: n 0} is context-free. {a1776b1776} is regular.
An Example: A Finite Number of Exceptions
55
![Page 56: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/56.jpg)
One Closure Theorem:
If L1 and L2 are context free, then so is
L3 = L1 L2.
But what if L3 and L1 are context free? What can we say about L2?
L3 = L1 L2.
Don’t Try to Use Closure Backwards
56
![Page 57: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/57.jpg)
One Closure Theorem:
If L1 and L2 are context free, then so is
L3 = L1 L2.
But what if L3 and L1 are context free? What can we say about L2?
L3 = L1 L2.
Example: anbnc* = anbnc* anbncn.
Don’t Try to Use Closure Backwards
57
![Page 58: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/58.jpg)
Using the Closure Theorems with the Pumping Theorem
Let WW = {ww : w {a, b}* }.
Let’s try pumping: Choose w = (ab)2k
(Don’t get confused about the two uses of w.)
w wababab…abababababab…ababababab
But this pumps fine with v = and y = 58
![Page 59: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/59.jpg)
Exploiting Regions
WW = {ww : w {a, b}* }.
Choose the string akbakb.
aaaaa…………………baaaaaa……………..b w w
But this also pumps fine.
59
![Page 60: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/60.jpg)
Make All Regions “Long”
WW = {ww : w {a, b}* }.
Choose the string akbkakbk.
aaa….. aabb………bbaa……aabb……..b w w
1 2 3 4
Now we list the possibilities:(1, 1), (2, 2), (3, 3), (4, 4), (1, 2), (2, 3), (3, 4), (1, 3), (1, 4), (2, 4), (1/2, 2), (1/2, 3), (1/2, 4), (1/2, 2/3),…
Whenever v or y spans regions, we’ll no longer have a string of the same form, but that’s okay given the definition of L.
60
![Page 61: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/61.jpg)
Using Intersection with a Regular Language
WW = {ww : w {a, b}* }.
Recall our last choice of w: akbkakbk.
aaa….. aabb………bbaa……aabb……..b w w
1 2 3 4
But let’s consider L' = L
61
![Page 62: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/62.jpg)
Using Intersection with a Regular Language
WW = {ww : w {a, b}* }.
But let’s consider L' = L a*b*a*b*.
L' is not context-free. Let w = akbkakbk.
aaa….. aabb………bbaa……aabb……..b w w
1 2 3 4
62
![Page 63: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/63.jpg)
Another Example L = {w : w can be written as x # y = z : x, y, z {0, 1}* and, if x, y, and z are
viewed as binary numbers without leading zeros, xy = zR}.
For example, 100#1111=001111 is in L.
63
![Page 64: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/64.jpg)
Another Example L = {w : w can be written as x # y = z :
x, y, z {0, 1}* and, if x, y, and z are viewed as binary numbers without leading zeros, x # y = zR}.
Choose w = 10k#1k=0k1k: 1 000 … 000 # 111 … 111 = 000 … 000111 …111
|1| 2 |3| 4 |5| 6 | 7 |
Note that w is in L.
If L is CF, so is L = L 10*#1*=0*1*:
64
![Page 65: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/65.jpg)
Another Example Choose w = 10k#1k=0k1k: 1 000 … 000 # 111 … 111 = 000 … 000111 …111
|1| 2 |3| 4 |5| 6 | 7 |
L = L 10*#1*=0*1* is not CF:
v or y overlaps 1, 3, or 5:
v or y contains the boundary between 6 and 7:
(2, 2), (4, 4), or (2, 4):
(6, 6), (7, 7), or (6, 7):
(4, 6):
(2, 6), (2, 7) or (4, 7):65
![Page 66: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/66.jpg)
L = {w {a, b, c}* : #a(w) = #b(w) = #c(w) }
If L were context-free, then L = L a*b*c* would also be context-free.
But L =
So neither is L.
66
![Page 67: Context-Free and Noncontext-Free Languages](https://reader036.fdocuments.us/reader036/viewer/2022062410/568162ca550346895dd356be/html5/thumbnails/67.jpg)
Why are the Context-Free Languages Not Closed under Complement, Intersection and
Subtraction But the Regular Languages Are? Given an NDFSM M1, build an FSM M2 such that L(M2) = L(M1): 1. From M1, construct an equivalent deterministic FSM M, using ndfsmtodfsm. 2. If M is described with an implied dead state, add the dead state
and all required transitions to it. 3. Begin building M2 by setting it equal to M. Then swap the accepting and the nonaccepting states. So:
M2 = (KM, , M, sM, KM - AM).
We could do the same thing for CF languages if we could do step 1,but we can’t.
The need for nondeterminism is the key.67