Languages and Finite Automata

PDAs Accept
Context-Free Languages
Costas Busch - LSU
1
Theorem:
Context-Free
Languages
(Grammars)

Costas 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 )
Costas 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 )
Costas Busch - LSU
4
Proof - step 1
Convert
Context-Free Grammars
to
PDAs
Costas Busch - LSU
5
Take an arbitrary context-free grammar
We will convert
G
G
to a PDA M such that:
L(G )  L( M )
Costas Busch - LSU
6
Conversion Procedure:
For each
production in
For each
terminal in
G
Aw
G
a
Add transitions
, A  w
q0
,   S
a, a  
q1
Costas Busch - LSU
, $  $
q2
7
Example
Grammar
S  aSTb
PDA
S b
T  Ta
T 
q0
 , S  aSTb
, S  b
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas 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
Costas 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
Costas Busch - LSU
10
Grammar
Leftmost Derivation
PDA Computation


 xAy
 (q1 ,  i  n , Ay $)
 x i  j Bzy
 (q1 ,  i  n ,  i  j Bzy $)
Production applied
A   i  j Bz
Transition applied
 , A   i  j Bz
q0
,   S
Costas 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
Costas 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 $)
Last Transition applied
All symbols  i  j
have been removed
from top of stack
 j , j  
q0
,   S
Costas 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……
Costas Busch - LSU
14
Example:
Input
Time 0
q0
a b
a b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
15
Derivation: S
Input
Time 1
q0
a b
a b
S
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
16
Derivation: S  aSTb
Input
Time 2
q0
a b
a
S
T
b
$
a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
17
Derivation: S  aSTb
Input
Time 3
q0
a b
a
S
T
b
$
a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
18
Derivation: S  aSTb  abTb
Input
Time 4
q0
a b
b
T
b
$
a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
19
Derivation: S  aSTb  abTb
Input
Time 5
q0
a b
b
T
b
$
a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
20
Derivation: S  aSTb  abTb  abTab
Input
Time 6
q0
a b
T
a
b
$
a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
21
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 7
q0
a b
T
a
b
$
a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
22
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 8
q0
a b
a b
a
b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
23
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 9
q0
a b
a b
b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
,   S
q1
Costas Busch - LSU
, $  $
q2
24
Derivation: S  aSTb  abTb  abTab  abab
Input
a b
Time 10
a b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
,T  
b, b  
accept
q0
,   S
q1
Costas 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 ,  ,$)
Costas Busch - LSU
26
In general, it can be shown that:
Grammar
generates
string w
*
G
If and
Only if
Sw
Therefore
PDA M
accepts
w
*
(q0 , w,$) (q2 ,  ,$)
L(G )  L( M )
Costas Busch - LSU
27
Proof - step 2
Convert
PDAs
to
Context-Free Grammars
Costas Busch - LSU
28
Take an arbitrary PDA M
We will convert M
to a context-free grammar G such that:
L(M )  L(G )
Costas 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
Costas Busch - LSU
30
1. The PDA has a single accept state
PDA
PDA
M1
M
Old
accept
states
New
accept
state
qf
Costas 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
Costas 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  
 ,  
Costas 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
qi
, a  b
, a 
Costas Busch - LSU
qj
,   b
qj
34
PDA M3
PDA
M4
Where
qi
qi

,  
,  
qj
,    q
j
is a symbol of the stack alphabet
Costas Busch - LSU
35
PDA
M4 is the final modified PDA
Note that the new initial stack symbol #
is never used in any transition
Costas Busch - LSU
36
Example:
M
a,   a
b, a  
q
M4
,a  
,b  
,Z  
a,   a
b, a  
q0
 ,  @
q1
q2
 ,  a
 ,  Z
q3
,a  
Costas Busch - LSU
q4
 ,@  
q5
37
Grammar Construction
Variables:
Aqi ,q j
States of PDA
Costas Busch - LSU
38
PDA
Kind 1: for each state
q
Grammar
Aqq  
Costas Busch - LSU
39
PDA
Kind 2: for every three states
p
q
r
Grammar
Apq  Apr Arq
Costas Busch - LSU
40
PDA
Kind 3: for every pair of such transitions
p a,   t r
s b, t   q
Grammar
Apq  aArs b
Costas Busch - LSU
41
PDA
Initial state
Accept state
q0
qf
Grammar
Start variable
Aq0qf
Costas Busch - LSU
42
Example:
M4
PDA
,a  
,b  
,Z  
a,   a
b, a  
q0
 ,  @
q1
q2
 ,  a
 ,  Z
q3
,a  
Costas Busch - LSU
q4
 ,@  
q5
43
Grammar
Kind 1: from single states
Aq0 q0  
Aq1q1  
Aq2 q2  
Aq3q3  
Aq4 q4  
Aq5q5  
Costas 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
Costas Busch - LSU
45
Kind 3: from pairs of transitions
M4
,a  
,b  
,Z  
a,   a
b, a  
q0
 ,  @
q1
Aq0q5  Aq1q4
Aq1q4  Aq2q4
q2
 ,  a
 ,  Z
q3  , a   q 4
Aq2q4  aAq2q4
Aq2q2  aAq2q2 b
Aq2q4  aAq2q3
Costas Busch - LSU
 ,@  
q5
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 )
Costas 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, # ) (q f ,  , # )
with input string
w
Costas Busch - LSU
48
We will actually show that if G has derivation:

Apq  w
Then there is a computation in
M
:

( p, w,  ) (q,  ,  )
Costas Busch - LSU
49
Therefore:

Aq0qf w

(q0 , w,  ) (q f ,  ,  )
Since there is no transition
with the # symbol

(q0 , w, # ) (q f ,  , # )
Costas 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,  ,  )
Costas Busch - LSU
51
Proof Intuition:
Apq    w
Type 2
Case 1:
Apq  Apr Arq    w
Type 3
Case 2:
Apq  aArs b    w
Costas Busch - LSU
52
Type 2
Case 1:
Apq  Apr Arq    w
Stack
height
p
Input string
Generated by
Apr
Costas 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
Costas 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
Costas 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,  ,  )
Costas Busch - LSU
56
Induction Hypothesis:
Apq    w
k
derivation steps
suppose it holds:

( p, w,  ) (q,  ,  )
Costas Busch - LSU
57
Induction Step:
Apq    w
k 1
derivation steps
We have to show:

( p, w,  ) (q,  ,  )
Costas 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
Costas 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
Costas Busch - LSU
60
Apr    y
Arq    z
At most k steps
At most k steps
From induction
hypothesis, in PDA:
From induction
hypothesis, in PDA:


( p, y ,  )  ( r ,  ,  )
( r , z ,  )  ( q,  ,  )
Costas Busch - LSU
61


( p, y ,  )  ( r ,  ,  )
( r , z ,  )  ( q,  ,  )


( p, yz,  ) (r , z,  ) (q,  ,  )
since w  yz

( p, w,  ) (q,  ,  )
Costas 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
Costas Busch - LSU
63
Ars    y
At most k steps
From induction hypothesis,
the PDA has computation:

( r , y ,  )  ( s,  ,  )
Costas Busch - LSU
64
Type 3
Apq  aArs b    w
Grammar contains production
Apq  aArs b
And PDA Contains transitions
p a,   t r
s
Costas Busch - LSU
b, t  
q
65
p a,   t r
s b, t   q
( p, ayb,  )  (r , yb, t )
( s, b, t )  (q,  ,  )
Costas 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,  ,  )
Costas Busch - LSU
67

( p, ayb,  )  (r , yb, t ) ( s, b, t )  (q,  ,  )
since w  ayb

( p, w,  ) (q,  ,  )
END OF PROOF
Costas 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 )
Costas Busch - LSU
69