1
… NPDAs continued
2
Pushing Strings
q1 q2a, bw
Input
symbol
Pop
symbolPush
string
3
q1 q2a, bcdf
a
b top
input
stack
a
Push
eh h
e
cdf
pushed
string
Example:
$ $
4
Another NPDA example
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
NPDA M
}:{)( ba nnwML
5
Time 0
Input
a ab b b
current
state
a
$
Stack
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
Execution Example:
6
Time 1
Input
a ab b ba
$
Stack
0
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
7
Time 3
Input
a bb b a
$
Stack
a
$
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
0
8
Time 4
Input
a bb b a
$
Stack
a
1
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
9
Time 5
Input
a bb b a
$
Stack
a
1
1
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
10
Time 6
Input
a bb b a
$
Stack
a
1
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
1
11
Time 7
Input
a bb b a
$
Stack
a
1
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
12
Time 8
Input
a bb b a a
$
Stack
$$, q1 q2
a, $0$
a, 000a,1
b, $1$
b,111b, 0
accept
13
Formalities for NPDAs
14
q1 q2a, bw
)},{(),,( 21 wqbaq
Transition function:
15
q1
q2wba ,
q3wba ,
)},(),,{(),,( 321 wqwqbaq
Transition function:
16
Formal Definition
Non-Deterministic Pushdown Automaton
NPDA
),,,δ,Γ,Σ,( 0 FzqQM
States
Input
alphabetStack
alphabet
Transition
function
Final
states
Stack
start
symbolInitial
state
17
Instantaneous Description
),,( suq
Current
state Remaining
input
Current
stack
contents
18
a,a
b, aq0 q1 q2 q3
Input
Stack
a a a b b b
$
a
a
b, a
Time 4:
, , $$
Example: Instantaneous Description
$),,( 1 aaabbbq
a
19
a,a
b, aq0 q1 q2 q3
Input
Stack
a a a b b b
$
a
a
b, a
Time 5:
, , $$
Example: Instantaneous Description
$),,( 2 aabbq
a
20
We write:
$),,($),,( 21 aabbqaaabbbq
Time 4 Time 5
21
a,a
b, aq0 q1 q2 q3
b, a
, , $$
,$),(,$),($),,($),,(
$),,($),,($),,(
,$),(,$),(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
A computation:
22
,$),(,$),($),,($),,(
$),,($),,($),,(
,$),(,$),(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
For convenience we write:
,$),(,$),( 30 qaaabbbq
23
Formal Definition
Language of NPDA :M
)}',,(),,(:{)( 0 sqswqwML f
Initial state Final state
24
Example:,$),(,$),( 30 qaaabbbq
a,a
b, aq0 q1 q2 q3
b, a
, , $$
NPDA :M
)(MLaaabbb
25
,$),(,$),( 30 qbaq nn
a,a
b, aq0 q1 q2 q3
b, a
, , $$
NPDA :M
)(MLba nn
26
a,a
b, aq0 q1 q2 q3
b, a
, , $$
NPDA :M
}0:{)( nbaML nnTherefore:
27
NPDAs Accept Context-Free Languages
28
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Theorem:
29
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Proof - Step 1:
Convert any context-free grammar
to a NPDA with:
GM )()( MLGL
30
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Proof - Step 2:
Convert any NPDA to a context-free
grammar with: G
M)()( MLGL
31
ConvertingContext-Free Grammars
to NPDAs
32
An example grammar:
T
TaT
bS
aSTbS
What is the equivalent NPDA?
33q0 q1 2q
S, , $$
Grammar:
NPDA:
T
TaT
bS
aSTbS
,
,
,
,
T
TaT
bS
aSTbS
bb
aa
,
,
34
The NPDA simulates
leftmost derivations of the grammar
L(Grammar) = L(NPDA)
35
Grammar:
T
TaT
bS
aSTbS
A leftmost derivation:
abababTababTbaSTbS
36
NPDA execution:
0q q1 2qS, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 0
b
current
state
37q0 q1 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 1
b
S
38q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 2
b
a
b
ST
q1
39q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 3
b
a
b
ST
q1
40q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 4
b
bTb
q1
41q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 5
b
bTb
q1
42q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 6
b
b
Ta
q1
43q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 7
b
b
Ta
q1
44q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 8
b
b
a
q1
45q0 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 9
b
b
q1
46q0 q1 2q
S, , $$
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 10
b
accept
47
In general:
Given any grammar G
We can construct a NPDA M
With )()( MLGL
48
Constructing NPDA from grammar :
q0 q1 2qS, , $$
wA, aa,
M
For any production
wA
For any terminal
a
G
49
Grammar generates string G w
if and only if
NPDA acceptsM w
)()( MLGL
50
Therefore:
For any context-free language
there is an NPDA
that accepts the same language
51
ConvertingNPDAs
toContext-Free Grammars
52
For any NPDA M
we will construct
a context-free grammar withG
)()( GLML
53
Intuition:
G
The grammar simulates the machine
A derivation in Grammar :
abcABCabcS
Current configuration in NPDA M
54
in NPDA M
abcABCabcS
Input processed Stack contents
terminals variables
GA derivation in Grammar :
55
Some Necessary Modifications
First, we modify the NPDA:
• It has a single final state
• It empties the stack
when it accepts the input
Original NPDA Empty Stack
fq
fq
56
Second, we modify the NPDA transitions:
all transitions will have form
iq jqBa,
or
iq jqCDBa ,
symbolsstack :,, DCB
57
$,0q fq
a, $0$
a, 000a,1
b, $1$
b,111b, 0
}:{)( ba nnwML
Example of a NPDA in correct form:
symbolstack initial :$
58
The Grammar Construction
)( jiBqq
In grammar :G
Terminals:
Input symbols of NPDA
states
Stack symbol
Variables:
59
iq jqBa,For each transition
We add production aBqq ji )(
60
For each transition
We add production ))(()( klljki DqqCqqaBqq
iq jqCDBa ,
For all states lk qq ,
61
Start Variable: )$( fo qq
Stack bottom symbol
Start state final state
62
Example:
$,0q fq
a, $0$
a, 000a,1
b, $1$
b,111b, 0
aqq )1( 00Grammar production:
63
Example:
$,0q fq
a, $0$
a, 000a,1
b, $1$
b,111b, 0
)$)(1(|)$)(1()$(
)$)(1(|)$)(1()$(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
Grammar productions:
64
Example:
$,0q fq
a, $0$
a, 000a,1
b, $1$
b,111b, 0
Grammar production: )$( 0 fqq
65
)$)(1(|)$)(1()$(
)$)(1(|)$)(1()$(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
)1)(1(|)1)(1()1(
)1)(1(|)1)(1()1(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
)$)(0(|)$)(0()$(
)$)(0(|)$)(0()$(
00000
00000000
fffff
ff
qqqqaqqqqaqq
qqqqaqqqqaqq
Resulting Grammar: ablestart vari:)$( 0 fqq
66
)0)(0(|)0)(0()0(
)0)(0(|)0)(0()0(
00000
00000000
fffff
ff
qqqqaqqqqaqq
qqqqaqqqqaqq
bqq
aqq
)0(
)1(
00
00
)$( 0 fqq
67
Derivation of string abba
)$( 0 fqq )$)(0( 000 fqqqqa
)$( 0 fqqab
)$)(1( 000 fqqqqabb
)$( 0 fqqabba abba
68
In general, in Grammar:
wqq f
)$( 0
if and only if
w is accepted by the NPDA
69
Explanation:
By construction of Grammar:
wAqq ji
)(
if and only if
in the NPDA going from to
the stack doesn’t change below
and is removed from stack
iq jq
AA