PUSH DOWN AUTOMATA
(PDA)
• CFGs define Context Free Languages. (CFL)
• An acceptor for every CFL, is a PDA
Push Down Automata (PDA)
Push Down Automata (PDA)
Two types of PDA:
1. Non-Deterministic PDA
2. Deterministic PDA
Deterministic PDA
A Deterministic pushdown automaton (or DPDA) M is a
seven tuple:
(K, ∑, Г, δ, s, Z0, A) where:
• K is a finite set of states.
• ∑ is the input alphabet,
• Г is the stack alphabet,
• s € K is the start state,
• Z0 is the special symbol used to mark the bottom of
stack
• A is the subset of K, is the set of accepting states,
and
• δ is the transition function which maps from
K X {∑ U ε} X Г → K X Г*
Deterministic PDA
• A PDA M = ( K, Σ, Γ , δ, s, Z0, A ) to be deterministic
PDA, if and only if the following conditions are met:
1. δ(q, a, X) has at most one member for any state q, where q
€ K, a € Σ or it can be ε and X is any stack symbol (γ)
2. For any q € K , X € Γ and a € ∑
If δ(q, a, X) is nonempty, then δ(q, ε, X) must be empty.
Non-Deterministic PDA
A Non-deterministic pushdown automaton (or NPDA) M is a
seven tuple:
(K, ∑, Г, δ, s, Z0, A) where:
K is a finite set of states.
∑ is the input alphabet,
Г is the stack alphabet,
s € K is the start state,
Z0 is the special symbol used to mark the bottom of stack
A is the subset of K, is the set of accepting states, and
δ is the transition function, which maps from
K X {∑ U ε} X Г → 2 K X Г *
A configuration of a PDA M is an element, which captures the
three things that can make a difference to M's future behavior:
1. Its current state,
2. The input that is still left to read and
3. The contents of its stack.
Let C be a computation of M on input w € ∑* Then we will say
that:
• C is an accepting computation if C = (s, w, Z0) l- M* (q, ε, Z0),
for some q € A. A computation accepts only if it runs out of
input when it is in an accepting state and the stack is empty.
• C is a rejecting computation if C = (s, w, Z0) l- M* (q, w’, α),
for where C is not an accepting computation and where M
has no moves that it can make from (q, w’, α).
• A computation can reject only if the criteria for accepting
have not been met and there are no further moves
(including following ε-transitions) that can be taken.
Languages accepted by PDA
• The language accepted by PDA M, denoted L(M), is the
set of all strings accepted by M.
• Acceptance by final state: After consuming the input
string, if a PDA enters an accepting state, then we call this
approach as acceptance by final state.
• Acceptance by empty stack: Set of input string that cause
the PDA to empty its stack, starting from initial ID.
Instantaneous Descriptions of a PDA (I D):
• How PDA processes the input string, that means the PDA
goes from configuration to configuration, in response to
input symbols ( or ε ) can be represented using
Instantaneous Descriptions of PDA.
• The Instantaneous Descriptions of a PDA has a triplet form
(q, w, γ ) where
• q is the state.
• w is the remaining input, and
• γ is the stack contents.
(q, aw, Zα)
Design a PDA to accept the language
L = { a n bn | n ≥ 1 } .
Draw the graphical representation
(Transition diagram) of PDA obtained.
Also write the ID for the string ‘aaabbb’.
(q0, a, Z0)
PDA to accept the language L = { an bn | n ≥ 1 } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where s = q0 , A = qf and δ is given by
Graphical representation OR Transition diagram of PDA
Instantaneous Descriptions of PDA (ID) for the string: aaabbb
Acceptance by final state: After consuming the input string, if a
PDA enters an accepting or final state, then we call this approach
as acceptance by final state.
Acceptance by empty stack: Set of input string that
cause the PDA to empty its stack, starting from initial
ID.
Design a PDA to accept the language
L = { wCwR | w € (a,b)*} .
Draw the graphical representation
(Transition diagram) of PDA obtained.
Also write the ID for the string ‘baaCabb’.
PDA to accept the language L = {wCwR | w € (a,b)*} is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where s = q0 , A = qf and δ is given by
Graphical representation OR Transition diagram of PDA
Instantaneous Descriptions of PDA (ID) for the string: baaCaab
• So far all of the PDAs that we have built have been
deterministic.
• So each machine followed only a single computational
path.
• In each PDAs we observed that; For any q € K , X € Γ
and a € ∑ If δ(q, a, X) is nonempty, then δ(q, ε, X)
must be empty condition is satisfied.
Design a NPDA to accept the language
L = { wwR | w € (a,b)*} .
Draw the graphical representation
(Transition diagram) of PDA obtained.
Also write the ID for the string ‘baaabb’.
• PDA is nondeterministic because it cannot know when it
has reached the middle of its input.
• Before each character is read, it has two choices:
1. It can guess that it has not yet reached the middle of the
string. In that case, it stays in start state, where it pushes
each symbol it reads.
2. It can guess that it has reached the middle. In that case, it
takes the transition from start state to next state, where
it pops one symbol for each symbol that it reads.
PDA to accept the language L = {wwR | w €(a,b)*} is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where s = q0 , A = qf and δ is given by
ils
fa
DA
DP
of
nd c :
n
itio
co isti
nd in
co rm
se dete
us on
ca is n
e
Be A
PD
Graphical representation OR Transition diagram of PDA
Instantaneous Descriptions of PDA (ID) for the string: baaaab
Design a NPDA to accept the language
L = { na (w) = nb (w) | w € (a,b)*}
OR
L = { #a(w) = #b(w) | w € (a,b)*}
Draw the graphical representation
(Transition diagram) of PDA obtained.
Also write the ID for the string ‘baaababb’.
PDA to accept the language L = {na (w) = nb (w) | w €(a,b)*} is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where s = q0 , A = qf and δ is given by
ils
fa
DA
DP
of
nd c :
n
itio
co isti
nd in
co rm
se dete
us on
ca is n
e
Be A
PD
Graphical representation OR Transition diagram of PDA
Instantaneous Descriptions of PDA (ID) for the string: baaababb
Conversion of:
CFG to PDA
PDA to CFG
Equivalence of Context-Free Grammars and PDAs
• Building a PDA from a Grammar
• Building a Grammar from a PDA
Conversion of CFG to PDA
1. CFG to PDA using Top Down Parsing technique.
2. CFG to PDA using Bottom Up Parsing
technique.
Conversion of CFG to PDA using Top Down parsing approach
• A top-down parser answers the question; Could
G generate w?" by starting with S, applying the
rules of P. and seeing whether ‘w can be
derived.
Top Down parsing means Deriving the string by applying LMD
Working Principle of Top Down parsing
ted fro m CFG
enera
Input g Down parsing
op
using T
L(M) = L(G)
δ(q, ε, Z0) = ( qf, Z0)
• PDA M has only two states; The only purpose of the first
state is to push S onto the stack and then go to the
second state. M's stack will actually do all the work by
keeping track of what G is trying derive.
• Conversion CFG to PDA begins by pushing the start
symbol ‘S’ of G onto the stack without reading any input
symbol and then go to second state.
• In second state pop S from stack and replace S with the
appropriate rule which is defined on RHS of S.
• As long as the symbol on the top of the stack is a non
terminal in G, this process continues. effectively applying
the rules of G to the top of the stack.
• The appearance of a terminal symbol c on the top of the
stack means that G is attempting to generate c.
• M only wants to pursue paths that generate its input string
w. So. at that point, it pops the top symbol off the stack
reads its next input character. and compares the two.
• If they match, the derivation that M is pursuing is consistent
with generating-w and the process continues.
• If they don't match, the path that M is currently following
ends without accepting
• So at each step. PDA M either applies a grammar rule,
without consuming any input, or it reads an input character
and pops one terminal symbol off the stack.
• When stack is empty, and PDA M has read all the characters
of string w, G can generate string w , so PDA M accepts
Conversion of CFG to PDA using Top Down parsing
Formally, PDA M = ( {p, q, qf }, ∑, Г, δ, Z0 {p}, { qf } )
where δ contains:
1. The start up transition δ (p, ε, Z0) = (q, S) which pushes the
start symbol of G onto the stack and goes to state q.
2. For each production rule in G of the form:
X → γ1 γ2 γ3 …………… γn introduce the transition in
state q as:
δ (q, ε, X) = (q, γ1 γ2 γ3 …………… γn )
3. For each terminal symbol or character c € ∑ introduce the
transition of the form:
δ (q, c, c) = (q, ε )
Convert the following CFG to PDA using Top down
parsing approach
E →E+T
E → T
T →T*F
T → F
F → (E )
F → id
PDA M = ( {p, q, qf }, ∑, Г, δ, Z0 {p}, { qf } ) where δ contains:
Obtain a PDA equivalent to the following grammar
using top down parsing approach.
S → aA
A → aA | bA | a | b
PDA M = ( {p, q, qf }, ∑, Г, δ, Z0 {p}, { qf } ) where δ contains:
For each Non terminal S and A, the transition functions are:
δ(p, ε, Z0) = { (q, S) }
δ(q, ε, S) = { (q, aA) }
δ(q, ε, A) = { (q, aA) , (q, bA), (q, a) , (q, b) }
For each terminal a and b the transition functions are:
δ(q, , a, a ) = { (q, ε) }
δ(q, , b, b ) = { (q, ε) }
δ(q, , ε, Z0 ) = { (qf, Z0) }
Obtain a PDA equivalent to the following grammar using
top down parsing approach.
S → aABB
A → aBB | a
B → bBB | A
C → a|ε
PDA M = ( {p, q, qf }, ∑, Г, δ, Z0 {p}, { qf } ) where δ contains:
For each Non terminal S, A,B and C, the transition
functions are:
δ(p, ε, Z0) = { (q, SZ0) }
δ(q, ε, S) = { (q, aABB) }
δ(q, ε, A) = { (q, aBB) , (q, a) }
δ(q, ε, B) = { (q, bBB) , (q, A) }
δ(q, ε, C) = { (q, a), (q, ε) }
For each terminal a and b the transition functions are:
δ(q, , a, a ) = { (q, ε) }
δ(q, , b, b ) = { (q, ε) }
δ(q, , ε, Z0 ) = { (qf, Z0) }
Conversion of CFG to PDA using Bottom Up parsing approach
• A bottom-up parser answers the question. "Could G generate w'?"
by starting with w, applying the rules of P backwards, and seeing
whether S can be reached.
Bottom Up parsing means Deriving the string by applying RMD in reverse
Parse tree for the string aab using Bottom Up parsing
• PDA M can read an input symbol and shift it onto the
stack.
• Whenever a sequence of elements at the top of the
stack matches, in reverse the right-hand side of some
rule in P. M can pop that sequence of and replace it by
the left-hand side of P. When this happens, we say that
M has reduced by rule P
Conversion of CFG to PDA using Bottom Up parsing approach
Formally, PDA M = ( {p, q}, ∑, Г, δ, Z0 {p}, { q} )
where δ contains:
1. The shift transition corresponding to input symbol (terminal
symbol) δ (p, c, Y) = (p, cY) for each c € ∑ and Y is any stack
symbol including Z0. (either terminal or non-terminal symbol of
G)
2. The reduce transitions for each rule in P as; X → γ1 γ2 γ3 … γn
introduce the transition of the form:
δ (p, ε, (γ1 γ2 γ3 … γn)R )= (p, X)
3. The finish up transition :
δ (p, ε, SZ0) = (q, Z0 )
Obtain a PDA equivalent to the following grammar
using Bottom up parsing approach.
S → aA
A → aA | bA | a | b
PDA M = ( {p, q}, ∑, Г, δ, Z0 {p}, { q } ) where δ contains:
Obtain a PDA equivalent to the following grammar using
Bottom Up parsing approach.
S → aABB
A → aBB | a
B → bBB | A
C → a|ε
PDA M = ( {p, q}, ∑, Г, δ, Z0 {p}, { q } ) where δ contains:
Obtain a PDA equivalent to the following grammar using
Bottom up parsing approach.
E →E+T
E → T
T →T*F
T → F
F → (E )
F → id
PDA M = ( {p, q}, ∑, Г, δ, Z0 {p}, { q } ) where δ contains:
Conversion of PDA to CFG
• The input symbols of PDA will be the terminal of CFG
• If the PDA moves from state qi to qj on consuming the
input a € Σ, when Z is the top of the stack, then non-
terminals of CFG are the triplets of the form (qiZqj )
• If q0 is the start state and qf is the final state then
(q0Zqf) is the start symbol of CFG.
The productions of CFG can be obtained from transitions of PDA
as shown below:
• For each transition of the form δ(qi,, a, Z) = (qj, AB) , introduce
the productions of the form
• qiZqk → a(qjAql ) (qlBqk) where ql and qk will take all possible
values from K.
• For each transition of the form δ(qi, , a, Z ) = (qj, ε) ,
introduce the production of the form: qiZqj → a
Convert the following PDA to CFG
δ(q0, a, Z) = (q0, AZ)
δ(q0, a, A) = (q1, ε) q0Aq1 → a
δ(q0, b, Z) = (q1, ε) q0Zq1 → b
K = { q0, q1} ql and qk will take all values in K.
δ(q0, a, Z) = (q0, AZ) introduce the productions as
q0Zq0 → a (q0Aq0) (q0Zq0) | a (q0Aq1) (q1Zq0)
q0Zq1 → a (q0Aq0) (q0Zq1) | a (q0Aq1) (q1Zq1)
Non-determinism and Halting
The computation of PDA halts on two conditions:
1. The computation C is an accepting computation.
2. The computation C ends in a configuration from which no δ
transition is possible.
Alternative Equivalent definition of PDA
1. By accepting the string by Final state.
2. By accepting the string by empty stack.
Alternatives that are not Equivalent to PDA
• PDA is FSM with a stack, which allows push and pop operation.
(LIFO)
Two alternatives that are not Equivalent to PDA:
1. Post Machine: Instead of stack, Queue data structure is used.
(FIFO)
2. Turing Machine: Which allows to read the string in forward and
backward directions and allow to write the corresponding
output string.