pdatocfl.doc

advertisement
Rules for conversion from PDA to CFG
1. S -> [qo, Zo, q] for each q  Q.
2. If q, q1  Q, a    {}, A  , and (q,a,A)= (q1 ,) include
productions [q,A,q1 ] -> a
3. if m  1, q,q1  Q, a    {}, A  , and
(q,a,A)= (q1 ,B1,B2,...,Bm) then for every choice of
q2,...,qm+1  Q, the production
[q,A,qm+1 ] -> a[q1,B1,q2 ] [q2,B2,q3 ][q3,B3,q4 ],...,[qm,Bm,qm+1 ]
are productions in the CFG
Example: Given a PDA, find the CFL
Let the PDA be M = (Q, , , , s, F) where
Q = {q,r}
 = {0,1}
 = {Z,X}
 = {(q,0,Z) |- (q,XZ), (q,0,X) |- (q,XX),
(q,1,X) |- (r,), (r,1,X) |- (r,),
(r,,X) |- (r,), (r,,Z) |- (r,)}
S = q
F =  (accept by empty stack)
Let Zo be an element on the stack initially, so (q, input string,
Zo) is the initial configuration. Zo is the bottom of stack
marker.
Construct
grammar.
the
CFL.
Let
S
be
the
start
nonterminal
for
the
The productions are:
S -> [q,Zo,q] | [q,Zo,r]
S -> A | B
q is the initial state, Z the
bottom of the stack and q and r
are all possible states of the
PDA.
Consider
[q,Zo,q]
[q,Zo,q]
[q,Zo,r]
[q,Zo,r]
(q,0,Zo) |- (q,XZo) from the PDA produces the rules
-> 0[q,X,q][q,Zo,q]
A -> 0CA
-> 0[q,X,r][r,Zo,q]
A -> 0DE
-> 0[q,X,q][q,Zo,r]
B -> 0CB
-> 0[q,X,r][r,Zo,r]
B -> 0DH
Consider (q,0,X) |- (q,XX)
[q,X,q] -> 0[q,X,q][q,X,q]
[q,X,q] -> 0[q,X,r][r,X,q]
[q,X,r] -> 0[q,X,q][q,X,r]
[q,X,r] -> 0[q,X,r][r,X,r]
C
C
D
D
Consider (q,1,X) |- (r,)
[q,X,r] -> 1
D -> 1
Consider (r,1,X) |- (r,)
[r,X,r] -> 1
G -> 1
Consider (r,,X) |- (r,)
[r,X,r] -> 
G -> 
Consder (r,,Zo) |- (r,)
[r,Zo,r] -> 
Now
let
A =
B =
C =
D =
E =
F =
G =
H =
->
->
->
->
0CC
0DF
0CD
0DG
H -> 
for confirmation:
[q,Zo,q]
[q,Zo,r]
[q,X,q]
[q,X,r]
[r,Zo,q]
[r,X,q]
[r,X,r]
[r,Zo,r]
Now notice that F and E have no productions so no terminal
string can be derived from them. All productions for C and A
have E or F on the right, no terminal string can be derived from
C or A either. So the final set of productions are:
S
B
D
D
G
G
H
->
->
->
->
->
->
->
B
0DH
0DG
1
1


Now S -> B -> 0DH -> 00DGH -> 000DGGH -> 000111 is a terminal
string generated by the grammar and accepted by the original PDA
as shown:
input:
000111
stack:
state:
A, XZo, XXZo, XXXZo, XXZo, XZo, Zo, 
q,0,Zo |- q,XZo
q,0,X |- q,XX
q,0,X |- q,XX
q,1,X |- r,
r,1,X |- r,
r,1,X |- r,
r,,Zo |- r, (accept)
Perform the same exercise for the following PDA
(q,1,Zo) |- (q,XZo)
(q,1,X) |- (q,XX)
(q,0,X) |- (r,X)
(q,,Zo) |- (q,)
(r,1,X) |- (r,)
(r,0,Zo) |- (q,Zo)
Download