0% found this document useful (0 votes)
26 views

Class 19 Pushdown Automata

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Class 19 Pushdown Automata

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

PUSHDOWN AUTOMATA

We have already discussed finite automata. But finite automata can


be used to accept only regular languages.
Pushdown Automata is a finite automata with extra memory called
stack which helps Pushdown automata to recognize Context Free
Languages.
The finite automata that we have studied earlier are not capable to
recognize the CFL such as { wcwR / w ε Σ*}
Because by reading a string in this language from left to right
automata must ‘remember’ the string before it encounters symbol ‘c’
and then compare it to the string after ‘c’.
But our FA is not capable to remember any thing.
From the above concept it is clear that FA can be extended by
addition of an auxiliary storage to accept CFL
The PDA is essentially a FA with control of both an input tape and a
stack to store what it has read.
Basically a pushdown automaton is −
"Finite state machine" + "a stack"

A pushdown automaton has three


components
• an input tape,
• a control unit, and
• a stack with infinite size.

The stack head scans the top symbol of the


stack.
A stack does two operations −
• Push − a new symbol is added at the top.
• Pop − the top symbol is read and removed.

A PDA may or may not read an input symbol,


but it has to read the top of the stack in every
transition.
A Pushdown Automata (PDA) can be defined as 7 tuples :
M= (Q, ∑, Γ, q0, Z, F, δ )

• Q is the set of states


• ∑ is the set of input symbols
• Γ is the set of pushdown symbols (which can be pushed and
popped from stack)
• q0 is the initial state
• Z is the initial pushdown symbol (which is initially present in stack)
• F is the set of final states
• δ is a transition function which maps Q x {Σ ∪ ∈} x Γ into Q x Γ*.

In a given state, PDA will read input symbol and stack symbol (top of
the stack) and move to a new state and change the symbol of stack.
Transitions and Graphical Notation
Let ((P, α, β), (q, γ) ) ε δ, then it means that
• read α from the tape
• pop the string β from the stack
• move from state p to state q
• push string γ on to the stack
The following diagram shows a transition in a PDA from a state q1 to state q2, labeled
as a,b → c

This means at state q1, if we encounter an input string ‘a’ and top symbol of the
stack is ‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to state q2.
Instantaneous Description (ID)
Instantaneous Description (ID) is an informal notation of
how a PDA “computes” a input string and make a decision
that string is accepted or rejected.

An ID is a triple (q, w, α), where:


1. q is the current state.
2. w is the remaining input.
3.α is the stack contents, top at the left.
Turnstile Notation
The "turnstile" notation is used for connecting pairs of ID's
that represent one or many moves of a PDA. The process of
transition is denoted by the turnstile symbol "⊢".
Consider a PDA (Q, ∑, S, δ, q0, I, F). A transition can be
mathematically represented by the following turnstile
notation − (p, aw, Tβ) ⊢ (q, w, αβ)

This implies that while taking a transition from state p to


state q, the input symbol ‘a’ is consumed, and the top of
the stack ‘T’ is replaced by a new string ‘α’.
Note − If we want zero or more moves of a PDA, we have
to use the symbol (⊢*) for it.
Comparison of NFA and PDA
NFA
I. (p,a,q) ͼ Δ means if machine M is in state P, then
on reading ‘a’ from input tape go to state q
II. (p,ͼ,q) ͼ Δ means if machine M is in state p, goes
to state q, without consuming input.

PDA
III. ((p,a, β), (q,y) ) 
if machine M is in state P, the symbol read from
input tape is ‘a’ and β is on the top of the stack
goes to state q and replace β by y on the top of the
stack
1. ((S,a,e), (S,a)) 
if machine M is in state S, reads ‘a’ remains in state S and
push ‘a’ onto stack (e- empty stack)
2. ((S,c,e),( f,e)) 
if read ‘c’ in state S and stack is empty, goes to final state f
and nothing to push onto stack.
3. ((S,e,e), (f,e)) 
if in state S, go to state f.
4. ((f,a,a) , (f, Ф) 
if read ‘a’ in state f, remain in state f and pop ‘a’ from stack

PDA’s are non-deterministic having some finite


number of moves in each situation.
Consider the language L = { wwr/w is in (0+1) }
Solu:
Language is palindrome over alphabet {0,1}
i) Start in q0, read symbols till the end of the string w and store
them on the stack by pushing a copy of each input symbol onto
stack.
ii) We guess we have seen the middle so go to state q1.

iii) Once in q1, we compare input symbols with the symbol at the top
of the stack. If they match we consume the input symbol, pop
the stack and proceed. If they do not match, the input string is
not palindrome.

iv) If we empty the stack, we have seen input ‘w’ followed by wr. The
string is accepted
PDA P (Q, ∑, Γ, δ, q0, z0, F)
Q ={q0,q1,q2}
∑ = {0,1}
Γ = {0,1, z0}
Start state = q0
Start stack symbol =z0
Final state {q2}
I. Set to push w onto stack
δ ( q0 , 0 , z0) = { (q0,0z0)}
δ ( q0 , 1 , z0) = { (q0,1z0)}
δ ( q0 , 0 , 0) = { (q0,`00)}
δ ( q0 , 0 , 1) = { (q0, 01)}
δ ( q0 , 1 , 0) = { (q0, 10)}
δ ( q0 , 1 , 1) = { (q0, 11)}
To find middle of the string, NPDA switches state from q0 to
q1
δ ( q0 , ε , z0) = { (q1, z0)}
δ ( q0 , ε , 0) = { (q1, 0)}
δ ( q0 , ε , 1) = { (q1, 1)}

Match Wr against contents on stack


δ ( q1 , 0 , 0) = { (q1, ε)}
δ ( q1 , 1 , 1) = { (q1, ε)}

To reach final state


δ ( q1 , ε , z0) = { (q2, z0)}
Design a PDA which accepts the language L={w ε {a,b}* / w has the
equal no’s of a’s and b’s }
Solu:
There is no concern of the position of a’s and b’s .
The machine can read input ‘a’ first or input ‘b’ first, ‘a’ after ‘b’, ‘b’
after ‘a’, ‘a’ followed by ‘a’ or ‘b’ followed by ‘b’ and of course null
string is also accepted.
1. ((S , a , ε), (q , a))
Q = {S,q,f}
Σ = {a,b}
2. ((S , b , ε), (q , b))
Γ = {a,b} 3. ((q , a , a), (q , aa))
F= {f} 4. ((q , b , b), (q , bb)
5. ((q , a , b), (q , ε))
6. ((q , b , a), (q , ε))
7. ((q , ε , ε), (f , ε))
Design PDA for the language L= {wcwR/ w ε {a,b}
It reads the first half of its input, if remains in its initial states and
trasfer symbols from the input string onto stack.
When the machine sees a ‘C’ in the input string, it switches from state
S to state f.
Once it matches the input with stack, we can pop the element of the
stack.
If both input and stack empty halt the machine.

Q={s.f} 1. ((S , a , ε), (S , a))


Σ = {a,b,c} 2. ((S , b , ε), (S , b))
Γ = {a,b}
F= {f}
3. ((S , c , a), (f , a))
4. ((S , c , b), (f , b))
5. ((f , a , a), (f , ε))
6. ((f , b , b), (f , ε)
7. ((f , ε , ε), (f , ε)
The Language of a PDA
Acceptance by Final State:
Let M = (Q, Σ, Γ,δ,q0,z0,F) be a PDA
Then the language accepted by M is of the string such that

L(M) = ( w / (q0,w,z0) ⊢* (p, ε, ϒ ) , p ε F, ϒ ε Γ* )


i.e, PDA M consumes w from the input and enters an accepting
state.

Acceptance by Empty Stack


Let N(M) be a language accepted by M is given by
N(M) = { w / (q0,w,z0) ⊢* (p, ε, ε ) , p ε Q }
Equivalence of PDA and CFG

A Language is generated by a CFG


a)iff if it is accepted by a PDA by empty stack
b)iff if it is accepted by a PDA by final state.
From CFG to PDA’s
Theorem
For any context free language L, there exists an pda M such
that L = L(M)
Proof
Let G= (V,T, P, S) be a grammar. There exists a Greibatch
Normal Form, then we can construct pda which simulates
left most derivations in this grammar.
M = (Q, Σ, Γ,δ,q0,z0,F) where
Q = {q0,q1,q2} = set of states
Σ - terminals of grammar
Γ - Variables of grammar U {z0} = V0 U {z0}
F= {qj} is final state
The transition function will include

δ(q0,λ, z) = {(q1,Sz)}, so that after the first move of M, the


stack contains the start symbol S of the derivation. (the
stack symbol z is a marker to allow us to detect the end of
the derivation)

In addition, the set of transition rules is such that


1. δ ( q0,λ, A) = { (q,α) } for each A α in P
2. δ ( q, a, a ) = { (q, λ )} for each a ε Σ
Example:
Construct a PDA for the CFG
G= ({s}, {a,b}, S, P ) where SaSbb / a

Solution
Step 1:
We must construct equivalent CFG with Greibach
Normal Form by changing productions to
S aSA/a
AbB
Bb
Step 2:
Now we can construct pda in the following way
M = (Q, Σ, Γ,δ,q0,z0,F) where
Q = {q0,q1,q2}. q0 – Initial state , q2- Final state
Σ - terminals of grammar = {a,b}
Γ - Variables of grammar U {z0} = V0 U {z0}
F – {q2} = Final state and δ is defined as:

(i) First the start symbol S is put on the stack


δ ( q0,λ, z0) = { (q1, Sz0)}

(ii) The Production SaSA/a will be simulated by PDA


as per A α rule
δ ( q1,λ, S) = { (q1, aSA), (q1,a)}
(iii) In Similar manner, the other productions are
δ ( q1,λ, A) = { (q1, bB)} for rule A bB
δ ( q1,λ, B) = { (q1, b)} for rule Bb.

(iv) For Each a ε Σ the corresponding productions


are:
δ ( q1, a, a ) = { (q1, λ )}
δ ( q1, b, b ) = { (q1, λ )}

(v) Then the end of the derivation is identified by


the stack symbol
δ ( q1, λ, z ) = { (q2, λ )}
Design a pda that accepts the language of the grammar
SAB
AaA / ε
B aBb/ ε (ε – null state) and check for string aaaabb

Solution:
We can construct pda
M = (Q, Σ, Γ,δ,q0,z0,F) where
Q = {q0, f}. q0 – Initial state , f- Final state
Σ - terminals of grammar = {a,b}
Γ - Variables of grammar U {z0} = V0 U {z0} = { S,A, B,Z0}
F – {f} = Final state and δ is defined as:
R1 : δ ( q0,λ, z0) = { (f, Sz0)}

R2 : δ (f , λ, S) = { (f, AB)} for production 1

R3 : δ (f , λ, A) = { (f, aA)} for production 2

R4 : δ (f , λ, A) = { (f, λ)} for production 2

R5 : δ (f , λ, B) = { (f, aBb), (f, λ )} for production 3

R6 : δ ( f, a, a ) = { (f, λ )}

R7 : δ ( f, b, b ) = { (f, λ )}

R8 : δ ( f, λ, z0 ) = { (f, λ )}
PDA to CFG
R1: The Production for start symbol S are given by
S[q0,z0,q] for each state q in Q

R2: For Each move that pops a symbol from stack with transition as
δ(q,a,z0) = (q1, ε) induces a production as
(q,z0,q1)  a for each q’ in Q

R3: For Each move that does not pop symbol from stack with
transition as δ(q1,a,z0) = (q1, z1z2z3z4) induces a production as

[q1,z0,q’’] a[q1,z1,q2][q2,z2,q3] [q3,z3,q4]…..[qm,zm,q’’]


The Productions in P are induced by move of PDA as follows:
1) S productions are given by
S [q0,zo,q] for every q ϵ Q
2)For Every Popping move
δ(q,a,z) = (q’, ϵ )
[q, z, q’] a
3)For Each push move
δ(q,a,z) = (q1, z1z2z3…zm) induces many productions

[q, z, q’]  a [q1 , z1, q2] [q2 , z2, q3] [q3 , z3, q4]………….[qm, zm. q’]

Where each state q’, q1, q2, …qm can be any state in Q
Generate CFG for given PDA M is defined as M= {{q0,q1}, {a.b},{z,zo},
δ,q0,z0,q1} where δ is given as follows
1.δ(q0,b,z0) = (q0, zz0)
2.δ(q0,ε,z0) = (q0, ε)
3.δ(q0,b,z) = (q0, z1z2)
4.δ(q0,a,z) = (q1, z)
5.δ(q1,b,z) = (q1, ε)
6.δ(q1,a,z0) = (q0, z0)

Solu:
R1:
S [q0,z0,q0]
S [q0,z0,q1]
R2:
2. δ(q0,ε,z0) = (q0, ε)
δ(q0,z0,q0) ε
R3:
3. δ(q0,b,z) = (q1, z1z2)
δ[q0,z, q0]  b [q1, z1, q0] [q0, z2, q0]
δ[q0,z, q0]  b [q1. z1, q1] [q1, z2, q0]
δ[q0,z, q1]  b [q1. z1, q0] [q0, z2, q1]
δ[q0,z, q1]  b [q1. z1, q1] [q1, z2, q1]
R3:
4. δ(q0,a,z) (q1,z)

δ[q0,z, q0]  a [q0, z, q0]


R2: 5. δ(q1,b,z) = (q1,ε)
δ[q1,z,q1]  b

R3:
6. δ(q1,a, z0) = (q0, z0)
δ[q1,z0,q0]  a [q0, z0, q0]
δ[q1, z0,q1]  a [q0, z0, q1]

R3:
1. δ(q0,b, z0) = (q0, zz0)
δ[q0, z0,q0]  b [q0, z, q0] [q0, z0, q0]
δ[q0, z0,q0]  b [q0, z, q1] [q1, z0, q0]
δ[q0, z0,q1]  b [q0, z, q0] [q0, z0, q1]
δ[q0, z0,q1]  b [q0, z, q1] [q1, z0, q1]
Construct CFG from PDA
δ(q,1,z0) = (q, xz0)
δ(q,1,x) = (q, xx)
δ(q,0,x) = (p, x)
δ(q, ε, x) = (q, ε )
δ(p, 1, x) = (p, ε )
δ(p, 0, z0) = (q, z0 )

R1:
S [q, z0, q]
S [q, z0, p]
R2
δ(q, x, q)  ε
δ(p, x, p) 1
δ(q,1, z0) = (q, xz0)
δ[q, z0,q]  1 [q, x, q] [q, z0, q]
δ[q, z0,q]  1 [q, x, p] [p, z0, q]
δ[q, z0,p]  1 [q, x, q] [q, z0, p]
δ[q, z0,p]  1 [q, x, p] [p, z0, p]

δ(p, 0, z0) = (q, z0 )


δ[p, z0,q]  0 [p, z0, p]
δ[q1, z0,q]  0 [p, z0, q]

δ(q, 0, x) = (p, x )
δ[q, z0,p]  0 [q, z0, q]
δ[p, z0,p]  0 [q, z0, p]
Deterministic PDA’s
• A PDA is deterministic if there is atmost one move for any
input symbol, any top stack symbol and at any state
• A no move or move without advancing input string are
possible in a deterministic PDA.
• A move without advancing input string implies that no
other move exists.
• The pushdown automation for WCWR is deterministic
• The set of languages accepted by DPDA is only a subset of
the languages accepted by NPDA’s.
• A regular language is language accepted by a DPDA
• Not all language is accepted by a DPDA are regular
• A language accepted by DPDA have an unambiguous CFG
• Not all unambiguous languages are accepted by DPDA.

You might also like