We have seen that the regular languages are closed under common set-theoretic
operations; the same, however, does not hold true for context-free languages.
Lemma: The context-free languages are closed under union, concatenation
and Kleene closure.
That is, if and are context-free languages, so are ,
and .
Proof:
We will prove that the languages are closed by creating the appropriate grammars.
Suppose we have two context-free languages, represented by grammars
with start symbols and respectively.
First of all, rename all the terminal symbols in the second grammar so that
they don't conflict with those in the first. Then:
Lemma: The context-free languages are not closed under
intersection
That is, if and are context-free languages, it it not always true
that is also.
Proof:
We will prove the non-closure of intersection by exhibiting a counter-example.
Consider the following two languages:
The intersection of these languages is:
Lemma: The context-free languages are not closed under
complementation.
That is, if is a context-free language, it it not always true
that is also.
Proof: (By contradiction)
Suppose that context-free languages are closed under complementation.
Then if and are context-free languages, so are and .
Since we have proved closure under union,
must also
be context-free, and, by our assumption, so must its complement
.
However, by de Morgan's laws (for sets),
,
so this must also be a context-free language.
Since our choice of and was arbitarary, we have contradicted the non-closure
of intersection, and have thus proved the lemma.