Professional Documents
Culture Documents
of
Context-Free languages
1
Union
Context-free languages
are closed under: Union
L1 is context free
L1 L2
L2 is context free is context-free
2
Example
Language Grammar
n n
L1 {a b } S1 aS1b |
R
L2 {ww } S2 aS2 a | bS2b |
Union
n n R S S1 | S 2
L {a b } {ww }
3
In general:
For context-free languages L1, L2
with context-free grammars G1, G2
and start variables S1, S 2
4
Concatenation
Context-free languages
are closed under: Concatenation
L1 is context free
L1L2
L2 is context free is context-free
5
Example
Language Grammar
n n
L1 {a b } S1 aS1b |
R
L2 {ww } S2 aS2 a | bS2b |
Concatenation
n n R S S1S 2
L {a b }{ww }
6
In general:
7
Star Operation
Context-free languages
are closed under: Star-operation
L is context free *
L is context-free
8
Example
Language Grammar
n n
L {a b } S aSb |
Star Operation
n n
L {a b } * S1 SS1 |
9
In general:
10
Negative Properties
of
Context-Free Languages
11
Intersection
Context-free languages
are not closed under: intersection
L1 is context free
L1 L2
L2 is context free not necessarily
context-free
12
Example
n n m n m m
L1 {a b c } L2 {a b c }
Context-free: Context-free:
S AC S AB
A aAb | A aA |
C cC | B bBc |
Intersection
n n n
L1 L2 {a b c } NOT context-free
13
Complement
Context-free languages
are not closed under: complement
14
Example
n n m n m m
L1 {a b c } L2 {a b c }
Context-free: Context-free:
S AC S AB
A aAb | A aA |
C cC | B bBc |
Complement
n n n
L1 L2 L1 L2 {a b c }
NOT context-free
15
Intersection
of
Context-free languages
and
Regular Languages
16
The intersection of
a context-free language and
a regular language
is a context-free language
L1 context free
L1 L2
L2 regular context-free
17
Machine M1 Machine M2
NPDA for L1 DFA for L2
context-free regular
NPDA M
q1, p1 a , b c
q2 , p2
transition
19
NPDA M1 DFA M2
, b c
q1 q2 p1
transition
NPDA M
q1, p1 , b c
q2 , p1
transition
20
NPDA M1 DFA M2
q0 p0
initial state initial state
NPDA M
q0 , p0
Initial state
21
NPDA M1 DFA M2
q1 p1 p2
final state final states
NPDA M
q1, p1 q1, p2
final states
22
Example: context-free
* *
L1 {w1w2 : | w1 || w2 |, w1 {a, b} , w2 {c, d } }
NPDA M1
a, 1 c,1
b, 1 d ,1
q0 , q1 , q2 , q3
23
regular
*
L2 {a, c}
DFA M2
a, c
p0
24
context-free
Automaton for: L1 L2 {a c : n 0} n n
NPDA M
a, 1 c,1
q0 , p0 , q1, p0 , q2 , p0 , q3 , p0
25
In General:
L ( M ) L ( M1 ) L ( M 2 )
26
Therefore:
M is NPDA
L( M1) L( M 2 ) is context-free
L1 L2 is context-free
27