Title: CSI 3104 Winter 2006: Introduction to Formal Languages Chapter 17: ContextFree Languages
1CSI 3104 /Winter 2006 Introduction to Formal
Languages Chapter 17 Context-Free Languages
- Chapter 17 Context-Free Languages
- I. Theory of Automata
- ? II. Theory of Formal Languages
- III. Theory of Turing Machines
2Chapter 17 Context-Free Languages
- Theorem. The set of context-free languages is
closed under union, concatenation, and Kleene
closure. - Union. L1L2
- L1 and L2 are generated by two context-free
grammars G1 and G2. We replace each nonterminal X
in G1 by X1, and each nonterminal X in G2 by X2.
We add the productions - S ? S1 S ? S2
- L1L2 is the language generated by this new CFG.
3Chapter 17 Context-Free Languages
- Example L1 PALINDROME S ? aSa bSb a
b ? - L2 anbn S ? aSb ?
- L1L2 S ? S1 S2
- S1 ? aS1a bS1b a b
? - S2 ? aS2b ?
- Example L1 aa,bb S ? aA bB A ? a
B ? b - L2 ? S ? ?
- L1L2 S ? S1 S2
- S1 ? aA1 bB1 A1 ?
a B1 ? b - S2 ? ?
4Chapter 17 Context-Free Languages
Proof by machines
5Chapter 17 Context-Free Languages
(begin with a)
(contain aa)
6Chapter 17 Context-Free Languages
L1 L2
7Chapter 17 Context-Free Languages
- Concatenation. L1L2
- Similar to union except we add
- S ? S1S2
- Example
- L1 palindrome S ? aSa bSb a b ?
- L2 anbn S ? aSb ?
- L1L2 S ? S1S2
- S1 ? aS1a bS1b a b ?
- S2 ? aS2b ? proof by PDAs?
8L
- Kleene star. L
- We replace S by S1 and add
- S ? S1S ?
- S ? S1S ? S1S1S ? S1S1S1S ?
- Example L S ? aSa bSb a b ?
- L S ? S1S ?
- S1 ? aS1a bS1b a b ?
9Chapter 17 Context-Free Languages
- Theorem. The intersection of two context-free
languages may or may not be context-free. - L1 anbnam
- S ? XA
- X ? aXb / ab
- A ? aA / a
- L2 anbmam
- S ? AX
- X ? bXa / ba
- A ? aA / a
- L1 n L2 anbnan (is not a context-free
language )
10Chapter 17 Context-Free Languages
- Theorem. The intersection of a context-free
language and a regular language is always
context-free. - Proof By constructive algorithm using pushdown
automata, similar to the intersection algorithm
for two finite automata. - (details not covered)
11Example use the theorem
- L doubleword ww abbabb,
- Assume L is CFL
- L n abab anbmanbm
- is then context free (n regular lang.)
- But we have proven that anbmanbm is not CFL
- ? contradiction.
12Chapter 17 Context-Free Languages
- Theorem. The complement of a context-free
language may or may not be context-free. - When the PDA is deterministic with other
properties, we could use for the complement a
similar technique as for FA
13Chapter 17 Context-Free Languages
PALINDROME-X
14Chapter 17 Context-Free Languages
For the complement We interchange the states
ACCEPT and REJECT. It does not work all the
time.
15Complement of a CFL may not be a CFL
- Proof by indirect arguments
- Suppose complement of every CFL is a CFL.
- L1 and L2 CFLs ? L1 and L2 CFLs ? L1L2 CFLs
? (L1L2) L1 ? L2 is CFL - Contradicts intersection theorem.
16Nondeterminism
- L accepts w if some paths lead to accept (some
paths may lead to reject) - L rejects w if all paths lead to reject
- Exchange accept and reject states
- L rejects w if some paths lead to reject ??
- L accepts w if all paths lead to accept ??
- Nondeterminism does not support complement
17Example
- Lanbnan is not CFL but L is CFL
- LMpqMqpMprMrpMqrMrqM
- Mpqapbqar pgtq aanbna CFL
- Mqpapbqar qgtp anbnba CFL
- Mprapbqar pgtr aanban CFL
- Mqr, Mrq, Mrp similarly CFL
- Mabc complement of a regular language
18Deterministic PDA lt Nondeterministic PDA
- Proof Complement of anbncn is CFL and cannot
have deterministic PDA since otherwise its
complement has deterministic PDA and would be CFL
(accept ?reject )