Download presentation
Presentation is loading. Please wait.
Published byGervase Payne Modified over 9 years ago
1
Context-Free and Noncontext-Free Languages Chapter 13 1
2
Languages That Are and Are Not Context-Free a * b * is regular. A n B n = { a n b n : n 0} is context-free but not regular. A n B n C n = { a n b n c n : n 0} is not context-free. 2
3
Of Languages and Machines 3
4
The Regular and the CF Languages Theorem: 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
5
The Regular and the CF Languages Lemma: 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 statedon't look atpush on stackstack In other words, we just don’t use the stack. 5
6
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: { a n b n, n 0} is context-free but not regular. So the regular languages are a proper subset of the context-free languages. 6
7
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
8
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: { a n b n c n : n 0} 8
9
Showing that L is Context-Free Techniques 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 for regular languages. 9
10
Showing that L is Not Context-Free Remember the pumping argument for regular languages: 10
11
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. 11
12
The Context-Free Pumping Theorem There is another derivation in G: S * uXz * uxz, in which, at the point labeled [1], the nonrecursive rule 2 is used. So uxz is also in L(G). 12
13
Nested and Cross-Serial Dependencies PalEven = {ww R : w { a, b }*} a a b b a a The dependencies are nested. W c W = {w c w : w { a, b }*} a a b c a a b Cross-serial dependencies. 13
14
Closure Theorems for Context-Free Languages The context-free languages are closed under: ● Union ● Concatenation ● Kleene star ● Reverse ● Letter substitution 14
15
Closure Under Union Let G 1 = (V 1, 1, R 1, S 1 ), and G 2 = (V 2, 2, R 2, S 2 ). Assume that G 1 and G 2 have disjoint sets of nonterminals, not including S. Let L = L(G 1 ) L(G 2 ). We can show that L is CF by exhibiting a CFG for it: 15
16
Closure Under Union Let G 1 = (V 1, 1, R 1, S 1 ), and G 2 = (V 2, 2, R 2, S 2 ). Assume that G 1 and G 2 have disjoint sets of nonterminals, not including S. Let L = L(G 1 ) L(G 2 ). We can show that L is CF by exhibiting a CFG for it: G = (V 1 V 2 {S}, 1 2, R 1 R 2 {S S 1, S S 2 }, S) 16
17
Closure Under Concatenation Let G 1 = (V 1, 1, R 1, S 1 ), and G 2 = (V 2, 2, R 2, S 2 ). Assume that G 1 and G 2 have disjoint sets of nonterminals, not including S. Let L = L(G 1 )L(G 2 ). We can show that L is CF by exhibiting a CFG for it: 17
18
Closure Under Concatenation Let G 1 = (V 1, 1, R 1, S 1 ), and G 2 = (V 2, 2, R 2, S 2 ). Assume that G 1 and G 2 have disjoint sets of nonterminals, not including S. Let L = L(G 1 )L(G 2 ). We can show that L is CF by exhibiting a CFG for it: G = (V 1 V 2 {S}, 1 2, R 1 R 2 {S S 1 S 2 }, S) 18
19
Closure Under Kleene Star Let G = (V, , R, S 1 ). 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: 19
20
Closure Under Kleene Star Let G = (V, , R, S 1 ). 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 = (V 1 {S}, 1, R 1 {S , S S S 1 }, S) 20
21
Closure Under Reverse L R = {w * : w = x R 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) R L(B) R. Construct, from G, a new grammar G, such that L(G) = L R : G = (V G, G, R, S G ), 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. 21
22
What About Intersection and Complement? Closure under complement implies closure under intersection, since: L 1 L 2 = ( L 1 L 2 ) 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 L 1 and L 2, construct an automaton for L 1 L 2 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? 22
23
Closure Under Intersection The context-free languages are not closed under intersection: The proof is by counterexample. Let: L 1 = { a n b n c m : n, m 0} /* equal a ’s and b ’s. L 2 = { a m b n c n : n, m 0} /* equal b ’s and c ’s. Both L 1 and L 2 are context-free, since there exist straightforward context-free grammars for them. But now consider: L = L 1 L 2 = 23
24
Closure Under Intersection The context-free languages are not closed under intersection: The proof is by counterexample. Let: L 1 = { a n b n c m : n, m 0} /* equal a ’s and b ’s. L 2 = { a m b n c n : n, m 0} /* equal b ’s and c ’s. Both L 1 and L 2 are context-free, since there exist straightforward context-free grammars for them. But now consider: L = L 1 L 2 = { a n b n c n : n 0} 24
25
Closure Under Complement L 1 L 2 = ( L 1 L 2 ) 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). 25
26
Closure Under Complement An Example A n B n C n is context-free: But ( A n B n C n ) = A n B n C n is not context-free. 26
27
Closure Under Difference Are the context-free languages closed under difference? 27
28
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. 28
29
The Intersection of a Context-Free Language and a Regular Language is Context-Free L = L(M 1 ), a PDA = (K 1, , 1, 1, s 1, A 1 ). R = L(M 2 ), a deterministic FSM = (K 2, , , s 2, A 2 ). We construct a new PDA, M 3, that accepts L R by simulating the parallel execution of M 1 and M 2. M = (K 1 K 2, , 1, , (s 1, s 2 ), A 1 A 2 ). Insert into : For each rule ((q 1,a, ), (p 1, )) in 1, and each rule (q 2, a, p 2 ) in , (((q 1, q 2 ), a, ), ((p 1, p 2 ), )). For each rule ((q 1, , ), (p 1, ) in 1, and each state q 2 in K 2, (((q 1, q 2 ), , ), ((p 1, q 2 ), )). This works because: we can get away with only one stack. 29
30
Theorem: The difference (L 1 – L 2 ) between a context-free language L 1 and a regular language L 2 is context-free. Proof: L 1 – L 2 = L 1 L 2. If L 2 is regular then so is L 2. If L 1 is context-free, so is L 1 L 2. The Difference between a Context-Free Language and a Regular Language is Context-Free 30
31
Let: L = { a n b n : n 0 and n 1776}. Alternatively: L = { a n b n : n 0} – { a 1776 b 1776 }. { a n b n : n 0} is context-free. { a 1776 b 1776 } is regular. An Example: A Finite Number of Exceptions 31
32
One Closure Theorem: If L 1 and L 2 are context free, then so is L 3 = L 1 L 2. But what if L 3 and L 1 are context free? What can we say about L 2 ? L 3 = L 1 L 2. Don’t Try to Use Closure Backwards 32
33
One Closure Theorem: If L 1 and L 2 are context free, then so is L 3 = L 1 L 2. But what if L 3 and L 1 are context free? What can we say about L 2 ? L 3 = L 1 L 2. Example: a n b n c * = a n b n c * a n b n c n. Don’t Try to Use Closure Backwards 33
34
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. 34
35
Why are the Context-Free Languages Not Closed under Complement, Intersection and Subtraction But the Regular Languages Are? Given an NDFSM M 1, build an FSM M 2 such that L(M 2 ) = L(M 1 ): 1. From M 1, 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 M 2 by setting it equal to M. Then swap the accepting and the nonaccepting states. So: M 2 = (K M, , M, s M, K M - A M ). 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. 35
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.