Transcript pda-cfl-11

NPDAs Accept
Context-Free Languages
Costas Busch - RPI
1
Theorem:
Context-Free
Languages
(Grammars)

Costas Busch - RPI
Languages
Accepted by
NPDAs
2
Proof - Step 1:
Context-Free
Languages
(Grammars)

Languages
Accepted by
NPDAs
Convert any context-free grammar G
to a NPDA M with: L(G )  L( M )
Costas Busch - RPI
3
Proof - Step 2:
Context-Free
Languages
(Grammars)

Languages
Accepted by
NPDAs
Convert any NPDA M to a context-free
grammar G with: L(G )  L( M )
Costas Busch - RPI
4
Proof - step 1
Converting
Context-Free Grammars
to
NPDAs
Costas Busch - RPI
5
We will convert any context-free grammar
G
to an NPDA automaton M
Such that:
M Simulates leftmost derivations of G
Costas Busch - RPI
6
Leftmost derivation
S    1 2  kV1V2 Vm  
G:
Input
processed
Stack
contents
leftmost variable
M:
Simulation of derivation
Input
1  2

 k  k 1

Stack
V1
V2

Vm
$
Costas Busch - RPI
7
Leftmost derivation
S    1 2  n
G:
string of terminals
M:
Simulation of derivation
Stack
Input
1  2

n
$
end of input is reached
Costas Busch - RPI
8
An example grammar:
S  aSTb
S b
T  Ta
T 
What is the equivalent NPDA?
Costas Busch - RPI
9
Grammar:
S  aSTb
NPDA:
S b
T  Ta
T 
q0
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
10
Grammar: S  aSTb
S b
T  Ta
T 
A leftmost derivation:
S  aSTb  abTb  abTab  abab
Costas Busch - RPI
11
Derivation:
Input
Time 0
q0
a b a b
 , S  aSTb
$
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
12
Derivation: S
Input
Time 0
q0
a b a b
S
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
13
Derivation: S  aSTb
Input
Time 1
q0
a
S
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
14
Derivation: S  aSTb
Input
Time 2
q0
a
S
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
15
Derivation: S  aSTb  abTb
Input
Time 3
q0
b
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
16
Derivation: S  aSTb  abTb
Input
Time 4
q0
b
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
17
Derivation: S  aSTb  abTb  abTab
Input
Time 5
q0
T
a
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
18
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 6
q0
T
a
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
19
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 7
q0
a b a b
a
b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
20
Derivation:
Input
Time 8
q0
a b a b
b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
21
Derivation:
Input
Time 9
a b a b
 , S  aSTb
$
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
accept
q0
,   S
q1
Costas Busch - RPI
, $  $
q2
22
In general:
Given any grammar
G
We can construct a NPDA
With
M
L(G)  L( M )
Costas Busch - RPI
23
Constructing NPDA
M from grammar G :
For any production
For any terminal
Aw
a
, A  w
q0
,   S
a, a  
q1
Costas Busch - RPI
, $  $
q2
24
Grammar
G generates string w
if and only if
NPDA
M accepts w
L(G)  L( M )
Costas Busch - RPI
25
Therefore:
For any context-free language
there is a NPDA
that accepts the same language
Context-Free
Languages
(Grammars)

Costas Busch - RPI
Languages
Accepted by
NPDAs
26
Proof - step 2
Converting
NPDAs
to
Context-Free Grammars
Costas Busch - RPI
27
For any NPDA
M
we will construct
a context-free grammar
G with
L( M )  L(G)
Costas Busch - RPI
28
Intuition: The grammar simulates the machine
A derivation in Grammar
terminals
G:
variables
S    abc ABC     abc
Input processed
Stack contents
Current configuration in NPDA
Costas Busch - RPI
M
29
Some Necessary Modifications
Modify (if necessary) the NPDA so that:
1) The stack is never empty
2) It has a single final state
and empties the stack when it accepts a string
3) Has transitions in a special form
Costas Busch - RPI
30
1) Modify the NPDA so that
the stack is never empty
Stack
a
$
$
OK
OK
Costas Busch - RPI
NOT OK
31
Introduce the new symbol
the bottom of the stack
#
to denote
a
$
$
a
#
#
$
$
Costas Busch - RPI
#
32
At the beginning push
new
initial state
#
into the stack
Original NPDA
 ,  #
original
initial state
Costas Busch - RPI
33
In transitions:
replace every instance of
Example:
qi
qi
$
with
#
a, $  b q
j
a, #  b
Costas Busch - RPI
qj
34
Convert all transitions so that:
if the automaton attempts to pop
or replace $ it will halt
Costas Busch - RPI
35
Convert transitions as follows:
qi
,  s q
j
x    {$}
qi
, x  x
,  s q
j
halting state
Costas Busch - RPI
36
2) Modify the NPDA so that
it empties the stack
and has a unique final state
Empty the stack
NPDA
, x  
x    {$}
, $   q
f
Old final states
Costas Busch - RPI
37
3) modify the NPDA so that
transitions have the following forms:
qi
, B 
qj
OR
qi
 , B  CD q
j
B, C , D : stack symbols
Costas Busch - RPI
38
Convert:
qi
qi
,   y
 ,   y
qj
qj
    {$}
Costas Busch - RPI
39
Convert:
symbols
qi
qi
, A B
 , A  XB
qj
, X 
qj
X    {$}
Costas Busch - RPI
40
| y | 2
Convert:
qi
 , A  By
Convert recursively
qi
, A y
symbols
qj
 , X  BX
X    {$}
Costas Busch - RPI
qj
41
Example of a NPDA in correct form:
L(M )  {w : na  nb}
$ : initial stack symbol
a, $  0$
a, 0  00
a, 1 
q0
b, $ 1$
b, 1 11
b, 0  
, $  
Costas Busch - RPI
qf
42
The Grammar Construction
In grammar
G:
Stack symbol
Variables:
(qi Bq j )
states
Terminals:
Input symbols of NPDA
Costas Busch - RPI
43
For each transition
We add production
qi
a, B  
qj
(qi Bq j )  a
Costas Busch - RPI
44
For each transition
a
,
B

CD
qj
qi
We add productions
(qi Bqk )  a (q j Cql )(ql Dqk )
For all possible states qk , ql
in the automaton
Costas Busch - RPI
45
Stack bottom symbol
Start Variable:
(qo $q f )
Start state
Costas Busch - RPI
final state
46
Example:
a, $  0$
a, 0  00
a, 1 
b, $ 1$
b, 1 11
b, 0  
, $  
q0
Grammar production:
qf
(q01q0 )  a
Costas Busch - RPI
47
Example:
a, $  0$
a, 0  00
a, 1 
q0
b, $ 1$
b, 1 11
b, 0  
, $  
qf
Grammar productions:
(q0 $q0 )  b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )
(q0 $q f )  b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )
Costas Busch - RPI
48
Example:
a, $  0$
a, 0  00
a, 1 
q0
b, $ 1$
b, 1 11
b, 0  
, $  
qf
Grammar production: ( q0 $q f )  
Costas Busch - RPI
49
Resulting Grammar:
(q0 $q f ) : start variable
(q0 $q0 )  b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )
(q0 $q f )  b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )
(q01q0 )  b(q01q0 )(q01q0 ) | b(q01q f )(q f 1q0 )
(q01q f )  b(q01q0 )(q01q f ) | b(q01q f )(q f 1q f )
(q0 $q0 )  a(q0 0q0 )(q0 $q0 ) | a(q0 0q f )(q f $q0 )
(q0 $q f )  a(q0 0q0 )(q0 $q f ) | a(q0 0q f )(q f $q f )
Costas Busch - RPI
50
(q0 0q0 )  a(q0 0q0 )(q0 0q0 ) | a(q0 0q f )(q f 0q0 )
(q0 0q f )  a(q0 0q0 )(q0 0q f ) | a(q0 0q f )(q f 0q f )
(q01q0 )  a
(q0 0q0 )  b
(q0 $q f )  
Costas Busch - RPI
51
Derivation of string
abba
(q0 $q f )  a (q0 0q0 )(q0 $q f ) 
ab(q0 $q f ) 
abb(q01q0 )(q0 $q f ) 
abba(q0 $q f )  abba
Costas Busch - RPI
52
In general:

(qi Aq j )  w
if and only if
the NPDA goes from qi to q j
by reading string w and
the stack doesn’t change below A
and then A is removed from stack
Costas Busch - RPI
53
Therefore:

(q0 $q f )  w
if and only if
w is accepted by the NPDA
Costas Busch - RPI
54
Therefore:
For any NPDA
there is a context-free grammar
that accepts the same language
Context-Free
Languages
(Grammars)

Costas Busch - RPI
Languages
Accepted by
NPDAs
55
Deterministic PDA
DPDA
Costas Busch - RPI
56
Deterministic PDA: DPDA
Allowed transitions:
q1
q1
a, b  w
, b  w
q2
q2
(deterministic choices)
Costas Busch - RPI
57
Allowed transitions:
a, b  w1
 , b  w1
q2
q1
q2
q1
a , c  w2
 , c  w2
q3
q3
(deterministic choices)
Costas Busch - RPI
58
Not allowed:
a, b  w1
 , b  w1
q2
q1
q2
q1
a , b  w2
a , b  w2
q3
q3
(non deterministic choices)
Costas Busch - RPI
59
DPDA example
L( M )  {a b : n  0}
n n
a,   a
b, a  
a,


a
b,
a



,
$

$
q3
q2
q0
q1
Costas Busch - RPI
60
The language
L( M )  {a b : n  0}
n n
is deterministic context-free
Costas Busch - RPI
61
Definition:
A language L is deterministic context-free
if there exists some DPDA that accepts it
Costas Busch - RPI
62
Example of Non-DPDA (NPDA)
L( M )  {ww }
R
a,   a
b,   b
q0
a, a  
b, b  
,   
q1
Costas Busch - RPI
, $  $
q2
63
Not allowed in DPDAs
a,   a
b,   b
q0
a, a  
b, b  
,   
q1
Costas Busch - RPI
, $  $
q2
64
NPDAs
Have More Power than
DPDAs
Costas Busch - RPI
65
It holds that:
Deterministic
Context-Free
Languages
(DPDA)

Context-Free
Languages
NPDAs
Since every DPDA is also a NPDA
Costas Busch - RPI
66
We will actually show:
Deterministic
Context-Free
Languages
(DPDA)

Context-Free
Languages
(NPDA)
L
L
We will show that there exists
a context-free language L which is not
accepted by any DPDA
Costas Busch - RPI
67
The language is:
L  {a b }  {a b }
n n
n 2n
n0
We will show:
• L is context-free
• L is not deterministic context-free
Costas Busch - RPI
68
L  {a b }  {a b }
n n
Language
n 2n
L is context-free
Context-free grammar for
S  S1 | S2
L:
{a nbn }  {a nb2n }
S1  aS1b | 
S2  aS2bb | 
n n
{a b }
n 2n
{a b }
Costas Busch - RPI
69
Theorem:
The language
L  {a b }  {a b }
n n
n 2n
is not deterministic context-free
(there is no DPDA that accepts
Costas Busch - RPI
L)
70
Proof: Assume for contradiction that
L  {a b }  {a b }
n n
n 2n
is deterministic context free
Therefore:
there is a DPDA
M that accepts L
Costas Busch - RPI
71
DPDA
M with L( M )  {a b }  {a b }
n n
accepts
n 2n
n n
a b
n n
a b
b
n
n 2n
a
accepts
Costas Busch - RPI
b
72
DPDA
M with L( M )  {a b }  {a b }
n n
n 2n
Such a path exists because of the determinism
M
n n
a b
b
Costas Busch - RPI
n
73
Fact 1:
n n n
The language {a b
is not context-free
c }
Context-free languages
a nb n
Regular languages
a *b *
(we will prove this at a later class using
pumping lemma for context-free languages)
Costas Busch - RPI
74
Fact 2:
The language L  {a
is not context-free
n n n
b c }
( L  {a b }  {a b })
n n
n 2n
(we can prove this using pumping lemma
for context-free languages)
Costas Busch - RPI
75
We will construct a NPDA that accepts:
L  {a b c }
n n n
( L  {a b }  {a b })
n n
n 2n
which is a contradiction!
Costas Busch - RPI
76
L( M )  {a b }  {a b }
n n
M
a nb n
Modify
M
bn
Replace
with c
M
n 2n
b
L( M )  {a nc n }  {a nc 2n }
a nc n
cn
Costas Busch - RPI
77
L  {a b c }
n n n
The NPDA that accepts
Connect final states of M 
with final states of M
M
a nc n
c
n

M
a nbn

bn
Costas Busch - RPI
78
L  {a b c } is accepted by a NPDA
n n n
Since
it is context-free
Contradiction!
(since
L  {a b c } is not context-free)
n n n
Costas Busch - RPI
79
Therefore:
Not deterministic context free
L  {a b }  {a b }
n n
n 2n
There is no DPDA that accepts
End of Proof
Costas Busch - RPI
80