a,a b,a 0
q q
1 q
2 q
3 Execution Example:
Inputa a a b b b
current
stateb,a
Time 0, ,$$
Stack$
a,a b,a q
0 q
1 q
2 q
3 Inputa a a b b b b,a
Time 1, ,$$
Stack$
a,a b,a q
0 q
1 q
2 q
3 Input
Stacka a a b b b $ a b,a
Time 2, ,$$
a,a b,a q
0 q
1 q
2 q
3 Input
Stacka a a b b b $ a a b,a
Time 3, ,$$
a,a b,a q
0 q
1 q
2 q
3 Input
Stacka a a b b b $ a a a b,a
Time 4, ,$$
a,a b,a q
0 q
1 q
2 q
3 Inputa a a b b b
Stack$ a a a b,a
Time 5, ,$$
a,a b,a q
0 q
1 q
2 q
3 Inputa a a b b b $ a
Stackb,a
Time 6, ,$$ a
a,a b,a q
0 q
1 q
2 q
3 Inputa a a b b b $
Stackb,a
Time 7, ,$$ a
a,a b,a q
0 q
1 q
2 q
3 Inputa a a b b b b,a
Time 8
accept, ,$$ $
Stack
A string is accepted if there is
a computation such that:
•All the input is consumed
•The last state is a final state
At the end of the computation,
we do not care about the stack contents
The Language of PDA
Formal Definition
Non-Deterministic Pushdown Automaton
NPDA),,,δ,Γ,Σ,(
0
FzqQM
States
Input
alphabet
Stack
alphabet
Transition
function
Final
states
Stack
start
symbol
Initial
state
Instantaneous Description),,( suq
Current
state
Remaining
input
Current
stack
contents
Acceptance by Final State
L(M)= set of all strings w such that starting
from initial ID machine consumes w from
input and enters an accepting state.
The contents of the stack at that time is
irrelevant.
{w | (q0, w, Z)
*
(q, ,)
For some final state q of F and any stack
staring .
Acceptance by empty
stack
N(M) = (w | (q0,w,Z)
*
(q,,)
That is N(M) is the set of all strings w such
that M can consume w and at the same
time empties its stack.Here we do not
care whether q is a final state or not.
The input string
is accepted by the NPDA:aaabbb a,a b,a q
0 q
1 q
2 q
3 b,a , ,$$
}0:{ nbaL
nn is the language accepted by the NPDA:a,a b,a q
0 q
1 q
2 q
3 b,a
In general,, ,$$
Another NPDA example,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
,
NPDA M }{)(
R
wwML
Execution Example:
Input
Time 0
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a a b b
Inputa a b
Time 1
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a b
Input
Time 2
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a a a b b b
Input
Time 3
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a a a b b b
Guess the middle
of string
Input
Time 4
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a a a b b b
Input
Time 5
Stack$ ,$$ 1q q
2 bb
aa
,
, , q
0 bb
aa
,
, a a b b a
Input
Time 6
Stack$ ,$$ q
1 bb
aa
,
, , q
0 bb
aa
,
, a a b b
acceptq
2
Rejection Example:
Input
Time 0
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a b b b
Input
Time 1
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a a b b b
Input
Time 2
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a b a b b b
Input
Time 3
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a b
Guess the middle
of stringa b b b
Input
Time 4
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a b a b b b
Input
Time 5
Stack$ ,$$ 1q q
2 bb
aa
,
, , q
0 bb
aa
,
, a a b b b
There is no possible transition.
Input is not
consumed
Another computation on same string:
Input
Time 0
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a b b b
Input
Time 1
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a a b b b
Input
Time 2
Stack$ ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, a b a b b b
Input
Time 3
Stack$ a b a b b b ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, b
Input
Time 4
Stacka b b b ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, $ a b b b
Input
Time 5
Stacka b b b $ a b b b ,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
,
No final state
is reached
,$$ q
1 q
2 bb
aa
,
, , q
0 bb
aa
,
, There is no computation
that accepts stringabbb )(MLabbb
Pushing Stringsq
1 q
2 a,bw
Input
symbol
Pop
symbol
Push
string
q
1 q
2 a,bcdf a b top
input
stacka
Pushe h h e c d f
pushed
string
Example:$ $
Another NPDA example$$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0
NPDA M }:{)(
ba
nnwML
Time 0
Inputa a b b b
current
statea $
Stack$$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0
Execution Example:
Time 1
Inputa a b b b a $
Stack0 $$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0
Time 3
Inputa b b b a $
Stacka $ $$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0 0
Time 4
Inputa b b b a $
Stacka 1 $$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0
Time 5
Inputa b b b a $
Stacka 1 1 $$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0
Time 6
Inputa b b b a $
Stacka 1 $$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0 1
Time 7
Inputa b b b a $
Stacka 1 $$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0
Time 8
Inputa b b b a a $
Stack$$, q
1 q
2 a,$0$ a,000 a,1 b,$1$ b,111 b,0
accept
Formal Definition
Non-Deterministic Pushdown Automaton
NPDA),,,δ,Γ,Σ,(
0
FzqQM
States
Input
alphabet
Stack
alphabet
Transition
function
Final
states
Stack
start
symbol
Initial
state
Instantaneous Description),,( suq
Current
state
Remaining
input
Current
stack
contents
a,a b,a q
0 q
1 q
2 q
3 Input
Stacka a a b b b $ a a b,a
Time 4:, ,$$
Example: Instantaneous Description$),,(
1 aaabbbq a
a,a b,a q
0 q
1 q
2 q
3 Input
Stacka a a b b b $ a a b,a
Time 5:, ,$$
Example: Instantaneous Description$),,(
2aabbq a
We write:$),,($),,(
21 aabbqaaabbbq
Time 4 Time 5
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Theorem:
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Proof -Step 1:
Convert any context-free grammar
to a NPDA with: G M )()( MLGL
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Proof -Step 2:
Convert any NPDA to a context-free
grammar with: G M )()( MLGL
Converting
Context-Free
Grammars
to
NPDAs
An example grammar:T
TaT
bS
aSTbS
What is the equivalent NPDA?
q
0 q
1 2q S, ,$$ Grammar:
NPDA:T
TaT
bS
aSTbS
,
,
,
, T
TaT
bS
aSTbS bb
aa
,
,
The NPDA simulates
leftmost derivations of the grammar
L(Grammar) = L(NPDA)
Grammar:T
TaT
bS
aSTbS
A leftmost derivation:abababTababTbaSTbS
NPDA execution:0
q q
1 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
,
Input
Stack$ a a b
Time 0b
current
state
q
0 q
1 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 1b S
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 2b a b S T q
1
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 3b a b S T q
1
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 4b b T b q
1
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 5b b T b q
1
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 6b b T a q
1
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 7b b T a q
1
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 8b b a q
1
q
0 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 9b b q
1
q
0 q
1 2q S, ,$$ T
TaT
bS
aSTbS
,
,
,
, bb
aa
,
, Input
Stack$ a a b
Time 10b
accept
In general:
Given any grammar G
We can construct a NPDA M
With)()( MLGL
Constructing NPDA from grammar :q
0 q
1 2q S, ,$$ wA, aa, M
For any productionwA For any terminala G
Grammar generates string G w
if and only if
NPDA acceptsM w )()( MLGL
Therefore:
For any context-free language
there is an NPDA
that accepts the same language
Converting
NPDAs
to
Context-Free
Grammars
For any NPDAM
we will construct
a context-free grammar withG )()( GLML
Intuition:G The grammar simulates the machine
A derivation in Grammar : abcABCabcS
Current configuration in NPDA M
in NPDAM abcABCabcS
Input processed Stack contents
terminalsvariablesG
A derivation in Grammar :
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 Stackfq fq
Second, we modify the NPDA transitions:
all transitions will have formiq j
q Ba,
oriq j
q CDBa, symbolsstack :,,DCB
$, 0q fq a,$0$ a,000 a,1 b,$1$ b,111 b,0 }:{)(
ba
nnwML Example of a NPDA in correct form:symbolstack initial :$
The Grammar
Construction)(
ji
Bqq
In grammar :G
Terminals:
Input symbols of NPDA
states
Stack symbol
Variables:
iq j
q Ba, For each transition
We add productionaBqq
ji
)(
For each transition
We add production))(()(
klljki
DqqCqqaBqq iq j
q CDBa,
For all states lkqq,
Start Variable:)$(
fo
qq
Stack bottom symbol
Start state final state
Derivation of stringabba )$(
0f
qq )$)(0(
000 f
qqqqa )$(
0f
qqab )$)(1(
000 f
qqqqabb )$(
0f
qqabba abba
In general, in Grammar:wqq
f)$(
0
if and only ifw
is accepted by the NPDA
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 i
q j
q A A