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

ACT CH4 Pushdown Automata

This document outlines pushdown automata (PDA) and their use in recognizing context-free languages. It defines a PDA as a 7-tuple consisting of states, input symbols, stack symbols, transition function, initial state, initial stack symbol, and accepting states. It describes the instantaneous description of a PDA's configuration as a triple of current state, remaining input, and stack contents. Examples are given of a PDA accepting the language of strings with equal numbers of a's and b's and another PDA rejecting an invalid string.

Uploaded by

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

ACT CH4 Pushdown Automata

This document outlines pushdown automata (PDA) and their use in recognizing context-free languages. It defines a PDA as a 7-tuple consisting of states, input symbols, stack symbols, transition function, initial state, initial stack symbol, and accepting states. It describes the instantaneous description of a PDA's configuration as a triple of current state, remaining input, and stack contents. Examples are given of a PDA accepting the language of strings with equal numbers of a's and b's and another PDA rejecting an invalid string.

Uploaded by

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

WOLKITE UNIVERSITY

College of Computing & Informatics


Department of Computer Science

Regular Program

Automata and Complexity Theory (CoSc3101)


Chapter Four
Pushdown automata (PDA)

By: Sisay T. (MSc.)


outline
• Pushdown automata
‒ Instantaneous Description of PDA
‒ Construction of PDA

• Non-deterministic pushdown automata


• Deterministic push down automata
• Pushdown automata context free languages

ACT CH-3: Context Free Languages 2


3
4
Initial Stack Symbol

Stack Stack

stack
$ z top
head

bottom special symbol


Appears at time 0
5
Pushdown Automaton -- PDA
Input String

Stack

States

6
The States

Input Pop Push


symbol symbol symbol

q1 a, b  c q2

7
a, b  c
q1 q2

input
a a

stack
b top Replac c
h e h
e e
$ $
8
a,   c
q1 q2

input
a a

stack c
b top Pus
b
h h h
e e
$ $
9
a,   
q1 q2

input
a a

stack
b top No b
h Change h
e e
$ $
10
a, b  
q1 q2

input
a a

stack
b top
h Pop h
e e
$ $ 11
Pop from Empty Stack
a, b  c
q1 q2
input
a
stack Pop Automaton halts!

top

If the automaton attempts to pop from


empty stack then it halts and rejects input

12
Formal Definition
Pushdown Automaton (PDA) is described by 7-tuples

M  (Q, Σ, Γ, δ, q0 , z , F ) Accept
states
States

Input Stack
alphabet Transition Initial start
Stack
function state symbol
alphabet
13
Transition function()

ACT CH-3: Context Free Languages 14


Instantaneous description of PDA
• Let PDA M =(Q, Σ, Γ, δ,q0,Z0,F), then its configuration at a given instant
can be defined by instantaneous description (ID)
• ID 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.
 w describes the remaining input.
 α describes the stack contents, top at the left.
• Turnstile Notation:
⊢ (turnstile) notation, represents relation b/n two consecutive IDs.
⊢* sign describes a sequence of moves.
ACT CH-3: Context Free Languages 15
ACT CH-3: Context Free Languages 16
ACT CH-3: Context Free Languages 17
Example-2: A computation of the string "aaabbb" by the PDA
accepting a language {anbn | n>=1}.

(q0 , aaabbb,$) (q1, aaabbb,$) 


(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$) 
(q2 , bb, aa$) (q2 , b, a$) (q2 ,  ,$) (q3 ,  ,$)
a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
18
(q0 , aaabbb,$)  (q1 , aaabbb,$) 
(q1 , aabbb, a$)  (q1 , abbb, aa$)  (q1 , bbb, aaa$) 
(q2 , bb, aa$)  (q2 , b, a$)  (q2 ,  ,$)  (q3 ,  ,$)

For convenience we write: (q0 , aaabbb,$)  (q3 ,  ,$)

Therefore, the string aaabbb is accepted by the given PDA

19
Rejection Example: Time 0
Input
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q b, a   q  , $  $ q
1 2 3
20
Rejection Example: Time 1
Input
a a b
$
Stack

current a,   a b, a  
state
 ,    q b, a   q  , $  $ q
q0 1 2 3
21
Rejection Example: Time 2
Input
a
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
22
Rejection Example: Time 3
Input a
a
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
23
Rejection Example: Time 4
Input a
a
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
24
Rejection Example: Time 5
Input a
a
a a b
$
Stack
reject
current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
25
There is no accepting computation for aab
The string aab is rejected by the PDA

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
26
Another PDA example


PDA M: L( M )  {vv : v  {a, b} }
R

a,   a a, a  
b,   b b, b  

q0
,    q1 , $  $ q2

27

Basic Idea: L( M )  {vv : v  {a, b} }
R

1. Push v 2. Guess 3. Match v R on input


on to the middle with v on stack
stack [pop v from the stack]
of input

4. Match
a,   a a, a  
found
b,   b b, b  

q0 ,    q1 , $  $ q2
28
Execution Example: Time 0
Input
a b b a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
29
Time 1
Input
a b b a
a
$

Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
30
Time 2
Input
b
a b b a
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
31
Time 3
Input
b
a b b a
Guess the middle a
of string $
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
32
Time 4
Input
b
a b b a
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
33
Time 5
Input
a b b a
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
34
Time 6
Input
a b b a
$
Stack
a,   a
a, a  
b,   b
b, b  
accept
q0 ,    q1 , $  $ q2
35
Rejection Example: Time 0
Input
a b b b
$
Stack

a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
36
Time 1
Input
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
37
Time 2
Input
b
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
38
Time 3
Input
b
a b b b
Guess the middle a
of string $
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
39
Time 4
Input
b
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
40
Time 5
Input There is no possible transition.

a b b b Input is not a
consumed $
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
41
There is no computation
that accepts string abbb

abbb  L(M )

a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
42
Language of PDA

Language L(M ) accepted by PDA M :



L( M )  {w : ( q0 , w, z )  ( q f ,  ,$)}

Initial state Accept state

• Given a PDA, P and a string w, P accepts w iff there is some series of


choices such that when P is run on w, it ends in a final state and
have an empty stack.

43
Construction of PDA
• Examle 1 : Design a PDA that accepts the language L = {a nb n | n ≥ 0}
Solution:
• Every time ‘a’ comes before ‘b’. When ‘a’ comes then push it in stack and if again
‘a’ comes then also push it.
• After that, when ‘b’ comes then pop one ‘a’ from the stack each time.
• So, at the end if the stack becomes empty then we can say that the string is
accepted by the PDA.

ACT CH-3: Context Free Languages 44


How can we use a stack to recognize w ∈ L
1. Push a special bottom of stack symbol $ to the stack
2. As long as you are seeing a’s in the input, push an a onto the stack.
3. While there are b’s in the input and there is a corresponding a on the
top of the stack, pop a from the stack
3.1. If at any point there is no a on the stack (hence you encounter $), you
should reject the string – not enough a’s!
3.2. If at the end of w, the top of the stack is NOT $ reject the string – not
enough b’s.
4. Otherwise accept the string.

ACT CH-3: Context Free Languages 45


PDA that accepts the language L = {a nb n | n ≥ 0}

ACT CH-3: Context Free Languages 46


Examle 2 :
• Construct a PDA for L = {wwR | w ∈ {0, 1} ∗} , where wR is the reverse
of string w.
Solution
• The PDA works by marking the first half of the string on the stack and
then verifying that the second half matches the reversed first half.
• If the process completes successfully, the PDA enters the accepting
state; otherwise, it enters the rejecting state.

ACT CH-3: Context Free Languages 47


PDA for L = {wwR | w ∈ {0, 1} ∗} operates as follows

ACT CH-3: Context Free Languages 48


ACT CH-3: Context Free Languages 49
Exercise 1
• Construct a PDA that accepts the language = {anbbban| n ≥ 1}
A. Show that the cceptability of aabbbaa using ID
B. Show that the rejection of aabbba using ID

ACT CH-3: Context Free Languages 50


Exercise 2
1. Design a PDA that accepts the language L = {anbm cn| n,m ≥ 1}
2. Obtain a PDA to accept the language L= {anb2n| n ≥ 1}
A. Show that the cceptability of aabbbb
B. Show that the rejection of aabbb

ACT CH-3: Context Free Languages 51


Non-deterministic pushdown automata
• A Non-deterministic Pushdown Automaton (NPDA) is a variation of the
Pushdown Automaton (PDA) that allows for non-deterministic transitions.
• In an NPDA, the transition function can lead to multiple possible next
configurations for a given combination of current state, input symbol, and
stack symbol.
• This non-determinism introduces branches in the computation, allowing
the NPDA to explore multiple paths simultaneously.

ACT CH-3: Context Free Languages 52


Con’t...
• NPDA is more powerful than a deterministic PDA in terms of
language recognition capabilities
• In this respect, the situation is not similar to the situation of
DFA-s and NFA-s.
• An NPDA accepts an input string if there exists at least one
computation path (sequence of transitions) that leads to an
accepting state and an empty stack.
• Nondeterministic PDA-s are equivalent to CFL-s.
ACT CH-3: Context Free Languages 53
Formal Definition of NPDA
PDA, M = (Q,Σ,Γ,δ,q0,Z0,F) is defined by the 7-tuple, where:
 Q : set of states
 Σ : input alphabet
 Γ : stack alphabet, Symbols that can be pushed and popped
 δ : Q×Σ U{ε}×Γ U {ε}→ 2Q× Γ U {ε} , Transition function
 q0∈ Q : start state
 Z0​: Initial stack symbol
 F⊆ Q : final states

ACT CH-3: Context Free Languages 54


Examples of NPDA
1. L={wwR|w)*}
2. L={wXwR|w)*,X=(a,b)}

ACT CH-3: Context Free Languages 55


Deterministic pushdown automata

• A deterministic pushdown automaton has at most one legal


transition for the same combination of input symbol, state, and top
stack symbol.
• This is where it differs from the nondeterministic pushdown automaton.

• Deterministic PDAs are of great practical importance, but not of particularly


great theoretical importance.

ACT CH-3: Context Free Languages 56


DPDA Con’t...
• A PDA (Q, Σ, Γ, δ, q0, Z0, F) is deterministic if:
 δ(q, a, X) has at most one member for any q in Q, a in Σ ∪ {ε} and X in Γ.
 If δ(q, a, X) is nonempty for some a in Σ, then δ(q, ε, X) must be empty.
• A language that can be recognized by a DPDA is called a deterministic
context-free language.
• A PDA can recognize { wwR | w is any string of a's and b's }, but no
DPDA can recognize this language.
• Since a DPDA can recognize the non-regular language { wcwR | w is
any string of a's and b's }, DPDA are strictly more powerful than finite
automata

ACT CH-3: Context Free Languages 57


Examples of DPDA
1. L={ancan|n>=1}
2. L={anbn|n>=1}
3. L={anbm|n }
4. L={wcwR|w)*}

ACT CH-3: Context Free Languages 58


Difference between NPDA and DPDA
S. No DPDA(Deterministic Pushdown Automata) NPDA(Non-deterministic Pushdown
Automata)

1. It is less powerful than NPDA. It is more powerful than DPDA.

It is possible to convert every DPDA to a It is not possible to convert every


2. corresponding NPDA. NPDA to a corresponding DPDA.

The language accepted by DPDA is a subset of the The language accepted by NPDA is not
3. language accepted by NDPA. a subset of the language accepted by
DPDA.

The language accepted by DPDA is called The language accepted by NPDA is


4. DCFL(Deterministic Context-free Language) which is called NCFL(Non-deterministic Context-
a subset of NCFL(Non-deterministic Context-free free Language
Language) accepted by NPDA.

ACT CH-3: Context Free Languages 59


Pushdown automata context free languages

• We can prove that PDAs accept exactly the context free languages by showing
that we can convert any CFG into a PDA and any PDA into a CFG

ACT CH-3: Context Free Languages 60


CFG to PDA Conversion
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

ACT CH-3: Context Free Languages 61


Example 1:
• Convert the following grammar to a PDA that accepts the same
language.
1.S → 0S1 | A
2.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:
1.S → 0SX | 1SY | ε
2.X → 1
3.Y → 0

ACT CH-3: Context Free Languages 62


Con’t...
The PDA can be:
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, ε)}

ACT CH-3: Context Free Languages 63


Example 2:
• Construct PDA for the given CFG, and test whether 010 4 is acceptable
by this PDA.
1.S → 0BB
2.B → 0S | 1S | 0
• Solution:
• The PDA can be given as:
A = {(q), (0, 1), (S, B, 0, 1), δ, q, S, ?}
• The production rule δ can be:
R1: δ(q, ε, S) = {(q, 0BB)}
R2: δ(q, ε, B) = {(q, 0S) | (q, 1S) | (q, 0)}
R3: δ(q, 0, 0) = {(q, ε)}
R4: δ(q, 1, 1) = {(q, ε)}
ACT CH-3: Context Free Languages 64
Testing 0104 , i.e. 010000 against PDA:
δ(q, 010000, S) ⊢ δ(q, 010000, 0BB)
⊢ δ(q, 10000, BB) R1
⊢ δ(q, 10000,1SB) R3
⊢ δ(q, 0000, SB) R2
⊢ δ(q, 0000, 0BBB) R1
⊢ δ(q, 000, BBB) R3
⊢ δ(q, 000, 0BB) R2
⊢ δ(q, 00, BB) R3
⊢ δ(q, 00, 0B) R2
⊢ δ(q, 0, B) R3
⊢ δ(q, 0, 0) R2
⊢ δ(q, ε) R3
ACCEPT
ACT CH-3: Context Free Languages 65
D!
E N
H E
T
ACT CH-3: Context Free Languages 66

You might also like