You are on page 1of 27

Positive Properties

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

The grammar of the union L1  L2


has new start variable S
and additional production S  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:

For context-free languages L1, L2


with context-free grammars G1, G2
and start variables S1, S 2

The grammar of the concatenation L1L2


has new start variable S
and additional production S  S1S 2

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:

For context-free language L


with context-free grammar G
and start variable S

The grammar of the star operation L *


has new start variable S1
and additional production S1  SS1 | 

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

L is context free L not necessarily


context-free

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

Construct a new NPDA machine M


that accepts L1  L2

M simulates in parallel M1 and M 2


18
NPDA M1 DFA M2
a, b  c a
q1 q2 p1 p2
transition transition

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:

M simulates in parallel M1 and M 2


M accepts string w if and only if

M1 accepts string w and


M 2 accepts string w

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

You might also like