Professional Documents
Culture Documents
L32 Pda To CFG
L32 Pda To CFG
Course Instructors:
Div 1: Praveen Pawar (Div 1)
Div 2: Jibi Abraham (Div 2)
1
Equivalence of PDA and CFG
PDA by PDA by
CFG empty stack
Final state
CFG to PDA
The automaton simulates leftmost derivations of CFG to
generate terminal string w (w = xy), accept by empty stack
of PDA
After consuming x of w let PDA be in ID (q, y, A) and
CFG has a production A
PDA has to move to ID (q, y, )
δ(q, , A) = {(q, )}
Terminal on the stack top matched with input symbol
If match, cancel by δ(q, a, a) = {(q, )}
Else PDA stops
CFG to PDA (Contd…)
Let G = (V, T, P, S) be a CFG
Equivalent PDA M which accepts L(G) by empty stack
is given by:
M = ({q}, T, V T, δ, q, S), where δ is defined by:
1. For each variable A with production A in Q
δ(q, , A) = {(q, )}
2. For each terminal a, δ(q, a, a) = {(q, )}
Another way of constructing PDA
Construct CFG, convert CFG to PDA
CFG to PDA (Contd…)
The stack symbols of the new PDA contain all
the terminal and non-terminals of the CFG
There is only one state in the new PDA, all the
rest of the information is encoded in the stack
1. Corresponding to each production (A ), there
is a transition on (δ(q, , A) = {(q, )} )
For each terminal, other transitions come
(δ(q, a, a) = {(q, )})
Example -1
Convert to PDA accepted by empty stack
S 0S1 | A
A 1A0 | S |
Solution
P = ({q}, {0, 1}, {0, 1, A, S}, δ, q, S)
δ(q, , S) = {(q, 0S1), (q, A)}
δ(q, , A) = {(q, 1A0), (q, S), (q, )}
δ(q, 0, 0) = {(q, )}
δ(q, 1, 1) = {(q, )}
Example-1 Contd…
S 0S1 | A
A 1A0 | S |
To derive string 010101 by left most derivations by CFG
S 0S1 0A1 01A01 01S01 010S101 010A101
010101(A)
δ(q, , S) = {(q, 0S1), (q, A)}
δ(q, , A) = {(q, 1A0), (q, S), (q, )}
δ(q, 0, 0) = {(q, )}
δ(q, 1, 1) = {(q, )}
To accept string 010101 by empty stack of PDA
(q, 010101, S) |- (q, 010101,0S1) |- (q, 10101, S1) |-(q, 10101,
A1) |- (q, 10101, 1A01) |- (q, 0101, A01) |- (q, 0101, S01) |- (q,
0101, 0S101) |- (q, 101, S101) |-(q, 101, A101) |- (q, 101, 101) |-
(q, 01, 01) |- (q, 1,1) |- (q, , ) – accept the string
Polling - 1
G = ({S, X}, {a, b}, P, S) where the
productions are S → XS, X → aXb | ab
How many members are there in the transition
function of the equivalent PDA by empty
stack?
A. 2
B. 3
C. 4
D. 5
E. 6
8
PDA to CFG
Let M = (Q, Σ, Γ, δ, q0, Z0) be a PDA accepted by empty
stack. Equivalent CFG G = (V, Σ, P, S), where
V = {S, [pXq]}, where p, q Q and X Γ
State [pXq] intended to pop X from stack while going
from state p to q
Productions P consists of
1. S [q0Z0 p], for all states p
2. Let δ(q, a, X) = {(r, Y1Y2…Yk)} where a Σ or
a = , k can be 0 or any number and r1r2 …rk are list of states
[qXrk] a[rY1r1] [r1Y2r2] … [rk-1Ykrk]
If k = 0 then [qXr] a
Example -2
PDA to accept if-else of a C program(does
not accept if-if-else-else),
P = ({q}, {i, e}, {X,Z}, δ, q, Z)
δ(q, i, Z) = {(q, XZ)}
δ(q, e, X) = {(q, )
δ(q, , Z) = {(q, )}
Example -2 Contd…
P = ({q}, {i, e}, {X,Z}, δ, q, Z)
δ(q, i, Z) = {(q, XZ)}
δ(q, e, X) = {(q, )
δ(q, , Z) = {(q, )}
Example-3 2.
3.
4.
δ(p, 0, X)= {(p, XX)}
δ(p, 1, X)= {(q, )}
δ(q, 1, X)= {(q, )}
δ(p, , X)= {(q, )}
Derive 0011 5.
6. δ(q, , Z)= {(q, )}
16
Polling - 2
L = {anbn|n>0}, what is production of the
equivalent CFG corresponding to (ε, Z0/ε) from
q1 to q2 in the PDA?
A. [q1 Z0 q1] → ε
B. [q1 Z0 q2] → ε
C. [q2 Z0 q2] → ε
D. [q1 Z0 q2] → [q1 Z0 q2]
E. [q1 Z0 q2] → [q1 Z0 q1] [q1 Z0 q2] 17
dDPDA and Ambiguous CFG
Convert L(P) to N(P)
to have prefix property, add an end marker to strings
of L L = L$
Construct dPDA P for L.
[qZp] → $
Treat $ as a variable and introduce a production $
19