CFG To PDA Conversion
CFG To PDA Conversion
Conversion
Prepared by : S A Jaipurkar
Asst Prof,CSED,
MIT, Aurangabad.
STEPS TO CONVERT CFG TO PDA
● The first symbol on R.H.S. production must be a terminal symbol.
● The following steps are used to obtain PDA from CFG is:
Step 1: Convert the given productions of CFG into GNF.
Step 2: The PDA will only have one state {q}.
Step 3: The initial symbol of CFG will be the initial symbol in the PDA.
Step 4: For non-terminal symbol, add the following rule:
δ(q, ε, A) = (q, α) Where the production rule is A → α
Step 5: For each terminal symbols, add the following rule:
δ(q, a, a) = (q, ε) for every terminal symbol
Example 1:
● Convert the following grammar to a PDA that
accepts the same language.
{S → 0S1 | A
A → 1A0 | S | ε}
Solution
● The CFG can be first simplified by eliminating
unit productions:
S → 0S1 | 1S0 | ε
● Now we will convert this CFG to GNF:
S → 0SX | 1SY | ε
X→1
Y→0
The PDA can be:
● R1: δ(q, ε, S) = {(q, 0SX) | (q, 1SY) | (q, ε)}
● R2: δ(q, ε, X) = {(q, 1)}
● R3: δ(q, ε, Y) = {(q, 0)}
● R4: δ(q, 0, 0) = {(q, ε)}
● R5: δ(q, 1, 1) = {(q, ε)}
Example 2:
Construct PDA for the given CFG, and test
whether 010^4 is acceptable by this PDA.
{ S → 0BB ,
B → 0S | 1S | 0 }
● A = {(q), (0, 1), (S, B, 0, 1), δ, q, S, ?}
● The production rule δ can be:
● R1: δ(q, ε, S) = {(q, 0BB)}
R2: δ(q, ε, B) = {(q, 0S) | (q, 1S) | (q, 0)}
R3: δ(q, 0, 0) = {(q, ε)}
R4: δ(q, 1, 1) = {(q, ε)}
Testing for string {010000}:
● δ(q, 010000, S) ⊢ δ(q, 010000, 0BB)
⊢ δ(q, 10000, BB) (using R1 ) ⊢ δ(q, 10000,1SB) (using R3 )
⊢ δ(q, 0000, SB) (using R2) ⊢ δ(q, 0000, 0BBB) ((using R1 )
⊢ δ(q, 000, BBB) (using R3) ⊢ δ(q, 000, 0BB) (using R2 )
⊢ δ(q, 00, BB) (using R3) ⊢ δ(q, 00, 0B) (using R2 )
⊢ δ(q, 0, B) (using R3 ) ⊢ δ(q, 0, 0) (using R2)
⊢ δ(q, ε) (using R3 )
ACCEPT
Example 3
● Draw a PDA for the CFG given below:
{ S → aSb
S→a|b|ε }
Remove null Production, therefore new set of
productions will be
{S → aSb |ab
S→a|b }
● The PDA can be given as :
P = {(q), (a, b), (S, a, b, z0), δ, q, z0, q}
● Transition functions will be :
R1: δ(q, ε, S) = {(q, aSb)}
R2: δ(q, ε, S) = {(q, a) | (q, b) | (q, ε)}
R3: δ(q, a, a) = {(q, ε)}
R4: δ(q, b, b) = {(q, ε)}
R5: δ(q, ε, z0) = {(q, ε)}
Simulation: for string aaabb
● δ(q, εaaabb, S) ⊢ δ(q, aaabb, aSb)
⊢ δ(q, εaabb, Sb)
⊢ δ(q, aabb, aSbb)
⊢ δ(q, εabb, Sbb)
⊢ δ(q, abb, abb)
⊢ δ(q, bb, bb)
⊢ δ(q, b, b)
⊢ δ(q, ε, z0)
⊢ δ(q, ε) HENCE ACCEPT