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

L32- ToC

Equivalence of PDA and 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, )}

 Equivalent CFG, Equivalent productions are:


S  [qZq]
[qZq]  i[qXq][qZq]
[qXq]  e
[qZq]  
If [qZq] is renamed to A and [qXq] is renamed to B then the CFG can
be defined by:
G = ({S, A, B}, {i, e}, {SA, AiBA | , Be }, S)
Example - 3
 PDA P = ({p, q}, {0,1}, {X,Z}, δ, q, Z)), Transition
function δ is defined by:

1. δ(p, 0, Z)= {(p, XZ)}


2. δ(p, 0, X)= {(p, XX)}
3. δ(p, 1, X)= {(q, )}
4. δ(q, 1, X)= {(q, )}
5. δ(p, , X)= {(q, )}
6. δ(q, , Z)= {(q, )}
Example -3 Contd...
1. δ(p, 0, Z)= {(p, XZ)}
 Solution 2. δ(p, 0, X)= {(p, XX)}
S  [pZp] | [pZq] 3. δ(p, 1, X)= {(q, )}
4. δ(q, 1, X)= {(q, )}
 For δ(p, 0, Z)= {(p, XZ)} 5. δ(q, , X)= {(q, )}
[pZp]  0[pXp][pZq] 6. δ(q, , Z)= {(q, )}
[pZp]  0[pXq][qZp]
[pZq]  0[pXp][pZq]
[pZq]  0[pXq][qZp] 1. S  [q0Z0 p], for all states p
2. Let δ(q, a, X) = {(r, Y1Y2…Yk)}k can be 0
 For δ(p, 0, X)= {(p, XX)} or any number and r1r2 …rk are list of states
[pXp]  0[pXp][pXp] [qXrk]  a[rY1r1] [r1Y2r2] … [rk-1Ykrk]
If k = 0 then [qXr] a
[pXp]  0[pXq][qXp]
[pXq]  0[pXp][pXq]
[pXq]  0[pXq][qXq]
Example -3 Contd…
 For δ(p, 1, X)= {(q, )}
[pXq]  1
 For δ(q, 1, X)= {(q, )} – Rename the variables [pZp]
[qXq]  1 to A, [pZq] to B, [qZp] to C,
 For δ(q, , X)= {(q, )} [qZq] to D, [pXp] to E [pXq]
[qXq]  
to F, [qXp] to G and [qXq] to
H
 For δ(q, , Z)= {(q, )}
– Equivalent CFG can be
[qZq]   defined by
G = ({S, A, B, C, D, E, F, G,
H}, {0,1},R, S)
Example-3
SA|B
A  0EB | 0FC Rename the variables [pZp] to A,
[pZq] to B, [qZp] to C, [qZq] to D,
B  0EB | 0FD
[pXp] to E [pXq] to F, [qXp] to G
E  0EE | 0FG and [qXq] to H
F  0EF | 0FH
F1
H  1 |H  
D
 G, C does not generate terminals, E, A in looping not leading
to generating; and B, D, F, H, S generates
 Final Rules: S  B B  0FD F  0FH F  1
H  1 |H   D  
15
1. δ(p, 0, Z)= {(p, XZ)}

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, )}

 (p, 0011, Z) |- (p, 011, XZ) |- (p, 11, XXZ) |- (q,


1, XZ) |- (q,  ,Z) |- (q, , ) SB B  0FD
F  0FH F  1
H  1 |H   D  

 S  B  0FD  00FHD  001HD 0011D


0011

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.

 dPDA surely includes δ(q, $, Z) = {(p, )}

 Convert N(P) to CFG


 Generate G  from P .
 Construct from G  a grammar G such that L(G)= L

 [qZp] → $
 Treat $ as a variable and introduce a production $  
19

You might also like