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

PPT 3.2.1 PDA Definition Examples

The document discusses Push Down Automata (PDA), focusing on its definition, structure, and applications in recognizing context-free languages (CFLs). It outlines the learning objectives for understanding PDAs, including their configurations, acceptance criteria, and examples of language recognition. Additionally, it explains the equivalence between PDAs that accept by final state and those that accept by empty stack.

Uploaded by

fodes88196
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PPT 3.2.1 PDA Definition Examples

The document discusses Push Down Automata (PDA), focusing on its definition, structure, and applications in recognizing context-free languages (CFLs). It outlines the learning objectives for understanding PDAs, including their configurations, acceptance criteria, and examples of language recognition. Additionally, it explains the equivalence between PDAs that accept by final state and those that accept by empty stack.

Uploaded by

fodes88196
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Department of Computer and Science Engineering (CSE)

UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE ENGINEERING
Bachelor of Engineering
Theory of Computation (CST-353)

Topic: PDA
DISCOVER . LEARN .
University Institute of Engineering (UIE) EMPOWER
Department of Computer and Science Engineering (CSE)

Learning Objectives & Outcomes


Objective:
• To understand the concept of Push Down Automata.

Outcome:
• Student will understand the
 Push down automata
 Applications of Push Down Automata

University Institute of Engineering (UIE)


Department of Computer and Science Engineering (CSE)

PDA - the automata for CFLs


• What is?
– FA to Reg Lang, PDA is to CFL
• PDA == [  -NFA + “a stack” ]
• Why a stack?

Input -NFA Accept/reject


string

A stack filled with “stack symbols”


3
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

Pushdown Automata - Definition

• A PDA P := ( Q,∑,, δ,q0,Z0,F ):


– Q: states of the -NFA
– ∑: input alphabet
– : stack symbols
– δ: transition function
– q0 : start state
– Z0: Initial stack top symbol
– F: Final/accepting states

4
University Institute of Engineering (UIE)
old state input symb. Stack top new state(s) new Stack top(s)

δ : Q x ∑ x  => Q x 
δ : The Transition Function

δ(q,a,X) = {(p,Y), …}
1. state transition from q to p
2. a is the next input symbol X Y
3. X is the current stack top symbol
a
dde n--

q p
e
NNoo

4. Y is the replacement for X;


tteer

it is in * (a string of stack symbols)


r
n
m

i. Set Y =  for: Pop(X) Y=? Action


m
in
in

ii. If Y=X:
i) Y= Pop(X)
issm
i

stack top is unchanged


m

iii. If Y=Z1Z2…Zk: X is popped ii) Y=X Pop(X)


and is replaced by Y Push(X)
in reverse iii) Y=Z1Z2..Zk Pop(X)
order (i.e., Z1 will be the Push(Zk)
new stack
Push(Zk-1)
top) …
Push(Z2)
Push(Z1)

University Institute of Engineering (UIE) 5


Example
Example : Define the pushdown automata for language {anbn | n > 0}
Solution : M = where Q = { q0, q1 } and Σ = { a, b } and Γ = { A, Z }
and &delta is given by :

University Institute of Engineering (UIE) 6


Department of Computer and Science Engineering (CSE)

PDA’s Instantaneous Description (ID)


A PDA has a configuration at any given instance: (q,w,y)
– q - current state
– w - remainder of the input (i.e., unconsumed part)
– y - current stack contents as a string from top to bottom of
stack
If δ(q,a, X)={(p, A)} is a transition, then the following are also true:
– (q, a, X ) |--- (p,,A)
– (q, aw, XB ) |--- (p,w,AB)
|--- sign is called a “turnstile notation” and represents one
move
|---* sign represents a sequence of moves

7
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

How does the PDA for Lwwr work on input “1111”?

All moves made by the non-deterministic PDA


(q0,1111,Z0)

(q1,1111,Z0) Path dies…


(q0,111,1Z0)

(q0,11,11Z0) (q1,111,1Z0) Path dies…

(q0,1,111Z0) (q1,11,11Z0)

(q0,,1111Z0) (q1,1,111Z0) (q1,1,1Z0) Acceptance by


final state:

(q1, ,1111Z0) (q1, ,11Z0) (q1, ,Z0) = empty input


Path dies… AND
Path dies…
(q2, ,Z0) 8final state
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)
There are two types of PDAs that one can design:
those that accept by final state or by empty stack
Acceptance by…
• PDAs that accept by final state:
– For a PDA P, the language accepted by P, denoted by L(P)
by final state, is:
• {w | (q0,w,Z0) |---* (q,, A) }, s.t., q  F Checklist:
- input exhausted?
- in a final state?

• PDAs that accept by empty stack:


– For a PDA P, the language accepted by P, denoted by N(P)
by empty stack, is:
• {w | (q0,w,Z0) |---* (q, , ) }, for any q  Q. Checklist:
- input exhausted?
Q) Does a PDA that accepts by empty stack - is the stack empty?
need any final state specified in the design?
10
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

Example: L of balanced parenthesis

An equivalent PDA that


PDA that accepts by final state accepts by empty stack
(,Z0 / ( Z0
PF: (,Z0 / ( Z0
PN: (, ( / ( (
(,( / ( ( ), ( / 
), ( /  ,Z0 / 

,Z0/ Z0 start
start
q0 q1 q0
,Z0/ Z0 ,Z0/ Z0

How will these two PDAs work on the input: ( ( ( ) ) ( ) ) ( )


11
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

PDAs accepting by final state and empty


stack are equivalent
• PF <= PDA accepting by final state
– PF = (QF,∑, , δF,q0,Z0,F)
• PN <= PDA accepting by empty stack
– PN = (QN,∑, , δN,q0,Z0)
• Theorem:
– (PN==> PF) For every PN, there exists a PF s.t. L(PF)=L(PN)

– (PF==> PN) For every PF, there exists a PN s.t. L(PF)=L(PN)

13
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

How to convert an empty stack PDA into a final state PDA?


PN==> PF construction
• Whenever PN’s stack becomes empty, make PF go to a
final state without consuming any addition symbol
• To detect empty stack in PN: PF pushes a new stack
symbol X0 (not in  of PN) initially before simultating PN
PF = (QN U {p0,pf}, ∑,  U {X0}, δF, p0, X0, {pf})

PF: P N: , X0/ X0
, X0/Z0X0 , X0/ X0
New
start p0 q0 , X0/ X0 pf

, X0 / X0
, X0/ X0

14
University Institute of Engineering (UIE)
Example: Matching parenthesis “(” “)”

P N: ( {q0}, {(,)}, {Z0,Z1}, δN, q0, Z0 ) P f: ( {p0,q0 ,pf}, {(,)}, {X0,Z0,Z1}, δf, p0, X0 , pf)

δN : δN(q0,(,Z0) = { (q0,Z1Z0) } δf : δf(p0, ,X0) = { (q0,Z0) }


δN(q0,(,Z1) = { (q0, Z1Z1) } δf(q0,(,Z0) = { (q0,Z1 Z0) }
δf(q0,(,Z1) = { (q0, Z1Z1) }
δN(q0,),Z1) = { (q0, ) }
δf(q0,),Z1) = { (q0, ) }
δN(q0, ,Z0) = { (q0, ) } δf(q0, ,Z0) = { (q0, ) }
δf(p0, ,X0) = { (pf, X0 ) }
(,Z0 /Z1Z0 (,Z0/Z1Z0
(,Z1 /Z1Z1 (,Z1/Z1Z1
),Z1 /  ),Z1/ 
,Z0 /   ,Z0/ 

start
q0
start
,X /Z X
0 0 0
,X / X
0 0
p0 q0 pf

Accept by empty stack Accept by final state

University Institute of Engineering (UIE) 15


Department of Computer and Science Engineering (CSE)

How to convert an final state PDA into an empty stack PDA?

PF==> PN construction
• Main idea:
– Whenever PF reaches a final state, just make an  -transition into a new
end state, clear out the stack and accept
– Danger: What if PF design is such that it clears the stack midway without
entering a final state?
 to address this, add a new start symbol X0 (not in  of PF)
PN = (Q U {p0,pe}, ∑,  U {X0}, δN, p0, X0)

PN:
, X0/Z0X0 , any/  , any/ 
New
start p0 q0 , any/  pe

, any/ 
PF
16
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

CFGs == PDAs ==> CFLs

PDA by PDA by

final state empty stack

?
CFG

17
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

Example 1

Let Lwwr = {wwR | w is in (0+1)*}


• CFG for Lwwr : S==> 0S0 | 1S1 | 
• PDA for Lwwr :
• P := ( Q,∑, , δ,q0,Z0,F )

= ( {q0, q1, q2},{0,1},{0,1,Z0},δ,q0,Z0,{q2})

18
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)
Initial state of the PDA:

PDA for Lwwr q0


Stack
1. δ(q0,0, Z0)={(q0,0Z0)} top Z0
2. δ(q0,1, Z0)={(q0,1Z0)} First symbol push on stack

3. δ(q0,0, 0)={(q0,00)}
4. δ(q0,0, 1)={(q0,01)}
5. δ(q0,1, 0)={(q0,10)} Grow the stack by pushing
6. δ(q0,1, 1)={(q0,11)} new symbols on top of old
(w-part)
7. δ(q0, , 0)={(q1, 0)}
8. δ(q0, , 1)={(q1, 1)} Switch to popping mode
9. δ(q0, , Z0)={(q1, Z0)} (boundary between w and wR)

10. δ(q1,0, 0)={(q1, )}


Shrink the stack by popping matching
11. δ(q1,1, 1)={(q1, )}
symbols (wR-part)
12. δ(q1, , Z0)={(q2, Z0)}
19
University Institute of Engineering (UIE)
PDA as a state diagram

δ(qi,a, X)={(qj,Y)}

Next Current Stack


input stack Top
Current symbol top Replacement
state (w/ string Y)

a, X / Y Next
qi qj state

University Institute of Engineering (UIE) 20


PDA for Lwwr: Transition Diagram

Grow stack ∑ = {0, 1}


0, Z0/0Z0 = {Z0, 0, 1}
1, Z0/1Z0 Pop stack for
Q = {q0,q1,q2}
0, 0/00 matching symbols
0, 1/01
1, 0/10 0, 0/ 
1, 1/11 1, 1/ 

q0 q1 q2
, Z0/Z0
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 Go to acceptance
Switch to
popping mode
This would be a non-deterministic PDA

University Institute of Engineering (UIE) 21


Example 2: language of balanced
paranthesis

Pop stack for ∑ = { (, ) }


matching symbols = {Z0, ( }
Grow stack
Q = {q0,q1,q2}
(, Z0 / ( Z0
(, ( / ( ( ), ( / 

q0 q1 q2
, Z0 / Z0 ), ( /  , Z0 / Z0
, Z0 / Z0 Go to acceptance (by final state)
when you see the stack bottom symbol
Switch to
(, ( / ( (
popping mode
(, Z0 / ( Z0 To allow adjacent
blocks of nested paranthesis

University Institute of Engineering (UIE) 22


Example 2: language of balanced
paranthesis (another design)

∑ = { (, ) }
(,Z0 / ( Z0 = {Z0, ( }
(,( / ( (
), ( / 
Q = {q0,q1}

start ,Z0/ Z0
q0 q1
,Z0/ Z0

University Institute of Engineering (UIE) 23


Example 3
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.
There are two parts for designing this PDA:
If 1 comes before any 0's
If 0 comes before any 1's.

We are going to design the first part i.e. 1 comes before 0's. The
logic is that read single 1 and push two 1's onto the stack.
Thereafter on reading two 0's, POP two 1's from the stack. The δ
can be
δ(q0, 1, Z) = (q0, 11, Z) Here Z represents that stack is empty
δ(q0, 0, 1) = (q0, ε)
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 change state from q0
to q1. [Note that state q1 indicates that first 0 is read and still
second 0 has yet to read].
University Institute of Engineering (UIE) 24
Continue..
Being in q1, if 1 is encountered then POP 0. Being in q1, if 0 is
read then simply read that second 0 and move ahead. The δ will
be:
δ(q0, 0, Z) = (q1, 0Z)
δ(q1, 0, 0) = (q1, 0)
δ(q1, 0, Z) = (q0, ε) (indicate that one 0 and one 1 is already r
ead, so simply read the second 0)
δ(q1, 1, 0) = (q1, ε)
Now, summarize the complete PDA for given L is:
δ(q0, 1, Z) = (q0, 11Z)
δ(q0, 0, 1) = (q1, ε)
δ(q0, 0, Z) = (q1, 0Z)
δ(q1, 0, 0) = (q1, 0)
δ(q1, 0, Z) = (q0, ε)
δ(q0, ε, Z) = (q0, ε) ACCEPT state

University Institute of Engineering (UIE) 25


Department of Computer and Science Engineering (CSE)

Application of PDA
• For designing the parsing phase of a compiler (Syntax
Analysis).
• For implementation of stack applications.
• For evaluating the arithmetic expressions.
• For solving the Tower of Hanoi Problem.

University Institute of Engineering (UIE)


Department of Computer and Science Engineering (CSE)

Summary
• PDAs for CFLs and CFGs
– Non-deterministic
– Deterministic
• PDA acceptance types
1. By final state
2. By empty stack
• PDA
– IDs, Transition diagram

• Applications of PDA

27
University Institute of Engineering (UIE)
Department of Computer and Science Engineering (CSE)

FAQ :

1. What are the different ways of language acceptances by


a PDA and define them
2. Convert the given expression to PDA
I --> a | b | Ia | Ib | I0 | I1

3. What is the additional feature a PDA has when compared


with NFA?
4. Design a PDA for accepting a language {L= a^nb^n | n
>=1}

University Institute of Engineering (UIE)


Department of Computer and Science Engineering (CSE)

REFERENCES :
• https://www.geeksforgeeks.org/introduction-of-
pushdown-automata/
• Martin J.C., “Introduction to Languages and Theory of
Computation”, Tata McGraw-Hill Publishing Company
Limited, 3rd Edition.
• https://www.tutorialspoint.com
• https://en.wikipedia.org

University Institute of Engineering (UIE)

You might also like