You are on page 1of 54

Theory of Computation

BY
MR. VIPIN WANI
UNIT 4

PUSHDOWN AUTOMATA (PDA)

Prepared by: Mr. Vipin Wani


Introduction to Pushdown Automata
2

➢ Pushdown Automata is a finite automata with extra memory called stack

which helps Pushdown automata to recognize Context Free Languages.

Prepared by: Mr. Vipin Wani


Introduction to Pushdown Automata
3

➢ Pushdown automata is a way to implement a CFG in the same way we design DFA for a
regular grammar. A DFA can remember a finite amount of information, but a PDA can
remember an infinite amount of information.
➢ Pushdown automata is simply an NFA augmented with an "external stack memory". The
addition of stack is used to provide a last-in-first-out memory management capability
to Pushdown automata. Pushdown automata can store an unbounded amount of
information on the stack. It can access a limited amount of information on the stack. A
PDA can push an element onto the top of the stack and pop off an element from the top
of the stack. To read an element into the stack, the top elements must be popped off and
are lost.
➢ A PDA is more powerful than FA. Any language which can be acceptable by FA can also
be acceptable by PDA. PDA also accepts a class of language which even cannot be
accepted by FA. Thus PDA is much more superior to FA.

Prepared by: Mr. Vipin Wani


Introduction to Pushdown Automata
4

Prepared by: Mr. Vipin Wani


Introduction to Pushdown Automata
5

PDA Components:

1. Input tape: The input tape is divided in many cells or symbols. The input head is
read-only and may only move from left to right, one symbol at a time.

2. Finite control: The finite control has some pointer which points the current
symbol which is to be read.

3. Stack: The stack is a structure in which we can push and remove the items from
one end only. It has an infinite size. In PDA, the stack is used to store the items
temporarily.

Prepared by: Mr. Vipin Wani


Introduction to Pushdown Automata
6

Formal definition of PDA: The PDA can be defined as a collection of 7 components:

Q: the finite set of states

∑: the input set

Γ: a stack symbol which can be pushed and popped from the stack

q0: the initial state

Z: a start symbol which is in Γ.

F: a set of final states

δ: mapping function which is used for moving from current state to next state.

Prepared by: Mr. Vipin Wani


Introduction to Pushdown Automata
7

The following diagram shows a transition in a PDA from a state q1 to state q2, labeled as
a,b → c −

Prepared by: Mr. Vipin Wani


Instantaneous Description of (PDA)
8
Instantaneous Description is an informal notation of how a PDA computes an input
string and make a decision that string is accepted or rejected.
An instantaneous description is a triple (q, w, α) where:
➢ q describes the current state after move.
➢ w describes the remaining input.
➢ α describes the stack contents, top at the left.
➢ ⊢ sign describes the turnstile notation and represents one move.
➢ For example,
(p, b, T) ⊢ (q, w, α)

In the above example, while taking a transition from state p to q, the input symbol 'b' is
consumed, and the top of the stack 'T' is represented by a new string α.
Prepared by: Mr. Vipin Wani
Examples on PDA
9

➢ Example 1: Design a PDA for accepting a language {anbn | n>=1}.

➢ Solution: In this language, n number of a's should be followed by n number of b's.


Hence, we will apply a very simple logic, and that is if we read single 'a', we will
push a's onto the stack. As soon as we read 'b' then for every single 'b' only one 'a'
should get popped from the stack. The Instantaneous Description for this example
can be given as
➢ δ (q0, a, Z0) = (q0, aZ0)

➢ δ (q0, a, a) = (q0, aa)

➢ δ (q0, b, a) = (q1, ε)

➢ δ (q1, b, a) = (q1, ε)

➢ δ (q1, ε, Z0) = (q2, ε)

Prepared by: Mr. Vipin Wani


Examples on PDA
10

➢ Example 1:

➢ Here q0 is Initial state and q2 is final state

➢ Let us Simulate the PDA for the String aaabbb

(q0, aaabbb, Z0) ⊢ (q0, aabbb, aZ0)


⊢ (q0, abbb, aaZ0)
⊢ (q0, bbb, aaaZ0)
⊢ (q1, bb, aaZ0)
⊢ (q1, b, aZ0)
⊢ (q1, ε, Z0)
⊢ (q2, ε)
Accept State

Prepared by: Mr. Vipin Wani


Examples on PDA
11

➢ Example 2: Design a PDA for accepting a language {anb2n | n>=1}.

➢ Solution: In this language, n number of a's should be followed by 2n number of b's.

Hence, we will apply a very simple logic, and that is if we read single 'a', we will push
two a's onto the stack. As soon as we read 'b' then for every single 'b’ two 'a' should get
popped from the stack. The Instantaneous Description for this example can be given as

➢ δ (q0, a, Z0) = (q0, aaZ0)

➢ δ (q0, a, a) = (q0, aaa)

➢ δ (q0, b, a) = (q1, ε)

➢ δ (q1, b, a) = (q1, ε)

➢ δ (q1, ε, Z0) = (q2, ε)

Prepared by: Mr. Vipin Wani


Examples on PDA
12

➢ Example 2: Here q0 is Initial state and q2 is final state

➢ Let us Simulate the PDA for the String aaabbbbbb

(q0, aaabbbbbb, Z0) ⊢ (q0, aabbbbbb, aaZ0)


⊢ (q0, abbbbbb, aaaaZ0)
⊢ (q0, bbbbbb, aaaaaaZ0)
⊢ (q0, bbbbb, aaaaaZ0)
⊢ (q1, bbbb, aaaaZ0)
⊢ (q1, bbb, aaaZ0)
⊢ (q1, bb, aaZ0)
⊢ (q1, b, aZ0)
⊢ (q1, ε, Z0)
⊢ (q2, ε)
Accept State

Prepared by: Mr. Vipin Wani


Examples on PDA
13

➢ Example 3: Design a PDA for accepting a language {XcXr | X ε {a,b}*}.

➢ Solution: In this language, X is string of and b and Xr is reverse of string X, So for


every input symbol of X push the input on stack, and as soon as symbol c occurs
start reading Xr .Then for every symbol of Xr pop single symbol from stack. The
Instantaneous Description for this example can be given as
1. δ (q0, a, Z0) = (q0, aZ0) 7. δ (q0, c, Z0) = (q1, Z0)
2. δ (q0, b, Z0) = (q0, bZ0) 8. δ (q0, c, a) = (q1, a)

3. δ (q0, a, a) = (q0, aa) 9. δ (q0, c, b) = (q1, b)

4. δ (q0, b, a) = (q0, ba) 10. δ (q1, a, a) = (q1, ε)

5. δ (q0, a, b) = (q0, ab)


11. δ (q1, b, b) = (q1, ε)
12. δ (q1, ε, Z0) = (q2, Z0)
6. δ (q0, b, b) = (q0, bb)

Prepared by: Mr. Vipin Wani


Examples on PDA
14

➢ Example 3: Here q0 is Initial state and q2 is final state

➢ Let us Simulate the PDA for the String bbacabb

(q0, bbacabb, Z0) ⊢ (q0, bacabb, bZ0)


⊢ (q0, acabb, bbZ0)
⊢ (q0, cabb, abbZ0)
⊢ (q0, cabb, abbZ0)
⊢ (q1, bb, bbZ0)
⊢ (q1, b, bZ0)
⊢ (q1, ε, Z0)
⊢ (q2, ε)
Accept State

Prepared by: Mr. Vipin Wani


Examples on PDA
15

➢ Example 4: Design a PDA for accepting a language {XcX | X ε {a,b}*}.

Prepared by: Mr. Vipin Wani


Examples on PDA
16

➢ Example 5: Design a PDA for accepting a language {X | X ε {a,b}* and na(X)=nb(X)}.

➢ Solution: This is a language of equal numbers of a’s and equal numbers of b’s.

Initially when stack is empty we may read either a or b, we will push any of the
symbol on the stack. If read a or b when stack is having the same element at top
then we will push the element on stack. Now if we read a and the top of the stack is
b then we will pop b from the stack similarly if we read b and the top of the stack
is a then we will pop a from the stack.

➢ The Instantaneous Description for this example can be given as

Prepared by: Mr. Vipin Wani


Examples on PDA
17

➢ Example 5:

1. δ (q0, a, Z0) = (q0, aZ0)

2. δ (q0, b, Z0) = (q0, bZ0)

3. δ (q0, a, a) = (q0, aa)

4. δ (q0, b, b) = (q0, bb)

5. δ (q0, a, b) = (q0, ε)

6. δ (q0, b, a) = (q0, ε)

7. δ (q0, ε, Z0) = (q1, Z0)

Prepared by: Mr. Vipin Wani


Examples on PDA
18

➢ Example 5: Here q0 is Initial state and q1 is final state

➢ Let us Simulate the PDA for the String aababb

(q0, aababb, Z0) ⊢ (q0, ababb, aZ0)


⊢ (q0, babb, aaZ0)
⊢ (q0, abb, aZ0)
⊢ (q0, bb, aaZ0)
⊢ (q0, b, aZ0)
⊢ (q0, ε, Z0)
⊢ (q1, ε)
Accept State

Prepared by: Mr. Vipin Wani


Examples on PDA
19

➢ Example 6: Design a PDA for accepting a language {X | X ε {a,b}* and na(X)>nb(X)}.

Prepared by: Mr. Vipin Wani


Examples on PDA
20

➢ Example 7: Design a PDA for accepting a language L={0n 1m 0n | m, n>=1}

➢ Solution: In this example n number of 0s are followed by m number of 1s which is

again followed by n number of 0s. To construct PDA for this raed 1st zero and push
it to stack, on reading 1s do nothing and on reading last zero pop zero.

1. δ (q0, 0, Z0) = (q0, 0Z0)

2. δ (q0, 0, 0) = (q0, 00)

3. δ (q0, 1, 0) = (q1, 0)

4. δ (q1, 1, 0) = (q1, 0)

5. δ (q1, 0, 0) = (q1, ε)

6. δ (q0, ε, Z0) = (q2, Z0)

Prepared by: Mr. Vipin Wani


Examples on PDA
21

➢ Example 7: Here q0 is Initial state and q2 is final state

➢ Let us Simulate the PDA for the String 0011100

(q0, 0011100, Z0) ⊢ (q0, 011100, 0Z0)


⊢ (q0, 11100, 00Z0)
⊢ (q0, 1100, 00Z0)
⊢ (q1, 100, 00Z0)
⊢ (q1, 00, 00Z0)
⊢ (q1, 0, 0Z0)
⊢ (q1, ε, Z0)
⊢ (q2, Z0)
Accept State

Prepared by: Mr. Vipin Wani


PDA Acceptance
22

➢ A language can be accepted by Pushdown automata using two approaches:

➢ 1. Acceptance by Final State:

➢ 2. Acceptance by Empty Stack:

Prepared by: Mr. Vipin Wani


Accepted By Final State
23

➢ The PDA is said to accept its input by the final state if it enters any final state in

zero or more moves after reading the entire input. Let P =(Q, ∑, Γ, δ, q0, Z, F) be a
PDA.

➢ The language acceptable by the final state can be defined as: L(PDA) = {w | (q0, w,

Z) ⊢* (p, ε, α), p ∈ F}

Prepared by: Mr. Vipin Wani


Accepted By Empty Stack
24

➢ On Reading the input string from the initial configuration for some PDA, the stack

of PDA gets empty.

➢ Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA.

➢ The language acceptable by empty stack can be defined as:

➢ N(PDA) = {w | (q0, w, Z) ⊢* (p, ε, ε), p ∈ Q}

Prepared by: Mr. Vipin Wani


Equivalence of Acceptance by Final State and Empty Stack
25

➢ If L = N(P1) for some PDA P1, then there is a PDA P2 such that L = L(P2). That

means the language accepted by empty stack PDA will also be accepted by final
state PDA.

➢ If there is a language L = L (P1) for some PDA P1 then there is a PDA P2 such that L

= N(P2). That means language accepted by final state PDA is also acceptable by
empty stack PDA.

Prepared by: Mr. Vipin Wani


Examples on PDA
26

➢ Example 1: Design a PDA for accepting a language via Empty Stack {anbn | n>=1}.

➢ Solution: In this language, n number of a's should be followed by n number of b's.


Hence, we will apply a very simple logic, and that is if we read single 'a', we will
push a's onto the stack. As soon as we read 'b' then for every single 'b' only one 'a'
should get popped from the stack. The Instantaneous Description for this example
can be given as
➢ δ (q0, a, Z0) = (q0, aZ0)

➢ δ (q0, a, a) = (q0, aa)

➢ δ (q0, b, a) = (q1, ε)

➢ δ (q1, b, a) = (q1, ε)

➢ δ (q1, ε, Z0) = (q2, ε)

Prepared by: Mr. Vipin Wani


Examples on PDA
27

➢ Example 1:

➢ Here q0 is Initial state and q2 is final state

➢ Let us Simulate the PDA for the String aaabbb

(q0, aaabbb, Z0) ⊢ (q0, aabbb, aZ0)


⊢ (q0, abbb, aaZ0)
⊢ (q0, bbb, aaaZ0)
⊢ (q1, bb, aaZ0)
⊢ (q1, b, aZ0)
⊢ (q1, ε, Z0)
⊢ (q2, ε)
Accept State
The language is accepted via Empty Stack.

Prepared by: Mr. Vipin Wani


Examples on PDA
28

➢ Example 6: Design a PDA for accepting a language {X | X ε {a,b}* and

na(X)>nb(X)} Via accepted by Final State.

Prepared by: Mr. Vipin Wani


Non Deterministic PDA
29

➢ The non-deterministic pushdown automata is very much similar to NFA.


➢ The CFG which accepts deterministic PDA accepts non-deterministic PDAs as well.
➢ Similarly, there are some CFGs which can be accepted only by NPDA and not by
DPDA. Thus NPDA is more powerful than DPDA.
➢ NPDA is more powerful or is more Strick than PDA
➢ In NPDA more than one move can be possible on reading the input symbol.

Prepared by: Mr. Vipin Wani


Non Deterministic PDA (Example)
30

➢ Design PDA for Palindrome strings (Even Strings).


➢ Solution:
➢ Suppose the language consists of string L = {, aa, bb, , baab, aaaa, abba, ......]. The
string can be odd palindrome or even palindrome. The logic for constructing PDA is
that we will push a symbol onto the stack till half of the string then we will read each
symbol and then perform the pop operation. We will compare to see whether the
symbol which is at top of stack is similar to the symbol which is read, then we will
pop the symbol. Whether we reach to end of the input, we expect the stack to be
empty.

Prepared by: Mr. Vipin Wani


Non Deterministic PDA (Example)
31

➢ Solution:
➢ R1: δ (q0, a, Z0) = (q0, aZ0)
➢ R2: δ (q0, b, Z0) = (q0, bZ0)
➢ R3: δ (q0, a, b) = (q0, ab)
➢ R4: δ (q0, b, a) = (q0, ba)
➢ R5: δ (q0, a, a) = (q0, aa)
➢ R6: δ (q0, b, b) = (q0, bb)
➢ R7: δ (q0, b, b) = (q1, ε)
➢ R8: δ (q0, a, a) = (q1, ε)
➢ R9: δ (q1, a, a) = (q1, ε)
➢ R10: δ (q1, b, b) = (q1, ε)
➢ R11: δ (q1, ε, Z0) = (q2, ε)
Prepared by: Mr. Vipin Wani
Non Deterministic PDA (Example)
32

➢ Solution: Let us Simulate for abaaba

1.δ(q0, abaaba, Z) ⊢ δ(q0, baaba, aZ) Apply rule 1


2. ⊢ δ(q0, aaba, baZ) Apply rule 4
3. ⊢ δ(q0, aba, abaZ) Apply rule 3
4. ⊢ δ(q1, ba, baZ) Apply rule 9
5. ⊢ δ(q1, a, aZ) Apply rule 10
6. ⊢ δ(q1, ε, Z) Apply rule 11
7. ⊢ δ(q2, ε) Accept the String

Prepared by: Mr. Vipin Wani


Non Deterministic PDA (Example)
33

➢ Solution: FA for given PDA


(a, a, ε)
(a,Z0,aZ0)
(b,b, ε)
(b,Z0,bZ0)

q0 q1 q2
(a, a, ε) (ε ,Z0, Z0)

(b,b, ε)
(a, a,aa)
(b,b,bb)
(a, b,ab)
(b,a,ba)

Prepared by: Mr. Vipin Wani


Non Deterministic PDA (Example)
34

➢ Design PDA for Palindrome strings (Odd Strings).


➢ Solution:
➢ Suppose the language consists of string L = {, a, b, aba, bab, aabaa, baaab, ......]. The
string can be odd palindrome or even palindrome. The logic for constructing PDA is
that we have assume every input symbol as a part of string or a middle symbol of w
& wr, and have derive two rules for every symbol.

Prepared by: Mr. Vipin Wani


Non Deterministic PDA (Example)
35

➢ Solution:
➢ R12: δ (q0, b, b) = (q1, b)
➢ R1: δ (q0, a, Z0) = (q0, aZ0)
➢ R13: δ (q1, a, a) = (q1, ε)
➢ R2: δ (q0, a, Z0) = (q1, Z0)
➢ R14: δ (q1, b, b) = (q1, ε)
➢ R3: δ (q0, b, Z0) = (q0, bZ0)
➢ R15: δ (q1, ε, Z0) = (q2, ε)
➢ R4: δ (q0, b, Z0) = (q1, Z0)
➢ R5: δ (q0, a, b) = (q0, ab)
➢ R6: δ (q0, a, b) = (q1, b)
➢ R7: δ (q0, b, a) = (q1, ba)
➢ R8: δ (q0, b, a) = (q1, a)
➢ R9: δ (q0, a, a) = (q0, aa)
➢ R10: δ (q0, a, a) = (q1, a)
➢ R11: δ (q0, b, b) = (q0, bb)
Prepared by: Mr. Vipin Wani
Non Deterministic PDA (Example)
36

➢ Solution: Let us Simulate for ababa

1.δ(q0, ababa, Z) ⊢ δ(q0, baba, aZ) Apply rule 1


2. ⊢ δ(q0, aba, baZ) Apply rule 7
3. ⊢ δ(q1, ba, baZ) Apply rule 10
4. ⊢ δ(q1, a, aZ) Apply rule 14
5. ⊢ δ(q1, ε, Z) Apply rule 13
6. ⊢ δ(q2, ε) Apply rule 15
Accept the String

Prepared by: Mr. Vipin Wani


CFG to PDA Conversion
37

➢ 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

Prepared by: Mr. Vipin Wani


CFG to PDA Conversion
38

➢ The first symbol on R.H.S. production must be a terminal symbol. The following steps are
used to obtain PDA from CFG is:
➢ For the given grammar we can accept the PDA accepted by Empty stack.
➢ Let us Consider
➢ G={V, T, P, S} Then PDA can be given as
➢ P=({q}, T, VUT, δ, q, s, Ф)

➢ The Transition Function can be given by following Rules


1. δ(q, ε, A) = (q, α)
Where the production rule is A → α
2. δ(q, a, a) = (q, ε) for every terminal symbol

Prepared by: Mr. Vipin Wani


CFG to PDA Conversion (Examples)
39

➢ Example 1: Construct PDA for CFG and also check weather 010000 is accepted by PDA
➢ S→ 0BB
➢ B→0S| 1S | 0
➢ Solution:
➢ Let PDA P= {{q}, {0,1}, {S, B, 0, 1}, δ, q, s, Ф}
➢ R1: δ(q, ε, S) = {(q, 0BB)}
➢ R2: δ(q, ε, B) = {(q, 0S), (q, 1S), (q, 0)}
➢ R3: δ(q, 0, 0) = (q, ε)
➢ R4: δ(q, 1, 1) = (q, ε)
➢ Now Let us check weather 010000 is accepted by PDA

Prepared by: Mr. Vipin Wani


CFG to PDA Conversion (Examples)
40
➢ Example 1:
(q, 010000, S) ⊢ (q, 010000, 0BB) By Rule 1
⊢ (q, 010000, BB)
⊢ (q, 10000, 1SB)
⊢ (q, 0000, SB)
⊢ (q, 0000, 0BBB)
⊢ (q, 000, BBB)
⊢ (q, 000, 0BB)
⊢ (q, 00, BB)
⊢ (q, 00, 0B)
⊢ (q, 0, B)
⊢ (q, 0, 0)
⊢ (q2, ε)
Accepted by PDA

Prepared by: Mr. Vipin Wani


CFG to PDA Conversion (Examples)
41

➢ Example 2: 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:

Prepared by: Mr. Vipin Wani


CFG to PDA Conversion (Examples)
42

➢ Example 2: The PDA can be given by:


➢ 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, ε)}

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
43

➢ Let PDA P= {Q, ∑, Γ, q0, Z0, F, δ}


➢ And CGF G={V, T, P, S,}
➢ Then to convert PDA to CFG derive the four tuples of CFG as follow
a. Variable in CFG (V): It has Special Symbol (Start Symbol) S and
remaining variables are [pAq] Where p & q are states in Q and A is stack symbol in Γ.
b. Terminals (T): Terminals in CFG ε ∑ in PDA
c. Start Symbol S : S ε V
d. Productions :
1. For all states in Q
S→[q0z0P] where p & q ε Q
2. For other transitions in P
If δ (q, x, A) = (p, β1, β2, ………. βm) then
( q, A, qm+1) → x[p, β1 , q2][q2, β2 , q3]………[qm, βm , qm+1] ----For all states q1, q2, …...qm+1 in Q
3. If For δ (q, x, A) = (p, ε)
 [qAp]→a
4. If For δ (q, ε, A) = (p, ε)
 [qAP]→ ε
Prepared by: Mr. Vipin Wani
PDA to CFG Conversion
44
➢ Example 1: Convert given PDA to equivalent CFG when,
R1: δ(q0, a, Z0) = (q0, XZ0)
R2: δ(q0, a, X) = (q0, XX)
R3: δ(q0, b, X) = (q1, ε)
R4: δ(q1, b, X) = (q1, ε)
R5: δ(q1, ε, Z0) = (q2, ε)
➢ Solution: Let us find first production rules for the equivalent grammar

➢ The special symbol S is derived using S→[q0z0P] for all P

S→[q0, Z0, q0] and S→[q0, Z0, q1]


P=({q0,q1}, {a,b}, {Z0, X}, δ, q0, Z0, Ф)
Now let us derive remaining symbols using
δ (q, x, A) = (p, β1, β2, ………. βm) then
q, a, qm+1) → x[p, β1 , q2][q2, β2 , q3]………[qm, βm , qm+1]

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
45
➢ Example 1:

Now Consider Transition Function R1

R1: δ(q0, a, Z0) = (q0, XZ0)

[q0, Z0, q0 ] → a [q0, X, q0 ] [q0, Z0, q0 ]

[q0, Z0, q0 ] → a [q0, X, q1 ] [q1, Z0, q0 ]

[q0, Z0, q1 ] → a [q0, X, q0 ] [q0, Z0, q1 ]

[q0, Z0, q1 ] → a [q0, X, q1 ] [q1, Z0, q1 ]

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
46
➢ Example 1:

Now Consider Transition Function R2


δ(q0, a, X) = (q0, XX)
[q0, X, q0 ] → a [q0, X, q0 ] [q0, X, q0 ]

[q0, X, q0 ] → a [q0, X, q1 ] [q1, X, q0 ]

[q0, X, q1 ] → a [q0, X, q0 ] [q0, X, q1 ]

[q0, X q1 ] → a [q0, X, q1 ] [q1, X, q1 ]


Now Consider Transition Function R3
R3: δ(q0, b, X) = (q1, ε)
[q0, X, q1 ]→b

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
47
➢ Example 1:

Now Consider Transition Function R4


R4:δ(q1, b, X) = (q1, ε)
[q1, X, q1 ]→b
Now Consider Transition Function R5
R5: δ(q1, ε, Z0) = (q1, ε)
[q1, Z0, q1 ]→ ε

Let us write all productions together:

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
48
➢ Example 1:
S→[q0, Z0, q0] and S→[q0, Z0, q1]
[q0, Z0, q0 ] → a [q0, X, q0 ] [q0, Z0, q0 ]
[q0, Z0, q0 ] → a [q0, X, q1 ] [q1, Z0, q0 ]
[q0, Z0, q1 ] → a [q0, X, q0 ] [q0, Z0, q1 ]
[q0, Z0, q1 ] → a [q0, X, q1 ] [q1, Z0, q1 ]
[q0, X, q0 ] → a [q0, X, q0 ] [q0, X, q0 ]
[q0, X, q0 ] → a [q0, X, q1 ] [q1, X, q0 ]
[q0, X, q1 ] → a [q0, X, q0 ] [q0, X, q1 ]
[q0, X, q1 ] → a [q0, X, q1 ] [q1, X, q1 ]
[q0, X, q1 ]→b
[q1, X, q1 ]→b
[q1, Z0, q1 ]→ ε

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
49
➢ Example 1:

➢ So these are the productions in equivalent grammar but it may have some useless

symbols let us identify and eliminate such productions.


➢ Like the symbol [q1, Z0, q0 ], [q1, x, q0 ] and [q1, Z0, q1 ] are present is right
hand side but not in left hand side it means is useless symbol so remove
all productions having [q1, Z0, q0 ] , [q1, Z0, q0 ] & [q1, Z0, q1 ] .
➢ As well as observe the symbol [q0, X, q0 ] is not reachable symbol so also
remove all productions having [q0, X, q0 ] .
➢ Also Observe the starting Symbol which produces [q0, Z0, q0] is also a
useless as is not present in left hand side. So also remove it.

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
50
➢ Example 1:
S→[q0, Z0, q0] --------------------------------------------------Eliminated
and S→[q0, Z0, q1]
[q0, Z0, q0 ] → a [q0, X, q0 ] [q0, Z0, q0 ] ---------------------------------------Eliminated
[q0, Z0, q0 ] → a [q0, X, q1 ] [q1, Z0, q0 ] -------------------------------------Eliminated
[q0, Z0, q1 ] → a [q0, X, q0 ] [q0, Z0, q1 ] --------------------------------------Eliminated
[q0, Z0, q1 ] → a [q0, X, q1 ] [q1, Z0, q1 ]
[q0, X, q0 ] → a [q0, X, q0 ] [q0, X, q0 ] --------------------------------------Eliminated
[q0, X, q0 ] → a [q0, X, q1 ] [q1, X, q0 ] --------------------------------------Eliminated
[q0, X, q1 ] → a [q0, X, q0 ] [q0, X, q1 ] --------------------------------------Eliminated
[q0, X, q1 ] → a [q0, X, q1 ] [q1, X, q1 ]
[q0, X, q1 ]→b
[q1, X, q1 ]→b
[q1, Z0, q1 ]→ ε

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
51
➢ Example 1: So the final Productions in grammar will be

S→[q0, Z0, q1]

[q0, Z0, q1 ] → a [q0, X, q1 ] [q1, Z0, q1 ]

[q0, X, q1 ] → a [q0, X, q1 ] [q1, X, q1 ]

[q0, X, q1 ]→b

[q1, X, q1 ]→b

[q1, X, q2 ]→ ε

Prepared by: Mr. Vipin Wani


PDA to CFG Conversion
52
➢ Example 1: So Let us rewrite the CFG again Where

CFG G= {V, T, P, S} Replaced [q0, Z0, q1] By A


Replaced [q0, X, q1] By B
P={ S→ A
Replaced [q1, Z0, q1] By C
A→ aBC
Replaced [q1, X, q1 ] By D
B→aBD
B→b
D→b
C→ ε }
Variables V={A, B , C, D, S}
Terminals T={a, b}
Starting Symbol is S.
Which is Expected Grammar equivalent to PDA.
Prepared by: Mr. Vipin Wani
PDA to CFG Conversion
53
➢ Example 2: Convert Given PDA to CFG

R1: δ(q0, 1, Z0) = (q0, XZ0)


R2: δ(q0, 1, X) = (q0, XX)
R3: δ(q0, 0, X) = (q1, X)
R4: δ(q0, ε, Z0) = (q0, ε)
R5: δ(q1, 1, X) = (q1, ε)
R6: δ(q0, 1, Z0) = (q0, Z0)

Prepared by: Mr. Vipin Wani


Prepared by: Mr. Vipin Wani 54

You might also like