Pushdown Automata (PDA)
Pushdown Automata (PDA)
o 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.
o 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.
o 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.
PDA Components:
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.
Finite control: The finite control has some pointer which points the current symbol
which is to be read.
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.
Example 1: Construct a PDA for language L = {wcw R | w={0, 1}*} where w R is the reverse
of w.
(OR)
Design a PDA that accepts the language of odd palindrome
Approach used in this PDA –
Keep on pushing 0’s and 1’s no matter whatever is on the top of stack until reach the middle
element c. When middle element ‘c’ is scanned then process it without making any changes in
stack. Now if scanned symbol is ‘1’ and top of stack also contain ‘1’ then pop the element from
top of stack or if scanned symbol is ‘0’ and top of stack also contain ‘0’ then pop the element
from top of stack. If string becomes empty or scanned symbol is z and stack becomes empty,
then reach to final state else move to dead state.
Step 1: On receiving 0 or 1, keep on pushing it on top of stack without going to next state.
Step 2: On receiving an element ‘c’, move to next state without making any change in stack.
Step 3: On receiving an element, check if symbol scanned is ‘1’ and top of stack also contain
‘1’ or if symbol scanned is ‘0’ and top of stack also contain ‘0’ then pop the element from top of
stack else move to dead state. Keep on repeating step 3 until string becomes empty.
Step 4: Check if symbol scanned is ‘∈’ and stack does not contain any element then move to
final state else move to dead state.
δ( q0, ∈, z) = { ( q0, z) }
δ is given by :
δ( q0, 0, z ) = { ( q0, 0z ) }
------
∈,z,nop,q2)
(q1, z q2
For construction of even length palindrome, user has to use Non Deterministic Pushdown
Automata (NPDA).
A NPDA is basically an NFA with a stack added to it.
The NPDA for this language is identical to the previous one except for epsilon transition.
However, there is a significant difference, that this PDA must guess when to stop pushing
symbols, jump to the final state and start matching off of the stack.
Therefore this machine is decidedly non-deterministic.
Keep on pushing 0’s and 1’s no matter whatever is on the top of stack and at the same time
keep a check on the input string, whether reach to the second half of input string or not.
If reach to last element of first half of the input string then after processing the last element of
first half of input string make an epsilon move and move to next state.
Now if scanned symbol is ‘1’ and top of stack also contain ‘1’ then pop the element from top of
stack or if scanned symbol is ‘0’ and top of stack also contain ‘0’ then pop the element from top
of stack.
If string becomes empty or scanned symbol is ‘∈’ and stack becomes empty, then reach to
final state else move to dead state.
Step 1: On receiving 0 or 1, keep on pushing it on top of stack and at a same time keep on
checking whether reach to second half of input string or not.
Step 2: If reach to last element of first half of input string, then push that element on top of
stack and then make an epsilon move to next state.
Step 3: On receiving an element, check if symbol scanned is ‘1’ and top of stack also contain
‘1’ or if symbol scanned is ‘0’ and top of stack also contain ‘0’ then pop the element from top of
stack else move to dead state. Keep on repeating step 3 until string becomes empty.
Step 4: Check if symbol scanned is ‘∈’ and stack does not contain any element then move to
final state else move to dead state.
δ is given by :
δ( q0, 0, z ) = { ( q0, 0z ) }
------
∈,z,nop,q2)
(q1, z q2
δ is given by :
δ( q0, a, z ) = { ( q0, az ) }
δ( q0, b, a) = { ( q1, ∈) }
δ( q0, a, a) = { ( q0, aa ) }
δ( q1, b, b) = { ( q1, ∈) }
δ( q1, ∈, z) = { ( q2, ∈) }
ID for aaabbb is
(q0,aaabbb,z)
⊢(q0,aabbb,az)
⊢(q0,abbb,aaz)
⊢(q0,bbb,aaaz)
⊢(q1,bb,aaz)
⊢(q1,b,az)
⊢(q1, ∈,z)
⊢(q2,z)
Note :
The above pushdown automaton is deterministic in nature because there is only
one move from a state on an input symbol and stack symbol.
The non-deterministic pushdown automata can have more than one move from a
state on an input symbol and stack symbol.
It is not always possible to convert non-deterministic pushdown automata to
deterministic pushdown automata.
Expressive Power of non-deterministic PDA is more as compared to expressive
deterministic PDA as some languages which are accepted by NPDA but not by
deterministic PDA which will be discussed in next article.
The push down automata can either be implemented using accepetance by
empty stack or accepetance by final state and one can be converted to another.
Example 4:
Design a PDA for accepting a language {a2nbn | n>=1}.
δ(q1, b, a) = (q2, ∈)
δ(q0, a, a) = (q1, aa)
δ(q2, b, a) = (q2, ∈)
δ(q2, ∈ , z) = (q3, ∈)
Let us see how this automata works for aaaabb.
ID for aaaabb is
⊢(q0,aaabb,az)
(q0,aaaabb,z)
⊢(q1,aabb,az)
⊢(q0,abb,aaz)
⊢(q1,bb,aaz)
⊢(q1,b,az)
⊢(q1, ∈,z)
⊢(q2,z)
Example 5:
Design a PDA for accepting a language {0n1m0n | m, n>=1}.
Solution: In this PDA, n number of 0's are followed by m number of 1's followed n
number of 0's. Hence the logic for design of such PDA will be as follows:
Push all 0's onto the stack on encountering first 0's. Then if we read 1, just do nothing.
Then read 0, and on each read of 0, pop one 0 from the stack.
--------
∈,z,nop,q3)
(q2, z q3
Now we will simulate this PDA for the input string "0011100".
ID is
Example 6:
Construct a PDA that accepts the language L over {0, 1} by empty stack which
accepts all the string of 0's and 1's in which a number of 0's are twice of
number of 1's.
Solution:
We are going to design the first part i.e. 1 comes before 0's. The logic is that read 1’s
and push onto the stack. For every 1 we have two 0’s. For first part of 0’s pop 1 and for
second part of 0’s we are not doing any operation.
1n02n
Now, consider the second part i.e. if 0 comes before 1's. The logic is that read first 0
push it onto the stack and for second 0 we are not doing any operation. Whenever we
see 1’s we pop all 0’s from the stack.
02n1n
Now we will simulate this PDA for the input string "000011".
PDA Acceptance
A language can be accepted by Pushdown automata using two approaches:
1. Acceptance by Final State: 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:
2. Acceptance by Empty Stack: 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:
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.
Example 7:
Design PDA that accepts the language L over {a, b, c} by acceptance by final
state where L={a3bncn / n>0}
Soln:
Here ensure that exactly 3 a’s are read initially. Then read all b’s on to the stack and
match them with c’s by popping off. To ensure three a’s, read each a and change state.
δ(q0, a, Z) = (q1, Z)
δ(q1, a, z) = (q2, z)
δ(q2, a, Z) = (q3, Z)
δ(q3, ε, z) = (qf, z)
δ(q3, b, z) = (q4, bz)
δ(q4, b, b) = (q4, bb)
δ(q4, c, b) = (q5, z)
δ(q5, c, b) = (q5, z)
δ(q5, ε, z) = (qf, z)
Now we will simulate this PDA for the input string "aaabbcc".
Step 3: The initial symbol of CFG will be the initial symbol in the PDA.
δ(q, ε, A) = (q, α)
Example 1:
Construct PDA for the given CFG, and test whether 010 4 is acceptable
by this PDA.
S → 0BB
B → 0S | 1S | 0
Solution:
The PDA can be given as:
Example 2:
Draw a PDA for the CFG given below:
S → aSb
S→a|b|ε
Solution:
Example 4: Consider the grammar G = (V, T, P, S) with V = {S}, T = {a, b, c}, and
P = {S → aSa, S → bSb, S → c}
Solution:
Let the equivalent PDA, M = ({q}, {a, b, c}, {a, b, c, S}, δ, q, S)
where δ:
R1: δ(q, ε , S) = {(q, aSa), (q, bSb ), (q,c )}
R 2 : δ(q, a, a) = {(q,
ε )}
R3:δ(q, b, b) = {(q, ε )}
R4: δ(q, c, c) = {(q, ε )}
Solution:
Let the equivalent PDA, M = ({q}, {0, 1}, {0, 1, S, A, B}, δ, q, S)
where δ:
R1: δ(q, ε , S) = {(q, A), (q, B ), (q, ε )}
R2: δ(q, ε , A) = {(q, 0S), (q, 1B), (q, 0)}
R3: δ(q, ε , B) = {(q, 0S), (q, 1A), (q, 1)}
R 4 : δ(q, 0, 0) = {(q, ε )}
R5: δ(q, 1, 1) = {(q, ε )}
Example 7: Construct a PDA that will accept the language generated by the grammar G = ({S, A},
{a, b}, P, S) with the productions S → AA / a, A → SA / b and test whether “abbabb” is in N(M).
Solution:
Let the equivalent PDA, M = ({q}, {a, b}, {a, b, S, A}, δ, q, S)
where δ:
R1: δ(q, ε , S) = {(q, AA), (q, a }
R2: δ(q, ε , A) = {(q, SA), (q, b)}
R3: δ(q, a, a) = {(q, ε )}
R4: δ(q, b, b) = {(q, ε )}
⊢ δ(q, bb , bA)
R3
⊢ δ(q, εb , A)
R2
⊢ δ(q, b , b)
R4
⊢ δ(q, ε , ε)
R2
R4
Tutorial Problems:
1. Consider the grammar G = (VN, VT, P, S) and test whether “abbabb” is in N(M).
Where P :
S → abA / baA / B / ε
A→ bS / b
B→ S
C→ε
2. Consider the grammar G = (V, T, P, S)
Where P :
A → aB
B → aB/bB/ε
3. Consider the grammar G = (V, T, P, S) and test whether “0101001” is in N(M). Where P :
S → 0S/1A/1/0B/0
A → 0A/1B/0/1
B → 0B/1A/0/1
4. Consider the grammar G = (V, T, P, S)
Where P :
A → Ba/Ab/b
B → Ca/Bb
C → Aa/Cb/a
5. Consider the grammar G = (V, T, P, S)
Where P :
A → aB/bA/b
B → aC/bB
C → aA/bC/a
6. Consider the grammar G = (V, T, P, S)
Where P :
S → ABCD
A → aab
B → bba / bbaB
C → bab
D → aab / aabD
L(G) is CFL.
Procedure: Construction of G.
We define, G=(V,T,P,S)
V={S} U {[q,z,q’]/q,q’ ∈ Q, Z ∈ Γ}
Where,
Here,
S -> start symbol for G
q, q’ -> states
z -> push down symbol
The productions in P are induced by moves of PDA as follows:
R1: S productions are given by S → [q0,Z, q] for every qQ
For example:
We have two states (q0, q1), so two rules for starting variable.
S → [q0,Z,q0]
S → [q0 ,Z, q1]
R2: Each moves erasing a push down symbol given by (q’, ∈) ∈ δ(q, a, Z) induces the
production
[q Z q’] → a
R3: Each move non-erasing a push down symbol given by (q’ Z1 Z2 Z3…. Zn) ∈ δ(q,
a, Z) induces many productions of forms
[q Z q’] → a [q1 Z1 q2] [q2 Z2 q3]……[qn Zn q’]
Where each of states q1, q2, …. qn can be any state in Q
Solution:
Step 1: Find the push and pop operations:
1. δ(q0, 1, Z0) ={( q0, XZ0)} - Push
2. δ(q0, 1, X) = {( q0, XX)} - Push
3. δ(q0, 0, X) = {( q1, X)} - Push
4. δ(q0, ε, X) = {( q0, ε)} - Pop
5. δ(q1, 1, X) = {( q1, ε)} - Pop
6. δ(q0, 0, Z0) = {( q1, Z0)} - Push
Productions for the variables [q1 z0 q0] and [q1 x q0] and productions for [q0 z0 q0] and [q0 x q0]
do not exist. Deleting all productions involving these four variables on either the right or left
and we end up with the following productions.
1. S → [q0 Z0 q1]
2. [q0 X q0] → ε
3. [q1 X q1] → 1
4. [q0 Z0 q1] → 1 [q0 X q1] [q1 Z0 q1]
5. [q0 X q1] → 1 [q0 X q1] [q1 X q1]
6. [q0 X q1] → 0 [q1 X q1]
7. [q0 Z0 q1] → 0 [q1 Z0 q1]
2. Give the grammar for the language N(M) where M=({ q0 ,q1}, {0,1}, {X,Z0}, δ, q0, Z0, Φ) and δ
is given by,
3. Give the grammar for the language N(M) where M=({ q0 ,q1}, {a,b}, {Z,Z0}, δ, q0, Z0, Φ) and δ
is given by,