Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CFG to PDA Conversion:

 Transform the grammar into Greibach normal form, if not.

Procedure:

For any context-free language L(G), G={N,T,P,S}, there exists an NPDA, M such that L = L(M)

NPDA will be accepted by final state


M = ({q0, q1, qf}, T, V∪ {z}, δ, q0, z, {qf}) as per the following rules

 Include δ(q0, λ, z) = {(q1, Sz)}


 Include the set of transition rules
(q1, u) ∈ δ(q1, a, A) whenever A → au in P
 Include δ(q1, λ, z) = {(qf, z )}

Ex: Construct a PDA for the grammar S → aSbb | a

 Transform the grammar into Greibach normal form


S → aSA | a
A → bB
B→b
 The start symbol is put on the stack by
δ(q0, λ, z) = (q1, Sz) // q0 : initial state
 Transitions for each grammar rule
δ(q1, a, S) = {(q1, SA), (q1, λ)}
δ(q1, b, A) = {(q1, B)}
δ(q1, b, B) = {(q1, λ)}
 δ(q1, λ, z) = {(q2, z)} // q2 : final state

Ex:
S → aA
A → aABC | bB | a
B→b
C→c

Transition rules:

δ(q0, λ, z) = {(q1, Sz)}


δ(q1, a, S) = {(q1, A)}
δ(q1, a, A) = {(q1, ABC), (q1, λ)}
δ(q1, b, A) = {(q1, B)}
δ(q1, b, B) = {(q1, λ)}
δ(q1, c, C) = {(q1, λ)}
δ(q1, λ, z) = {(qf, z)}
The sequence of moves made by M in processing aaabc
(q0, aaabc, z) |– (q1, aaabc, Sz) |– (q1, aabc, Az) |– (q1, abc, ABCz)
|– (q1, bc, BCz) |– (q1, c, Cz) |– (q1, λ, z) |– (qf, z)

This corresponds to the derivation


S ⇒aA ⇒aaABC ⇒aaaBC ⇒aaabC ⇒aaabc

Que: Construct a PDA that accepts the language generated by the following CFG
SaB
BbA/b
AaB
And show the ID for string ”abab”
PDA to CFG Conversion:
Procedure:

Let G={N,T,P,S} is converted CFG from a PDA


M = (Q, Σ, Γ, δ, q0, z0,Ф) Ф means PDA accepts by empty stack

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.

Example: Convert the following PDA to equivalent CFG.

M= {{q0,q1}, {0,1},{z0,x}, δ, q0, z0,Ф}


Where δ is given as follows:
δ(q0,1,z0)=(q0,xz0)
δ(q0,Ɛ,z0)=(q0,Ɛ)
δ(q0,1,x)=(q0,xx)
δ(q0,0,x)=(q1,x)
δ(q1,1,x)=(q1,Ɛ)
δ(q1,Ɛ,z0)=(q0,z0)
Soln:
Let converted CFG is G={N,T,P,S}
Where N={S} {[q0,z0,q0], [q0,z0,q1], [q0,x,q0], [q0,x,q1], [q1,z0,q0], [q1,z0,q1],
[q1,x,q0], [q1,x,q1]}
T={0,1}
And P is given as follows:
Productions(P):
In this PDA, |Q|=2, so no. of productions from one transition function
will be 2α where α=no of symbols pushed onto stack in that transition (except Start symbol,
S)
For start symbol, apply R1:
No of productions is equal to no. of states in PDA=2
So, S[q0,z0,q0] / [q0,z0,q1]

Now apply R2:


For δ(q0,1,z0)=(q0,xz0), the productions (22=4)are as follows-

[q0,z0,q0]1[q0,x,q0] [q0,z0, q0]

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]

For, δ(q0,Ɛ,z0)=(q0,Ɛ), the productions (20=1) is as follows-

[q0,z0,q0] Ɛ

For, δ(q0,1,x)=(q0,xx), the productions (22=4) are as follows-

[q0,x,q0]1[q0,x,q0] [q0,x, q0]


[q0,x,q0]1[q0,x,q1] [q1,x, q0]
[q0,x,q1]1[q0,x,q0] [q0,x, q1]
[q0,x,q1]1[q0,x,q1] [q1,x, q1]

For, δ(q0,0,x)=(q1,x), the productions (21=2) are as follows-

[q0,x,q0]0[q1,x,q0]
same

[q0,x,q1]0[q1,x,q1]

For, δ(q1,1,x)=(q1,Ɛ) , the productions (20=1) is as follows-

[q1,x,q1] 1

For, δ(q1,Ɛ,z0)=(q0,z0) , the productions (21=2) are as follows-

[q1,z0,q0] Ɛ [q0,z0,q0]
[q1,z0,q1] Ɛ [q0,z0,q1]

Que: Convert the following PDA to equivalent CFG.


M= {{q0,q1, q2}, {a,b,c },{ z0,z1,z2}, δ, q0, z0,Ф}
Where δ is given as follows:
δ(q0,a,z0)=(q0,z1z0)
δ(q0,a,z1)=(q0,z1z1)
δ(q0,b ,z1)=(q1,Ɛ)
δ(q1,b ,z1)=(q1,Ɛ)
δ(q1,b ,z0)=(q1,z2z0)
δ(q1,b ,z2)=(q1,z2z2)
δ(q1,c,z2)=(q2,Ɛ)
δ(q2,c,z2)=(q2,Ɛ)
δ(q2, Ɛ,z0)=(q2,Ɛ)
Two Stack PDA:
In case of CSL (context sensitive language) like {anbncn|n≥1}, the PDA
will not work. If more than one stack is added in form of auxiliary storage with PDA, then the
accepting power of PDA will increase. So, we need two stack PDA. Not only two but more
than two stacks can be added to PDA.
Two stack PDA is equivalent to Turing Machine.

A two stack PDA is defined by the nine tuple:


M = (Q, Σ, Γ, Γ’, δ, q0, z1, z2, F)
where
Q is a non empty finite set of internal states of the control unit
Σ is the non empty finite set of input alphabet
Γ is the non empty finite set of first stack symbols
Γ’ is the non empty finite set of second stack symbols
δ : Q×(Σ∪{λ})×Γx Γ’ → finite subsets of Q×Γ*x Γ’* is the transition function
q0 ∈ Q is the initial state of the control unit
z1 ∈ Γ is the first stack start symbol
z2 ∈ Γ’ is the second stack start symbol
F ⊆ Q is the set of final states.(it may be empty)

Example:

Construct a two stack PDA for L= {anbncn|n≥0}.

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.

The transition function is as follows:

δ(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, Ɛ, Ɛ)

You might also like