Pushdown Automata & Context

Download Report

Transcript Pushdown Automata & Context

PDAs Accept
Context-Free Languages
Fall 2006
Costas Busch - RPI
1
Theorem:
Context-Free
Languages
(Grammars)
Fall 2006

Costas Busch - RPI
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 )
Fall 2006
Costas Busch - RPI
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 )
Fall 2006
Costas Busch - RPI
4
Proof - step 1
Convert
Context-Free Grammars
to
PDAs
Fall 2006
Costas Busch - RPI
5
Take an arbitrary context-free grammar
We will convert
G
G
to a PDA M such that:
L(G )  L( M )
Fall 2006
Costas Busch - RPI
6
Conversion Procedure:
For each
production in
For each
terminal in
G
Aw
G
a
Add transitions
, A  w
q0
Fall 2006
,   S
a, a  
q1
Costas Busch - RPI
, $  $
q2
7
Example
Grammar
S  aSTb
S b
T  Ta
T 
q0
Fall 2006
PDA
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
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
Fall 2006
Stack
contents
Costas Busch - RPI
9
Grammar
Leftmost Derivation
Terminals
Leftmost
variable
Variables
or terminals

 xAy
 x i  j Bzy
Production applied
A   i  j Bz
Terminals
Fall 2006
Variables
or terminals
Variable
Costas Busch - RPI
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
Fall 2006
Costas Busch - RPI
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
Fall 2006
Costas Busch - RPI
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
Fall 2006
Last Transition applied
 j , j  
q0  ,   S
Costas Busch - RPI
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
Fall 2006
And so on……
Costas Busch - RPI
14
Example:
Input
Time 0
Fall 2006
q0
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
$
Stack
q2
15
Derivation: S
Input
Time 1
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
S
$
Stack
q2
16
Derivation: S  aSTb
Input
Time 2
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
a
S
T
b
$
Stack
q2
17
Derivation: S  aSTb
Input
Time 3
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
a
S
T
b
$
Stack
q2
18
Derivation: S  aSTb  abTb
Input
Time 4
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
b
T
b
$
Stack
q2
19
Derivation: S  aSTb  abTb
Input
Time 5
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
b
T
b
$
Stack
q2
20
Derivation: S  aSTb  abTb  abTab
Input
Time 6
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
T
a
b
$
Stack
q2
21
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 7
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
T
a
b
$
Stack
q2
22
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 8
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
a
b
$
Stack
q2
23
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 9
q0
Fall 2006
a b
a b
 , S  aSTb
, S  b
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
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
Fall 2006
,   S
q1
Costas Busch - RPI
, $  $
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 ,  ,$)
Fall 2006
Costas Busch - RPI
26
In general, it can be shown that:
Grammar
generates
string w
*
G
If and
Only if
Sw
Therefore
Fall 2006
PDA M
accepts
w
(q0 , w,$)  (q2 ,  ,$)
L(G )  L( M )
Costas Busch - RPI
27
Proof - step 2
Convert
PDAs
to
Context-Free Grammars
Fall 2006
Costas Busch - RPI
28
Take an arbitrary PDA M
We will convert M
to a context-free grammar G such that:
L(M )  L(G )
Fall 2006
Costas Busch - RPI
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 #
4. Each transition either pushes a symbol
or pops a symbol but not both together
Fall 2006
Costas Busch - RPI
30
1. The PDA has a single accept state
PDA
PDA
M1
M
Old
accept
states
New
accept
state
qf
Fall 2006
Costas Busch - RPI
31
2. Use new initial stack symbol #
Top of stack
old initial stack symbol
Z
@
auxiliary stack symbol
#
new initial stack symbol
PDA M2
,   @
,   Z
PDA M1
M1 still thinks that Z is the initial stack
Fall 2006
Costas Busch - RPI
32
3. On acceptance the stack contains only
stack symbol #
PDA M3
Empty stack
x    {@, # }
PDA M2
Old
accept
state
, x  
,   
Fall 2006
Costas Busch - RPI
, @  
New
accept
state
qf
33
4. Each transition either pushes a symbol
or pops a symbol but not both together
PDA
PDA
Fall 2006
M4
M3
qi
, a  b

,
a


qi
Costas Busch - RPI
qj
,   b q
j
34
PDA M3
PDA
Fall 2006

,



qi
M4
Where
,    q
qi
j

,    q
j
is a symbol of the stack alphabet
Costas Busch - RPI
35
PDA
M4 is the final modified PDA
Note that the new initial stack symbol #
is never used in any transition
Fall 2006
Costas Busch - RPI
36
Example:
M
a,   a
b, a  
q
M4
a,   a
b, a  
q0
,   @
q1
Fall 2006
, a  
, b  
, Z  
q2
,   a
q3
, a  
q4
, @  
q5
,   Z
Costas Busch - RPI
37
Grammar Construction
Variables:
Aqi ,q j
States of PDA
Fall 2006
Costas Busch - RPI
38
PDA
Kind 1: for each state
q
Grammar
Aqq  
Fall 2006
Costas Busch - RPI
39
PDA
Kind 2: for every three states
p
q
r
Grammar
Apq  Apr Arq
Fall 2006
Costas Busch - RPI
40
PDA
Kind 3: for every pair of such transitions
p
a,   t
b,t   q
s
r
Grammar
Apq  aArs b
Fall 2006
Costas Busch - RPI
41
PDA
Initial state
Accept state
q0
qf
Grammar
Start variable
Fall 2006
Aq0qf
Costas Busch - RPI
42
Example:
PDA
M4
a,   a
b, a  
q0
,   @
q1
Fall 2006
, a  
, b  
, Z  
q2
,   a
q3
, a  
q4
, @  
q5
,   Z
Costas Busch - RPI
43
Grammar
Kind 1: from single states
Aq0q0  
Aq1q1  
Aq2q2  
Aq3q3  
Aq4q4  
Aq5q5  
Fall 2006
Costas Busch - RPI
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
Fall 2006
Aq0q5
Costas Busch - RPI
45
Kind 3: from pairs of transitions
M4
q0
,   @
q1
q2
,   a
q3
, a  
q4
, @  
q5
,   Z
Aq0q5  Aq1q4
Aq1q4  Aq2q4
Fall 2006
, a  
, b  
, Z  
a,   a
b, a  
Aq2q4  aAq2q4
Aq2q2  Aq3q2 b
Aq2q2  aAq2q2 b
Aq2q4  Aq3q3
Aq2q4  aAq2q3
Aq2q4  Aq3q4
Costas Busch - RPI
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 )
Fall 2006
Costas Busch - RPI
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
Fall 2006
w
Costas Busch - RPI
48
We will actually show that if G has derivation:

Apq  w
Then there is a computation in
M
:

( p,w ,  ) (q , ,  )
Fall 2006
Costas Busch - RPI
49
Therefore:

Aq0qf w

(q0 ,w ,  ) (qf , ,  )
Since there is no transition
with the # symbol

(q0 ,w , # ) (qf , , # )
Fall 2006
Costas Busch - RPI
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 , ,  )
Fall 2006
Costas Busch - RPI
51
Proof Intuition:
Apq    w
Type 2
Case 1:
Apq  Apr Arq    w
Type 3
Case 2:
Fall 2006
Apq  aArs b    w
Costas Busch - RPI
52
Type 2
Case 1:
Apq  Apr Arq    w
Stack
height
p
Input string
Fall 2006
Generated by
Apr
Costas Busch - RPI
r
Generated by
q
Arq
53
Type 3
Case 2:
Apq  aArs b    w
Stack
height
r
p
Input string
Fall 2006
s
a
Generated by
Costas Busch - RPI
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
Fall 2006
Costas Busch - RPI
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 , ,  )
Fall 2006
Costas Busch - RPI
56
Induction Hypothesis:
Apq    w
k
derivation steps
suppose it holds:

( p,w ,  ) (q , ,  )
Fall 2006
Costas Busch - RPI
57
Induction Step:
Apq    w
k 1
derivation steps
We have to show:

( p,w ,  ) (q , ,  )
Fall 2006
Costas Busch - RPI
58
Apq    w
k 1
derivation steps
Type 2
Case 1:
Apq  Apr Arq    w
Type 3
Case 2:
Fall 2006
Apq  aArs b    w
Costas Busch - RPI
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
Fall 2006
At most k steps
Costas Busch - RPI
60
Apr    y
Arq    z
At most k steps
At most k steps
From induction
hypothesis, in PDA:

( p , y ,  ) (r , ,  )
Fall 2006
From induction
hypothesis, in PDA:

(r , z ,  ) (q , ,  )
Costas Busch - RPI
61


( p , y ,  ) (r , ,  )
(r , z ,  ) (q , ,  )


( p, yz ,  ) (r , z ,  ) (q , ,  )
since w  yz

( p,w ,  ) (q , ,  )
Fall 2006
Costas Busch - RPI
62
Type 3
Case 2:
Apq  aArs b    w
k 1
We can write
steps
w  ayb
Ars    y
At most k steps
Fall 2006
Costas Busch - RPI
63
Ars    y
At most k steps
From induction hypothesis,
the PDA has computation:

(r , y ,  ) (s , ,  )
Fall 2006
Costas Busch - RPI
64
Type 3
Apq  aArs b    w
Grammar contains production
Apq  aArs b
And PDA Contains transitions
p
Fall 2006
a,   t
b,t   q
s
r
Costas Busch - RPI
65
p
a,   t
b,t   q
s
r
(s , b,t )  (q , ,  )
( p, ayb ,  )  (r , yb ,t )
Fall 2006
Costas Busch - RPI
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 , ,  )
Fall 2006
Costas Busch - RPI
67

( p, ayb ,  )  (r , yb ,t ) (s , b,t )  (q , ,  )
since w  ayb

( p,w ,  ) (q , ,  )
END OF PROOF
Fall 2006
Costas Busch - RPI
68
So far we have shown:
L(G )  L(M )
With a similar proof we can show
L(G )  L(M )
Therefore:
Fall 2006
L(G )  L(M )
Costas Busch - RPI
69