Professional Documents
Culture Documents
CFG To PDA and Two Stack PDA
CFG To PDA and Two Stack PDA
Procedure:
For any context-free language L(G), G={N,T,P,S}, there exists an NPDA, M such that L = L(M)
Ex:
S → aA
A → aABC | bB | a
B→b
C→c
Transition rules:
Que: Construct a PDA that accepts the language generated by the following CFG
SaB
BbA/b
AaB
And show the ID for string ”abab”
PDA to CFG Conversion:
Procedure:
Where T= Σ,
N= {S} U {[q,z,q’] }| q,q’ϵ Q and z ϵ Γ
and S is start symbol of CFG in which every production is represented as a triplet
[state ,stack symbol ,state]
R1: From start symbol, the no. of production is equal to no. of states in PDA machine
S[q0,z0,qi] for all qi ϵ Q
R2: For one transition, there will be |Q||α| productions in grammar where |Q|is no. of
states in PDA and |α|is the no. of stack symbol push into stack for this specific
transition.
same
same
[q0,z0,q0]1[q0,x,q1] [q1,z0, q0]
[q0,z0,q1]1[q0,x,q0] [q0,z0, q1]
[q0,z0,q1]1[q0,x,q1] [q1,z0, q1]
[q0,z0,q0] Ɛ
[q0,x,q0]0[q1,x,q0]
same
[q0,x,q1]0[q1,x,q1]
[q1,x,q1] 1
[q1,z0,q0] Ɛ [q0,z0,q0]
[q1,z0,q1] Ɛ [q0,z0,q1]
Example:
Soln:
While reading a, push x onto first stack and while reading b, push y onto second stack.
While reading c, pop x and y from stack one and stack two respectively.
δ(q0,a,z1,z2)=(q0,xz1,z2)
δ(q0,a,x,z2)=(q0,xx,z2)
δ(q0,b,x,z2)=(q0,x,yz2)
δ(q0,b,x,y)=(q0,x,yy)
δ(q0,c,x,y)=(q1,Ɛ,Ɛ)
δ(q1,c,x,y)=(q1,Ɛ,Ɛ)
δ(q1,Ɛ,z1,z2)=(qf,z1,z2)
δ(q0,Ɛ,z1,z2)=(qf,z1,z2)
or
δ(q1,Ɛ,z1,z2)=(q1, Ɛ, Ɛ)
δ(q0, Ɛ,z1,z2)=( q0, Ɛ, Ɛ)