Languages and Finite Automata

Download Report

Transcript Languages and Finite Automata

Properties
of
Context-Free languages
Prof. Busch - LSU
1
Union
Context-free languages
are closed under:
Union
L1
is context free
L2
is context free
L1  L2
is context-free
Prof. Busch - LSU
2
Example
Language
Grammar
L1  {a b }
S1  aS1b | 
L2  {ww }
S2  aS2a | bS2b | 
n n
R
Union
L  {a b }  {ww }
n n
R
Prof. Busch - LSU
S  S1 | S2
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
Prof. Busch - LSU
L1,
G1,
S1,
L2
G2
S2
L1  L2
S
S  S1 | S2
4
Concatenation
Context-free languages
Concatenation
are closed under:
L1
is context free
L1L2
L2
is context free
is context-free
Prof. Busch - LSU
5
Example
Language
Grammar
L1  {a b }
S1  aS1b | 
L2  {ww }
S2  aS2a | bS2b | 
n n
R
Concatenation
L  {a b }{ww }
n n
R
Prof. Busch - LSU
S  S1S2
6
In general:
For context-free languages
with context-free grammars
and start variables
L1,
G1,
S1,
L2
G2
S2
The grammar of the concatenation L1L2
S
has new start variable
and additional production S  S1S2
Prof. Busch - LSU
7
Star Operation
Context-free languages
Star-operation
are closed under:
*
L is context free
L is context-free
Prof. Busch - LSU
8
Example
Language
Grammar
L  {a b }
S  aSb | 
n n
Star Operation
S1  SS1 | 
L  {a b } *
n n
Prof. Busch - LSU
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 | 
Prof. Busch - LSU
10
Negative Properties
of
Context-Free Languages
Prof. Busch - LSU
11
Intersection
Context-free languages
intersection
are not closed under:
L1
is context free
L2
is context free
L1  L2
not necessarily
context-free
Prof. Busch - LSU
12
Example
L1  {a b c }
L2  {a b c }
Context-free:
Context-free:
n n m
n m m
S  AC
A  aAb | 
C  cC | 
S  AB
A  aA | 
B  bBc | 
Intersection
L1  L2  {a b c } NOT context-free
n n n
Prof. Busch - LSU
13
Complement
Context-free languages
complement
are not closed under:
L is context free
L
Prof. Busch - LSU
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
A  aAb | 
C  cC | 
S  AB
A  aA | 
B  bBc | 
Complement
L1  L2  L1  L2  {a b c }
n n n
NOT context-free
Prof. Busch - LSU
15
Intersection
of
Context-free languages
and
Regular Languages
Prof. Busch - LSU
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
Prof. Busch - LSU
17
Machine
M1
NPDA for
L1
Machine
M2
DFA for
L2
regular
context-free
Construct a new NPDA machine
that accepts L1  L2
M
simulates in parallel
Prof. Busch - LSU
M1
and
M
M2
18
NPDA
q1
DFA
M1
a, b  c
q2
a
p1
transition
M2
p2
transition
NPDA
M
q1, p1 a, b  c q2 , p2
transition
Prof. Busch - LSU
19
NPDA
q1
DFA
M1
, b  c
q2
M2
p1
transition
NPDA
M
q1, p1  , b  c q2 , p1
transition
Prof. Busch - LSU
20
NPDA
DFA
M1
M2
q0
p0
initial state
initial state
NPDA
M
q0 , p0
Initial state
Prof. Busch - LSU
21
NPDA
DFA
M1
p1
q1
final state
M2
p2
final states
NPDA
M
q1, p1
q1, p2
final states
Prof. Busch - LSU
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
Prof. Busch - LSU
23
regular
*
L2  {a, c}
DFA
M2
a, c
p0
Prof. Busch - LSU
24
context-free
Automaton for: L1  L2  {a nc n : n  0}
NPDA
a,   1
q0 , p0
M
c,1  
 ,    q1, p0  ,    q2 , p0  ,   
Prof. Busch - LSU
q3 , p0
25
In General:
M
simulates in parallel
M1
and
M2
M accepts string w if and only if
M1
accepts string
w
M2
accepts string
w
and
L( M )  L( M1)  L( M 2 )
Prof. Busch - LSU
26
Therefore:
M is NPDA
L( M1)  L(M 2 )
L1  L2
is context-free
is context-free
Prof. Busch - LSU
27
Applications
of
Regular Closure
Prof. Busch - LSU
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
context-free
Prof. Busch - LSU
29
An Application of Regular Closure
Prove that:
n n
L  {a b : n  100, n  0}
is context-free
Prof. Busch - LSU
30
We know:
n n
{a b : n  0}
is context-free
Prof. Busch - LSU
31
We also know:
L1  {a
100 100
b
L1  {(a  b) }  {a
*
is regular
}
100 100
b
Prof. Busch - LSU
}
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
Prof. Busch - LSU
33
}
Another Application of Regular Closure
Prove that:
L  {w : na  nb  nc }
is not context-free
Prof. Busch - LSU
34
If
L  {w : na  nb  nc }
is context-free
(regular closure)
Then
L  {a * b * c*}  {a b c }
context-free
n n n
regular
context-free
Impossible!!!
Therefore,
L is not context free
Prof. Busch - LSU
35