PDAs Accept
Context-Free Languages
Prof. Busch - LSU
1
Theorem:
Context-Free
Languages
(Grammars)
Prof. Busch - LSU
Languages
Accepted by
PDAs
2
Proof - Step 1:
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Convert any context-free grammar G
to a PDA M with: L(G ) L( M )
Prof. Busch - LSU
3
Proof - Step 2:
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Convert any PDA M to a context-free
grammar G with: L(G ) L( M )
Prof. Busch - LSU
4
Proof - step 1
Convert
Context-Free Grammars
to
PDAs
Prof. Busch - LSU
5
Take an arbitrary context-free grammar
We will convert
G
G
to a PDA M such that:
L(G ) L( M )
Prof. Busch - LSU
6
Conversion Procedure:
For each
production in
For each
terminal in
G
Aw
G
a
Add transitions
, A w
q0
, S
a, a
q1
Prof. Busch - LSU
, $ $
q2
7
Example
Grammar
S aSTb
S b
T Ta
T
q0
PDA
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
8
PDA simulates leftmost derivations
Grammar
Leftmost Derivation
PDA Computation
(q0 , 1 k k 1 n ,$)
S
(q1 , 1 k k 1 n , S $)
1 k X 1 X m
(q1 , k 1 n , X 1 X m $)
1 k k 1 n
(q2 , ,$)
Scanned
symbols
Stack
contents
Prof. Busch - LSU
9
Grammar
Leftmost Derivation
Terminals
Leftmost
variable
Variables
or terminals
xAy
x i j Bzy
Production applied
A i j Bz
Terminals
Variables
or terminals
Variable
Prof. Busch - LSU
10
Grammar
Leftmost Derivation
PDA Computation
xAy
(q1 , i n , Ay $)
x i j Bzy
Production applied
A i j Bz
(q1 , i n , i j Bzy $)
Transition applied
, A i j Bz
q0 , S
Prof. Busch - LSU
q1
, $ $
q2
11
Grammar
Leftmost Derivation
PDA Computation
xAy
(q1 , i n , Ay $)
x i j Bzy
(q1 , i n , i j Bzy $)
(q1 , i 1 n , i 1 j Bzy $)
Transition applied
Read i from input
and remove it from stack
q0 , S
Prof. Busch - LSU
i ,i
q1
, $ $
q2
12
Grammar
Leftmost Derivation
xAy
(q1 , i n , Ay $)
x i j Bzy
(q1 , i n , i j Bzy $)
PDA Computation
(q1 , i 1 n , i 1 j Bzy $)
(q1 , j 1 n , Bzy $)
All symbols i j
have been removed
from top of stack
Last Transition applied
j , j
q0 , S
Prof. Busch - LSU
q1
, $ $
q2
13
The process repeats with the next
leftmost variable
xAy
x i j Bzy
(q1 , j 1 n , Bzy $)
x i j j 1 k Cpzy
(q1 , j 1 n , j 1 k Cpzy $)
(q1 , k 1 n , Cpzy $)
Production applied
B j 1 k Cp
And so on……
Prof. Busch - LSU
14
Example:
Input
Time 0
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
$
Stack
q2
15
Derivation: S
Input
Time 1
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
S
$
Stack
q2
16
Derivation: S aSTb
Input
Time 2
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
a
S
T
b
$
Stack
q2
17
Derivation: S aSTb
Input
Time 3
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
a
S
T
b
$
Stack
q2
18
Derivation: S aSTb abTb
Input
Time 4
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
b
T
b
$
Stack
q2
19
Derivation: S aSTb abTb
Input
Time 5
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
b
T
b
$
Stack
q2
20
Derivation: S aSTb abTb abTab
Input
Time 6
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
T
a
b
$
Stack
q2
21
Derivation: S aSTb abTb abTab abab
Input
Time 7
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
T
a
b
$
Stack
q2
22
Derivation: S aSTb abTb abTab abab
Input
Time 8
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
a
b
$
Stack
q2
23
Derivation: S aSTb abTb abTab abab
Input
Time 9
q0
a b
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
b
$
Stack
q2
24
Derivation: S aSTb abTb abTab abab
Input
a b
Time 10
a b
, S aSTb
, S b
a, a
, T Ta
b, b
, T
$
Stack
accept
q0
, S
q1
Prof. Busch - LSU
, $ $
q2
25
Grammar
Leftmost Derivation
S
aSTb
abTb
abTab
abab
PDA Computation
(q0 , abab,$)
(q1 , abab, S $)
(q1 , bab, STb$)
(q1 , bab, bTb$)
(q1 , ab, Tb$)
(q1 , ab, Tab$)
(q1 , ab, ab$)
(q1 , b, b$)
(q1 , ,$)
(q2 , ,$)
Prof. Busch - LSU
26
In general, it can be shown that:
Grammar
generates
string w
*
G
If and
Only if
Sw
Therefore
PDA M
accepts
w
*
(q0 , w,$) (q2 , ,$)
L(G ) L( M )
Prof. Busch - LSU
27
Proof - step 2
Convert
PDAs
to
Context-Free Grammars
Prof. Busch - LSU
28
Take an arbitrary PDA M
We will convert M
to a context-free grammar G such that:
L(M ) L(G )
Prof. Busch - LSU
29
First modify PDA
M so that:
1. The PDA has a single accept state
2. Use new initial stack symbol #
3. On acceptance the stack contains only
stack symbol # (this symbol is not used in any transition)
4. Each transition either pushes a symbol
or pops a symbol but not both together
Prof. Busch - LSU
30
1. The PDA has a single accept state
PDA
PDA
M1
M
Old
accept
states
New
accept
state
qf
Prof. Busch - LSU
31
2. Use new initial stack symbol #
Top of stack
initial stack symbol of M
Z
@
auxiliary stack symbol
#
new initial stack symbol
PDA M2
, @
, Z
PDA M1
M1 still thinks that Z is the initial stack
Prof. Busch - LSU
32
3. On acceptance the stack contains only
stack symbol #
(this symbol is not used in any transition)
PDA M3
Empty stack
x {@, # }
PDA M2
Old
accept
state
, x
,
Prof. Busch - LSU
, @
New
accept
state
qf
33
4. Each transition either pushes a symbol
or pops a symbol but not both together
PDA
PDA
M4
M3
qi
, a b
,
a
qi
Prof. Busch - LSU
qj
, b q
j
34
PDA M3
PDA
,
qi
M4
Where
, q
qi
j
, q
j
is a symbol of the stack alphabet
Prof. Busch - LSU
35
PDA
M4 is the final modified PDA
Note that the new initial stack symbol #
is never used in any transition
Prof. Busch - LSU
36
Example:
M
a, a
b, a
q
M4
, a
, b
, Z
a, a
b, a
q0
, @
q1
q2
, a
q3
, a
q4
, @
q5
, Z
Prof. Busch - LSU
37
Grammar Construction
Variables:
Aqi ,q j
States of PDA
Prof. Busch - LSU
38
PDA
Kind 1: for each state
q
Grammar
Aqq
Prof. Busch - LSU
39
PDA
Kind 2: for every three states
p
q
r
Grammar
Apq Apr Arq
Prof. Busch - LSU
40
PDA
Kind 3: for every pair of such transitions
p
a, t
b,t q
s
r
Grammar
Apq aArs b
Prof. Busch - LSU
41
PDA
Initial state
Accept state
q0
qf
Grammar
Start variable
Aq0qf
Prof. Busch - LSU
42
Example:
PDA
M4
, a
, b
, Z
a, a
b, a
q0
, @
q1
q2
, a
q3
, a
q4
, @
q5
, Z
Prof. Busch - LSU
43
Grammar
Kind 1: from single states
Aq0q0
Aq1q1
Aq2q2
Aq3q3
Aq4q4
Aq5q5
Prof. Busch - LSU
44
Kind 2: from triplets of states
Aq0q0 Aq0q0 Aq0q0 | Aq0q1 Aq1q0 | Aq0q2 Aq2q0 | Aq0q3 Aq3q0 | Aq0q4 Aq4q0 | Aq0q5 Aq5q0
Aq0q1 Aq0q0 Aq0q1 | Aq0q1 Aq1q1 | Aq0q2 Aq2q1 | Aq0q3 Aq3q1 | Aq0q4 Aq4q1 | Aq0q5 Aq5q1
Aq0q5 Aq0q0 Aq0q5 | Aq0q1 Aq1q5 | Aq0q2 Aq2q5 | Aq0q3 Aq3q5 | Aq0q4 Aq4q5 | Aq0q5 Aq5q5
Aq5q5 Aq5q0 Aq0q5 | Aq5q1 Aq1q5 | Aq5q2 Aq2q5 | Aq5q3 Aq3q5 | Aq5q4 Aq4q5 | Aq5q5 Aq5q5
Start variable
Aq0q5
Prof. Busch - LSU
45
Kind 3: from pairs of transitions
M4
, a
, b
, Z
a, a
b, a
q0
, @
q1
q2
, a
q3
, a
q4
, @
q5
, Z
Aq0q5 Aq1q4
Aq1q4 Aq2q4
Aq2q4 aAq2q4
Aq2q2 aAq2q2 b
Aq2q4 aAq2q3
Prof. Busch - LSU
Aq2q2 Aq3q2 b
Aq2q4 Aq3q3
Aq2q4 Aq3q4
46
Suppose that a PDA M is converted
to a context-free grammar G
We need to prove that
L(G ) L(M )
or equivalently
L(G ) L(M )
L(G ) L(M )
Prof. Busch - LSU
47
L(G ) L(M )
We need to show that if
Aq0qf w
G
has derivation:
(string of terminals)
Then there is an accepting computation in
M
:
(q0 ,w , # ) (qf , , # )
with input string
w
Prof. Busch - LSU
48
We will actually show that if G has derivation:
Apq w
Then there is a computation in
M
:
( p,w , ) (q , , )
Prof. Busch - LSU
49
Therefore:
Aq0qf w
(q0 ,w , ) (qf , , )
Since there is no transition
with the # symbol
(q0 ,w , # ) (qf , , # )
Prof. Busch - LSU
50
Lemma:
If
Apq w
(string of terminals)
then there is a computation
from state p to state q on string
which leaves the stack empty:
w
( p,w , ) (q , , )
Prof. Busch - LSU
51
Proof Intuition:
Apq w
Type 2
Case 1:
Apq Apr Arq w
Type 3
Case 2:
Apq aArs b w
Prof. Busch - LSU
52
Type 2
Case 1:
Apq Apr Arq w
Stack
height
p
Input string
Generated by
Apr
Prof. Busch - LSU
r
Generated by
q
Arq
53
Type 3
Case 2:
Apq aArs b w
Stack
height
r
p
Input string
s
a
Generated by
Prof. Busch - LSU
Ars
b q
54
Formal Proof:
We formally prove this claim
by induction on the number
of steps in derivation:
Apq w
number of steps
Prof. Busch - LSU
55
Induction Basis:
Apq w
(one derivation step)
A Kind 1 production must have been used:
App
Therefore, p q
and
w
This computation of PDA trivially exists:
( p , , ) ( p , , )
Prof. Busch - LSU
56
Induction Hypothesis:
Apq w
k
derivation steps
suppose it holds:
( p,w , ) (q , , )
Prof. Busch - LSU
57
Induction Step:
Apq w
k 1
derivation steps
We have to show:
( p,w , ) (q , , )
Prof. Busch - LSU
58
Apq w
k 1
derivation steps
Type 2
Case 1:
Apq Apr Arq w
Type 3
Case 2:
Apq aArs b w
Prof. Busch - LSU
59
Type 2
Case 1:
Apq Apr Arq w
k 1 steps
We can write
w yz
Apr y
Arq z
At most k steps
At most k steps
Prof. Busch - LSU
60
Apr y
Arq z
At most k steps
From induction
hypothesis, in PDA:
( p , y , ) (r , , )
At most k steps
From induction
hypothesis, in PDA:
(r , z , ) (q , , )
Prof. Busch - LSU
61
( p , y , ) (r , , )
(r , z , ) (q , , )
( p, yz , ) (r , z , ) (q , , )
since w yz
( p,w , ) (q , , )
Prof. Busch - LSU
62
Type 3
Case 2:
Apq aArs b w
k 1
We can write
steps
w ayb
Ars y
At most k steps
Prof. Busch - LSU
63
Ars y
At most k steps
From induction hypothesis,
the PDA has computation:
(r , y , ) (s , , )
Prof. Busch - LSU
64
Type 3
Apq aArs b w
Grammar contains production
Apq aArs b
And PDA Contains transitions
p
a, t
b,t q
s
r
Prof. Busch - LSU
65
p
a, t
b,t q
s
r
(s , b,t ) (q , , )
( p, ayb , ) (r , yb ,t )
Prof. Busch - LSU
66
We know
(r , y , ) (s , , )
(r , yb ,t ) (s , b ,t )
( p, ayb , ) (r , yb ,t )
We also know
(s , b,t ) (q , , )
Therefore:
( p, ayb , ) (r , yb ,t ) (s , b,t ) (q , , )
Prof. Busch - LSU
67
( p, ayb , ) (r , yb ,t ) (s , b,t ) (q , , )
since w ayb
( p,w , ) (q , , )
END OF PROOF
Prof. Busch - LSU
68
So far we have shown:
L(G ) L(M )
With a similar proof we can show
L(G ) L(M )
Therefore:
L(G ) L(M )
Prof. Busch - LSU
69
© Copyright 2026 Paperzz