Pushdown Automata & Context

advertisement
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



  1  k k 1  n
 (q2 ,  ,$)
Fall 2006
Scanned
symbols
 (q1 ,  k 1  n , X 1  X m $)
Stack
contents
Costas Busch - RPI
9
Grammar
Leftmost Derivation
Terminals
Leftmost
variable
Variables
or terminals

 xAy
 x i  j Bzy
Production applied
Variables
or terminals
A  i  j Bz
Terminals
Fall 2006
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
PDA Computation
 (q1 ,  i  n ,  i  j Bzy $)
 (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  kCp
Fall 2006
And so on……
Costas Busch - RPI
14
Example:
Input
Time 0
Fall 2006
q0
a b a b
 , S  aSTb
$
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
15
Derivation: S
Input
Time 1
q0
Fall 2006
a b a b
S
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
16
Derivation: S  aSTb
Input
Time 2
q0
Fall 2006
a
S
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
17
Derivation: S  aSTb
Input
Time 3
q0
Fall 2006
a
S
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
18
Derivation: S  aSTb  abTb
Input
Time 4
q0
Fall 2006
b
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
19
Derivation: S  aSTb  abTb
Input
Time 5
q0
Fall 2006
b
T
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
20
Derivation: S  aSTb  abTb  abTab
Input
Time 6
q0
Fall 2006
T
a
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
21
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 7
q0
Fall 2006
T
a
b
$
a b a b
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
22
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 8
q0
Fall 2006
a b a b
a
b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
23
Derivation: S  aSTb  abTb  abTab  abab
Input
Time 9
q0
Fall 2006
a b a b
b
$
 , S  aSTb
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
,   S
q1
Costas Busch - RPI
, $  $
q2
24
Derivation: S  aSTb  abTb  abTab  abab
Input
a b a b
Time 10
 , S  aSTb
$
, S  b
Stack
a, a  
 , T  Ta
b, b  
, T  
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

,   
qj
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
,   Z
q3
, a  
Costas Busch - RPI
q4
, @  
q5
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
r
s
b,t   q
Grammar
Apq  aArs b
Fall 2006
Costas Busch - RPI
41
PDA
Initial state
Accept state
qf
q0
Grammar
Start variable
Fall 2006
Aq qf
0
Costas Busch - RPI
42
Example:
PDA
M4
a,   a
b, a  
q0
,   @
q1
Fall 2006
, a  
, b  
, Z  
q2
,   a
,   Z
q3
, a  
Costas Busch - RPI
q4
, @  
q5
43
Grammar
Kind 1: from single states
Aq q  
0 0
Aq q  
1 1
Aq q  
2 2
Aq q  
3 3
Aq q  
4 4
Aq q  
5 5
Fall 2006
Costas Busch - RPI
44
Kind 2: from triplets of states
Aq q  Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
0 0
0 0
0 0
0 1
1 0
0 2
2 0
0 3
3 0
0 4
4 0
0 5
5 0
Aq q  Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
0 1
0 0
0 1
0 1
1 1
0 2
2 1
0 3
3 1
0 4
4 1
0 5
5 1

Aq q  Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
0 5
0 0
0 5
0 1
1 5
0 2
2 5
0 3
3 5
0 4
4 5
0 5
5 5

Aq q  Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
5 5
5 0
0 5
5 1
1 5
5 2
Start variable
Fall 2006
2 5
5 3
3 5
5 4
4 5
5 5
5 5
Aq q
0 5
Costas Busch - RPI
45
Kind 3: from pairs of transitions
M4
q0
,   @
q1
1 4
Aq q  Aq q
1 4
q2
,   a
,   Z
Aq q  Aq q
0 5
, a  
, b  
, Z  
a,   a
b, a  
2 4
q3
, a  
q5
Aq q  aAq q
Aq q  aAq q b
Aq q  Aq q b
Aq q  aAq q
Aq q  Aq q
2 4
2 2
2 4
2 2
2 4
Fall 2006
q4
, @  
Costas Busch - RPI
2 3
2 2
3 2
Aq q  Aq q
2 4
2 4
3 3
3 4
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

Aq qf w
0
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:

Aq qf w
0

(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


(r , z ,  ) (q , ,  )
( p , y ,  ) (r , ,  )


( 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
r
s
Costas Busch - RPI
b,t   q
65
p
a,  t
r
s
(s , b,t )  (q , , )
( p, ayb, )  (r , yb,t )
Fall 2006
b,t   q
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
Download