Vous êtes sur la page 1sur 100

Courtesy Costas Busch - RPI 1

NPDAs Accept
Context-Free Languages
(12-13)
Courtesy Costas Busch - RPI 2
Instantaneous Description
) , , ( s u q
Current
state
Remaining
input
Current
stack
contents
3
Properties of Instantaneous Description
If an ID sequence is a legal computation for
a PDA, then so is the sequence obtained by
adding an additional string at the end of
component number two.
) , , ( ) , , (
) , , ( ) , , (
*
*
F E
F E
yw p xw q
y p x q
P
P
H
H

4
Properties of Instantaneous Description
If an ID sequence is a legal computation for
a PDA, then so is the sequence obtained by
adding an additional string at the bottom of
component number three.
) , , ( ) , , (
) , , ( ) , , (
*
*
FK EK
F E
y p x q
y p x q
P
P
H
H

5
Properties of Instantaneous Description
If an ID sequence is a legal computation for
a PDA, and some tail of the input is not
consumed, then removing this tail from all
ID's result in a legal computation sequence.
) , , ( ) , , (
) , , ( ) , , (
*
*
F E
F E
y p x q
yw p xw q
P
P
H
H

Languages of PDA
Acceptance by Final State
Language of NPDA : M
F q
q Z w q w M L
f
f

)} , , ( ) , , ( : { ) (
0 0
E P H
Initial state
Final state
) (M L
) , , , , , , (
0 0
F Z q Q M H + 7 !
7
Languages of PDA
Acceptance by Empty Stack
Language of NPDA : M
)} , , ( ) , , ( : { ) (
0 0
P P q Z w q w M N

! H
Initial state
Any state
) (M L
) , , , , , , (
0 0
F Z q Q M H + 7 !
8
From Empty Stack to Final State
) , , , , , (
0 0
Z q Q P
N
H + 7 !
F
P
If of some PDA ) (
N
P N L !
Then there is a PDA such that
) (
F
P L L !
9
From Empty Stack to Final State
Proof: Let
10
Property # 2
11
Then there is a PDA such that
From Final State to Empty Stack
) , , , , , , (
0 0
F Z q Q P
F
H + 7 !
N
P
If for some PDA
) (
F
P L L !
) (
N
P N L !
12
From Final State to Empty Stack
Proof: Let
13
from Property # 2 of ID
Courtesy Costas Busch - RPI 14
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
!
Theorem:
Courtesy Costas Busch - RPI 15
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
_
Proof - Step 1:
Convert any context-free grammar
to a NPDA with:
G
M
) ( ) ( M L G L !
Courtesy Costas Busch - RPI 16
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
_
Proof - Step 2:
Convert any NPDA to a context-free
grammar with:
G
M
) ( ) ( M L G L !
Courtesy Costas Busch - RPI 17
Converting
Context-Free Grammars
to
NPDAs
Proof - step 1
Courtesy Costas Busch - RPI 18
to an NPDA automaton M
We will convert any context-free grammar
G
Such that:
M
Simulates leftmost derivations of
G
Courtesy Costas Busch - RPI 19
. . . . .
m k
V V V S
2 1 2 1
o o o
/
. .
1
o
2
o
k
o
1 + k
o
$
1
V
2
V
m
V
Input
processed
Stack
contents
: G
: M
Input
Stack
leftmost variable
Leftmost derivation
Simulation of derivation
Courtesy Costas Busch - RPI 20
n
S o o o . . . .
2 1

. .
1
o
2
o
n
o
$
: G
: M
Input
Stack
Leftmost derivation
Simulation of derivation
string of terminals
end of input is reached
Courtesy Costas Busch - RPI 21
An example grammar:
P p
p
p
p
T
Ta T
b S
aSTb S
What is the equivalent NPDA?
Courtesy Costas Busch - RPI 22
q
0
q
1 2
q
S p P P,
P, $ p$
Grammar:
NPDA:
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P p
p
p
p
T
Ta T
b S
aSTb S
P
P
p
p
b b
a a
,
,
Courtesy Costas Busch - RPI 23
Grammar:
P p
p
p
p
T
Ta T
b S
aSTb S
A leftmost derivation:
abab abTab abTb aSTb S
Courtesy Costas Busch - RPI 24
0
q
q
1 2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b
Time 0
b
Derivation:
Courtesy Costas Busch - RPI 25
q
0
q
1 2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
S
Time 0
Derivation:
S
Courtesy Costas Busch - RPI 26
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
a
b
S
T
q
1
Time 1
Derivation:
aSTb S
Courtesy Costas Busch - RPI 27
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
a
b
S
T
q
1
Time 2
aSTb S
Derivation:
Courtesy Costas Busch - RPI 28
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
b
T
b
q
1
Time 3
Derivation:
abTb aSTb S
Courtesy Costas Busch - RPI 29
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
b
T
b
q
1
Time 4
Derivation:
abTb aSTb S
Courtesy Costas Busch - RPI 30
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
b
T
a
q
1
Time 5
Derivation:
abTab abTb aSTb S
Courtesy Costas Busch - RPI 31
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
b
T
a
q
1
Time 6
Derivation:
abab abTab abTb aSTb S
Courtesy Costas Busch - RPI 32
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
b
a
q
1
Time 7
Derivation:
abab abTab abTb aSTb S
Courtesy Costas Busch - RPI 33
q
0
2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
b
q
1
Time 8
Derivation:
Courtesy Costas Busch - RPI 34
q
0
q
1 2
q
S p P P,
P, $ p$
P P
P
P
P
p
p
p
p
T
Ta T
b S
aSTb S
,
,
,
,
P
P
p
p
b b
a a
,
,
Input
Stack
$
a
a
b b
accept
Time 9
Derivation:
Courtesy Costas Busch - RPI 35
In general:
Given any grammar
G
We can construct a NPDA
M
With ) ( ) ( M L G L !
}) { $, , , , , }, , , ({
2 0 2 1 0
q q T V T q q q M H !
) , , , ( P S T V G !
Courtesy Costas Busch - RPI 36
Constructing NPDA from grammar :
q
0
q
1 2
q
S p P P,
P, $ p$
w Ap , P P p a a,
M
For any production
w Ap
For any terminal
a
G
Courtesy Costas Busch - RPI 37
Grammar generates string
G
w
if and only if
NPDA accepts
M
w
) ( ) ( M L G L !
Courtesy Costas Busch - RPI 38
Therefore:
For any context-free language
there is a NPDA
that accepts the same language
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
_
Courtesy Costas Busch - RPI 39
Note: From CFG to PDA accepting by
emptying stack
Given any grammar
G
We can construct a NPDA
M
With
) ( ) ( M N G L !
) , , , ( P S T V G !
) , , , , }, ({ S q T V T q M H !
Courtesy Costas Busch - RPI 40
Constructing NPDA from grammar :
q
w Ap , P P p a a,
M
For any production
w Ap
For any terminal
a
G
Courtesy Costas Busch - RPI 41
Converting
NPDAs
to
Context-Free Grammars
Proof - step 2
Courtesy Costas Busch - RPI 42
For any NPDA M
we will construct
a context-free grammar with
G
) ( ) ( G L M L !
Courtesy Costas Busch - RPI 43
Intuition:
G
The grammar simulates the machine
A derivation in Grammar :
Current configuration in NPDA M
- - - - . abc ABC abc S
Input processed Stack contents
terminals variables
Courtesy Costas Busch - RPI 44
From NPDA to CFG
Lets look at how a PDA can consume
and empty the stack.
We shall define a grammar with variables of the
form [p
i-1
Y
i
p
i
] that would represent going from p
i-1
to p
i
with the net effect of popping Y
i
.
k
x x x x .
2 1
!
45
Q s r rYs pXr a qZs Z a q XY p
Q r pYr a qZr Z a q Y p
a qZp Z a q p
p
p
p
, : ] ][ [ ] [ ) , , ( ) , (
: ] [ ] [ ) , , ( ) , (
] [ ) , , ( ) , (
H
H
H I
) , , ( ) , , (
*
0
I I p Z w q
o
H
To generate all those strings
w that cause P to pop Z
0
from
its stack while going from q
0
to p.
Courtesy Costas Busch - RPI 46
47
48
} ], [ ], [ ], [ ], [ ], [ ], [ ], [ ], {[
0 0 0 0
S q pZ p pZ pXq pXp p qZ q qZ qXp qXq V !
49
Courtesy Costas Busch - RPI 50
Some Necessary Modifications
Modify (if necessary) the NPDA
(accepting by reaching final state) 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
Courtesy Costas Busch - RPI 51
1) Modify the NPDA so that
the stack is never empty
$
Stack
a
OK
$
OK NOT OK
Courtesy Costas Busch - RPI 52
$
a
$
Introduce the new symbol to denote
the bottom of the stack
#
$
a
#
$
#
#
Courtesy Costas Busch - RPI 53
Original NPDA
# , p P P
At the beginning push into the stack
#
original
initial state
new
initial state
Courtesy Costas Busch - RPI 54
i
q
j
q
b a p $ ,
i
q
j
q
b a p # ,
In transitions:
replace every instance of with $
#
Example:
Courtesy Costas Busch - RPI 55
if the automaton attempts to pop
or replace it will halt
$
Convert all transitions so that:
Courtesy Costas Busch - RPI 56
i
q
j
q
s p \ o,
i
q
j
q
x x p , P s p \ o,
{$} + x
Convert transitions as follows:
halting state
Courtesy Costas Busch - RPI 57
NPDA
f
q
P P p x ,
Empty the stack
{$} + x
2) Modify the NPDA so that
it empties the stack
and has a unique final state
Old final states
P P p $ ,
Courtesy Costas Busch - RPI 58
3) modify the NPDA so that
transitions have the following forms:
i
q
j
q
P o p B ,
OR
i
q
j
q
CD B p , o
symbols stack : , , D C B
Courtesy Costas Busch - RPI 59
i
q
j
q
y p P o,
Convert:
i
q
j
q
t t o y p ,
{$} + t
Courtesy Costas Busch - RPI 60
i
q
j
q
B Ap , o
Convert:
i
q
j
q
XB Ap , o P o p X ,
{$} + X
symbols
Courtesy Costas Busch - RPI 61
i
q
j
q
By Ap , o
Convert:
i
q
j
q
y Ap , o BX X p , o
{$} + X
symbols
2 | | > y
Convert recursively
Courtesy Costas Busch - RPI 62
P P p $ ,
0
q
f
q
a, $ p0$
a, 0 p00
a, 1p P
b, $ p1$
b, 1p11
b, 0 p P
} : { ) (
b a
n n w M L ! !
Example of a NPDA in correct form:
symbol stack initial : $
Courtesy Costas Busch - RPI 63
The Grammar Construction
) (
j i
Bq q
In grammar : G
Terminals:
Input symbols of NPDA
states
Stack symbol
Variables:
Courtesy Costas Busch - RPI 64
i
q
j
q
P p B a,
For each transition
We add production
a Bq q
j i
p ) (
Courtesy Costas Busch - RPI 65
For each transition
We add productions
) )( ( ) (
k l l j k i
Dq q Cq q a Bq q p
i
q
j
q
CD B a p ,
For all possible states
in the automaton
l k
q q ,
Courtesy Costas Busch - RPI 66
Start Variable:
) $ (
f o
q q
Stack bottom symbol
Start state
final state
Courtesy Costas Busch - RPI 67
Example:
P P p $ ,
0
q
f
q
a, $ p0$
a, 0 p00
a, 1p P
b, $ p1$
b, 1p11
b, 0 p P
a q q p ) 1 (
0 0
Grammar production:
Courtesy Costas Busch - RPI 68
Example:
P P p $ ,
0
q
f
q
a, $ p0$
a, 0 p00
a, 1p P
b, $ p1$
b, 1p11
b, 0 p P
) $ )( 1 ( | ) $ )( 1 ( ) $ (
) $ )( 1 ( | ) $ )( 1 ( ) $ (
0 0 0 0 0
0 0 0 0 0 0 0 0
f f f f f
f f
q q q q b q q q q b q q
q q q q b q q q q b q q
p
p
Grammar productions:
Courtesy Costas Busch - RPI 69
Example:
P P p $ ,
0
q
f
q
a, $ p0$
a, 0 p00
a, 1p P
b, $ p1$
b, 1p11
b, 0 p P
Grammar production:
P p ) $ (
0 f
q q
Courtesy Costas Busch - RPI 70
) $ )( 1 ( | ) $ )( 1 ( ) $ (
) $ )( 1 ( | ) $ )( 1 ( ) $ (
0 0 0 0 0
0 0 0 0 0 0 0 0
f f f f f
f f
q q q q b q q q q b q q
q q q q b q q q q b q q
p
p
) 1 )( 1 ( | ) 1 )( 1 ( ) 1 (
) 1 )( 1 ( | ) 1 )( 1 ( ) 1 (
0 0 0 0 0
0 0 0 0 0 0 0 0
f f f f f
f f
q q q q b q q q q b q q
q q q q b q q q q b q q
p
p
) $ )( 0 ( | ) $ )( 0 ( ) $ (
) $ )( 0 ( | ) $ )( 0 ( ) $ (
0 0 0 0 0
0 0 0 0 0 0 0 0
f f f f f
f f
q q q q a q q q q a q q
q q q q a q q q q a q q
p
p
Resulting Grammar:
able start vari : ) $ (
0 f
q q
Courtesy Costas Busch - RPI 71
) 0 )( 0 ( | ) 0 )( 0 ( ) 0 (
) 0 )( 0 ( | ) 0 )( 0 ( ) 0 (
0 0 0 0 0
0 0 0 0 0 0 0 0
f f f f f
f f
q q q q a q q q q a q q
q q q q a q q q q a q q
p
p
b q q
a q q
p
p
) 0 (
) 1 (
0 0
0 0
P p ) $ (
0 f
q q
Courtesy Costas Busch - RPI 72
Derivation of string
abba
) $ (
0 f
q q ) $ )( 0 (
0 0 0 f
q q q q a
) $ (
0 f
q q ab
) $ )( 1 (
0 0 0 f
q q q q abb
) $ (
0 f
q q abba abba
Courtesy Costas Busch - RPI 73
In general:
w Aq q
j i

) (
if and only if
the NPDA goes from to
by reading string and
the stack doesnt change below
and then is removed from stack
i
q
j
q
A
A
w
Courtesy Costas Busch - RPI 74
Therefore:
w q q
f

) $ (
0
if and only if
w is accepted by the NPDA
Courtesy Costas Busch - RPI 75
Therefore:
For any NPDA
there is a context-free grammar
that accepts the same language
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
_
Courtesy Costas Busch - RPI 76
Deterministic PDA
DPDA
Courtesy Costas Busch - RPI 77
Deterministic PDA: DPDA
q
1
q
2
w b a p ,
1
q
2
q
w b p , P
Allowed transitions:
(deterministic choices)
Courtesy Costas Busch - RPI 78
Allowed transitions:
q
1
q
2
1
, w b p P
q
3
2
, w c p P
q
1
q
2
1
, w b a p
q
3
2
, w c a p
(deterministic choices)
Courtesy Costas Busch - RPI 79
q
1
q
2
1
, w b a p
q
3
2
, w b a p
Not allowed:
(non deterministic choices)
q
1
q
2
1
, w b p P
q
3
2
, w b a p
Courtesy Costas Busch - RPI 80
DPDA example
a, P p a
b, a p P
q
0
q
1
q
2
q
3
b, a p P
P, $ p$
} 0 : { ) ( > ! n b a M L
n n
a, P p a
Courtesy Costas Busch - RPI 81
} 0 : { ) ( > ! n b a M L
n n
The language
is deterministic context-free
Courtesy Costas Busch - RPI 82
Definition:
A language is deterministic context-free
if there exists some DPDA that accepts it
L
Courtesy Costas Busch - RPI 83
Example of Non-DPDA (NPDA)
P, $ p$
q
1
q
2
b b
a a
p
p
P
P
,
,
P, P pP
q
0
P
P
p
p
b b
a a
,
,
} { ) (
R
ww M L !
Courtesy Costas Busch - RPI 84
P, $ p$
q
1
q
2
b b
a a
p
p
P
P
,
,
P, P pP
q
0
P
P
p
p
b b
a a
,
,
Not allowed in DPDAs
Courtesy Costas Busch - RPI 85
NPDAs
Have More Power than
DPDAs
Courtesy Costas Busch - RPI 86
Deterministic
Context-Free
Languages
(DPDA)
Context-Free
Languages
NPDAs
_
Since every DPDA is also a NPDA
It holds that:
Courtesy Costas Busch - RPI 87

We will actually show:


We will show that there exists
a context-free language which is not
accepted by any DPDA
L
L
L
Deterministic
Context-Free
Languages
(DPDA)
Context-Free
Languages
(NPDA)
Courtesy Costas Busch - RPI 88
The language is:
} { } {
2n n n n
b a b a L !
0 > n
We will show:
L
is context-free
L
is not deterministic context-free
Courtesy Costas Busch - RPI 89
} { } {
2n n n n
b a b a L !
Language is context-free L
Context-free grammar for : L
2 1
| S S S p
P |
1 1
b aS S p
P |
2 2
bb aS S p
} {
n n
b a
} {
2n n
b a
} { } {
2n n n n
b a b a
Courtesy Costas Busch - RPI 90
} { } {
2n n n n
b a b a L !
is not deterministic context-free
Theorem:
The language
(there is no DPDA that accepts ) L
Courtesy Costas Busch - RPI 91
Proof:
Assume for contradiction that
} { } {
2n n n n
b a b a L !
is deterministic context free
Therefore:
there is a DPDA that accepts M L
Courtesy Costas Busch - RPI 92
DPDA with M
n n
b a
n
b
accepts
n n
b a
2
accepts
n n
b a
} { } { ) (
2n n n n
b a b a M L !
Courtesy Costas Busch - RPI 93
DPDA with M
n n
b a
n
b
} { } { ) (
2n n n n
b a b a M L !
Such a path exists because of the determinism
M
Courtesy Costas Busch - RPI 94
The language
is not context-free
} {
n n n
c b a
(we will prove this at a later class using
pumping lemma for context-free languages)
act 1:
Regular languages
* *b a
Context-free languages
n n
b a
Courtesy Costas Busch - RPI 95
The language
is not context-free
} {
n n n
c b a L
}) { } { (
2n n n n
b a b a L !
Fact 2:
(we can prove this using pumping lemma
for context-free languages)
Courtesy Costas Busch - RPI 96
We will construct a NPDA that accepts:
} {
n n n
c b a L
}) { } { (
2n n n n
b a b a L !
which is a contradiction!
Courtesy Costas Busch - RPI 97
Modify M
M
'
n
c
n n
c a
} { } { ) (
2n n n n
b a b a M L !
} { } { ) (
2n n n n
c a c a M L !
'
Replace
with
b
c
n n
b a
n
b
M
Courtesy Costas Busch - RPI 98
The NPDA that accepts
} {
n n n
c b a L
n n
b a
n
b
n
c
M
'
M
n n
c a
P P
Connect final states of
with final states of
M
'
M
Courtesy Costas Busch - RPI 99
Since is accepted by a NPDA } {
n n n
c b a L
it is context-free
Contradiction!
(since is not context-free) } {
n n n
c b a L
Courtesy Costas Busch - RPI 100
Therefore:
} { } {
2n n n n
b a b a L !
There is no DPDA that accepts
End of Proof
Not deterministic context free

Vous aimerez peut-être aussi