Chapter 17: Context-Free Languages

Topics
Introduction
The CFL’s are closed under È
Lemma
The CFL’s are closed under product
The CFL’s are closed under Kleene closure
The CFL’s are not closed under Ç
The CFL’s are not closed under ¢ (complementation)
The deterministic CFL’s are closed under ¢ (complementation)
Not every CFL can be accepted by a deterministic PDA
The intersection of a regular language and a CFL is a CFL


Introduction

Recall that the regular languages are closed under the following set operations:

How about the context-free languages? Let’s see.


The CFL’s are closed under È
We show two proofs:

Proof by grammar, by example

Language X’s grammar: S ® Ba | Bc, B ® b
Language Y’s grammar: S ® BaC, B ® b, C ® c

Rename the nonterminals so that there is no overlap:

X: S1 ® Ba | Bc, B ® b
Y: S2 ® DaC, D ® b, C ® c
Grammar for X È Y:
S ® S1 | S2
S1 ® Ba | Bc, B ® b
S2 ® DaC, D ® b, C ® c
Proof by machine, by example
 
Language X’s machine:  Language Y’s machine:

(the rest of the machine)

(the rest of the machine)

Language X È Y’s machine (merge start states):


Lemma
Any PDA that accepts string xy after it has read the x-prefix, has an equivalent PDA that will also (1) accept with an empty stack, and (2) accept after reading each character from y.

Replace any accepting state:

by:






The CFL’s are closed under product

proof by grammar, by example

Language X’s grammar: S ® Ba | Bc, B ® b
Language Y’s grammar: S ® BaC, B ® b, C ® c

Rename the nonterminals so that there is no overlap

X: S1 ® Ba | Bc, B ® b
Y: S2 ® DaC, D ® b, C ® c
The grammar for XY:
S ® S1S2
S1 ® Ba | Bc, B ® b
S2 ® DaC, D ® b, C ® c
proof by machine

Call the machines for X and Y, MX and MY, resp..

1. alter MX so that it will enter an accepting state according to the above Lemma.

2.

if MX has accepting state:  and MY has start state:

 "wire" them together like so, thus "passing by" MX’s accepting state and MY’s start state:

Remember, a PDA can accept a string before it has been completely read. If, for instance, MX accepts the string "aaabcdef" after it sees the b, and MY accepts a "ggg", we still need to be able to accept "aaabcdefggg". But since we have modified MX according to the Lemma, it will also accept after reading the c, and after reading the d, etc., so that along one path it will consume the rest of the string "aaabcdef" before going on to recognize (via MY’s part of the combined machine) the ggg.


The CFL’s are closed under Kleene closure

proof by grammar, by example

Language X’s grammar: S ® Ba | Bc, B ® b

Rename X’s sentence symbol: S1 ® Ba | Bc, B ® b

Add two new productions to form X*’s grammar:

S ® S1S, S ®l , S1 ® Ba | Bc, B ® b
The CFL’s are not closed under Ç

The proof is by counter-example.

L1 = anbncm (equal numbers of a’s and b’s, followed by arbitrary many c’s).
                    L1 = (anbn)(cn), the product of two CFL’s, hence is CF.
L2 = ambncn (arbitrary many a’s, followed by equal numbers of c’s and d’s). Likewise CF.

Now think about L3 = L1 Ç L2. Any string of L3 must have #a’s = #b’s and #b’s = # c’s. That is,
L3 = anbncn, which we know not to be a CFL.

Of course, some intersections of CFL’s are CF:
If L1 = PALINDROME, and L2 = a+b+a+, then L1 Ç L2 = anbmcn, which is easily seen to be CF.


The CFL’s are not closed under ¢ (complementation)

(proof by contradiction)

Assume the CFL’s are closed under complementation. Then if L1and L2 are CFL, then so are

1. L1¢ and L2¢ (by assumption)
2. L1¢ È L2¢ (CFL’s are closed under È )
3. (L1¢ È L2¢ )¢ (again, by assumption)

But the last is (by DeMorgan’s law) just L1 Ç L2. But the CFL's are not closed under Ç. Contradiction, so the assumption must be false, and the theorem true.

Again, some complements of CFL’s are CF.


The deterministic CFL’s are closed under ¢ (complementation)

(proof by machine)

The deterministic CFL’s are those that can be accepted by a deterministic PDA. For such a PDA, we can simply change each ACCEPT state to a REJECT state, and vice-versa. The new machine will accept the complement of the language of the old machine.

This trick will not work for a non-deterministic machine. Suppose an input leads to both ACCEPT and REJECT states. By definition, it will be accepted.  Use the above trick to create a machine to reject the input. That input will still terminate in an ACCEPT and a REJECT state, and the string will still be accepted.


Not every CFL can be accepted by a deterministic PDA

If it could, then its complement would also be a CFL, contrary to the theorem that the CFL’s are not closed under complementation.


The intersection of a regular language and a CFL is a CFL

(see proof in book)

Hosted by www.Geocities.ws

1