Closure Properties of Context

Closure Properties
of
Context-Free languages
Summer 2004
COMP 335
1
Union
Context-free languages
are closed under:
Union
L1
is context free
L2
is context free
L1 ∪ L2
Summer 2004
is context-free
COMP 335
2
Example
Language
Grammar
L1 = {a b }
S1 → aS1b | λ
L2 = {ww }
S 2 → aS 2 a | bS 2b | λ
n n
R
Union
L = {a b } ∪ {ww }
n n
Summer 2004
R
COMP 335
S → S1 | S 2
3
In general:
For context-free languages
with context-free grammars
and start variables
The grammar of the union
has new start variable
and additional production
Summer 2004
COMP 335
L1, L2
G1, G2
S1, S 2
L1 ∪ L2
S
S → S1 | S 2
4
Concatenation
Context-free languages
Concatenation
are closed under:
L1
is context free
L1L2
L2
is context free
Summer 2004
is context-free
COMP 335
5
Example
Language
Grammar
L1 = {a b }
S1 → aS1b | λ
L2 = {ww }
S 2 → aS 2 a | bS 2b | λ
n n
R
Concatenation
S → S1S 2
L = {a b }{ww }
n n
Summer 2004
R
COMP 335
6
In general:
For context-free languages
with context-free grammars
and start variables
L1, L2
G1, G2
S1, S 2
The grammar of the concatenation L1L2
S
has new start variable
and additional production S → S1S 2
Summer 2004
COMP 335
7
Star Operation
Context-free languages
are closed under:
Star-operation
L
*
is context free
Summer 2004
L
COMP 335
is context-free
8
Example
Language
Grammar
L = {a b }
S → aSb | λ
n n
Star Operation
S1 → SS1 | λ
L = {a b } *
n n
Summer 2004
COMP 335
9
In general:
For context-free language
with context-free grammar
and start variable
L
G
S
The grammar of the star operation L *
S1
has new start variable
and additional production S1 → SS1 | λ
Summer 2004
COMP 335
10
Negative Properties
of
Context-Free Languages
Summer 2004
COMP 335
11
Intersection
Context-free languages
are not closed under:
intersection
L1
is context free
L2
is context free
L1 ∩ L2
Summer 2004
not necessarily
context-free
COMP 335
12
Example
L1 = {a b c }
L2 = {a b c }
Context-free:
Context-free:
n n m
n m m
S → AC
S → AB
A → aAb | λ
A → aA | λ
C → cC | λ
B → bBc | λ
Intersection
L1 ∩ L2 = {a b c } NOT context-free
n n n
Summer 2004
COMP 335
13
Complement
Context-free languages
are not closed under:
complement
L
is context free
Summer 2004
L
COMP 335
not necessarily
context-free
14
Example
L1 = {a b c }
L2 = {a b c }
Context-free:
Context-free:
n n m
n m m
S → AC
S → AB
A → aAb | λ
A → aA | λ
C → cC | λ
B → bBc | λ
Complement
L1 ∪ L2 = L1 ∩ L2 = {a b c }
n n n
NOT context-free
Summer 2004
COMP 335
15
Intersection
of
Context-free languages
and
Regular Languages
Summer 2004
COMP 335
16
The intersection of
a context-free language and
a regular language
is a context-free language
L1
context free
L1 ∩ L2
L2
regular
Summer 2004
context-free
COMP 335
17
Machine
M1
NPDA for
L1
Machine
M2
DFA for
L2
regular
context-free
Construct a new NPDA machine
that accepts L1 ∩ L2
M
Summer 2004
simulates in parallel
COMP 335
M1
and
M
M2
18
NPDA
a, b → c
q1
DFA
M1
q2
a
p1
transition
M2
p2
transition
NPDA
M
a
,
b
→
c
q1, p1
q2 , p2
transition
Summer 2004
COMP 335
19
NPDA
λ, b → c
q1
DFA
M1
q2
M2
p1
transition
NPDA
M
λ
,
b
→
c
q1, p1
q2 , p1
transition
Summer 2004
COMP 335
20
NPDA
DFA
M1
M2
q0
p0
initial state
initial state
NPDA
M
q0 , p0
Initial state
Summer 2004
COMP 335
21
NPDA
DFA
M1
p1
q1
final state
M2
p2
final states
NPDA
q1, p1
M
q1, p2
final states
Summer 2004
COMP 335
22
Example:
context-free
L1 = {w1w2 : | w1 |=| w2 |, w1 ∈{a, b}* , w2 ∈{c, d }*}
NPDA
a, λ → 1
b, λ → 1
M1
c,1 → λ
d ,1 → λ
q0 λ , λ → λ q1 λ , λ → λ q2 λ ,$ → λ q3
Summer 2004
COMP 335
23
regular
*
L2 = {a, c}
DFA
M2
a, c
p0
Summer 2004
COMP 335
24
context-free
Automaton for: L1 ∩ L2 = {a c : n ≥ 0}
n n
NPDA
a, λ → 1
q0 , p0
Summer 2004
M
c,1 → λ
λ , λ → λ q1, p0 λ , λ → λ q2 , p0 λ ,$ → λ
COMP 335
q3 , p0
25
In General:
M
M
simulates in parallel
accepts string
w
M1
and
M2
if and only if
M1
accepts string
w
M2
accepts string
w
and
L ( M ) = L ( M1 ) ∩ L ( M 2 )
Summer 2004
COMP 335
26
Therefore:
M
is NPDA
L( M1 ) ∩ L( M 2 )
L1 ∩ L2
Summer 2004
is context-free
is context-free
COMP 335
27
Applications
of
Regular Closure
Summer 2004
COMP 335
28
The intersection of
a context-free language and
a regular language
is a context-free language
L1
Regular Closure
context free
L1 ∩ L2
L2
regular
Summer 2004
context-free
COMP 335
29
An Application of Regular Closure
Prove that:
n n
L = {a b : n ≠ 100, n ≥ 0}
is context-free
Summer 2004
COMP 335
30
We know:
n n
{a b : n ≥ 0}
Summer 2004
is context-free
COMP 335
31
We also know:
L1 = {a
100 100
b
L1 = {( a + b) } − {a
*
Summer 2004
is regular
}
100 100
COMP 335
b
}
is regular
32
n n
{a b }
context-free
(regular closure)
n n
L1 = {( a + b) } − {a
*
100 100
b
regular
{a b } ∩ L1
n n
context-free
n n
{a b } ∩ L1 = {a b : n ≠ 100, n ≥ 0} = L
is context-free
Summer 2004
COMP 335
33
}
Another Application of Regular Closure
Prove that:
L = {w : na = nb = nc }
is not context-free
Summer 2004
COMP 335
34
If
L = {w : na = nb = nc } is context-free
(regular closure)
Then
L ∩ {a * b * c*} = {a b c }
n n n
context-free
regular
context-free
Impossible!!!
Therefore,
Summer 2004
L
is not context free
COMP 335
35
Decidable Properties
of
Context-Free Languages
Summer 2004
COMP 335
36
Membership Question:
for context-free grammar
find if string w ∈ L (G )
G
Membership Algorithms: Parsers
• Exhaustive search parser
• CYK parsing algorithm
Summer 2004
COMP 335
37
Empty Language Question:
for context-free grammar
find if L (G ) = ∅
G
Algorithm:
1. Remove useless variables
2. Check if start variable
Summer 2004
COMP 335
S
is useless
38
Infinite Language Question:
for context-free grammar
find if L (G ) is infinite
G
Algorithm:
1. Remove useless variables
2. Remove unit and
λ
productions
3. Create dependency graph for variables
4. If there is a loop in the dependency graph
then the language is infinite
Summer 2004
COMP 335
39
Example:
S → AB
A → aCb | a
B → bB | bb
C → cBS
Infinite language
Dependency graph
A
C
S
B
Summer 2004
COMP 335
40
S → AB
A → aCb | a
B → bB | bb
C → cBS
S ⇒ AB ⇒ aCbB ⇒ acBSbB ⇒ acbbSbbb
∗
∗
S ⇒ acbbSbbb ⇒(acbb) S (bbb)
∗
⇒(acbb) S (bbb)
Summer 2004
i
2
2
i
COMP 335
41