0% found this document useful (0 votes)
9 views107 pages

Chapter 5 PDA

The document discusses push-down automata (PDAs). PDAs are similar to finite automata but have an additional stack memory. This allows PDAs to accept context-free languages, while finite automata can only accept regular languages. The key aspects of PDAs are: - They have states, input symbols, a stack, transitions between states based on input and top of stack - Transitions may involve pushing or popping symbols from the stack - A string is accepted if processing the input leads to an accepting state with an empty stack Examples of PDAs are given to accept specific languages involving matching parentheses or symbols.

Uploaded by

ucen staff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views107 pages

Chapter 5 PDA

The document discusses push-down automata (PDAs). PDAs are similar to finite automata but have an additional stack memory. This allows PDAs to accept context-free languages, while finite automata can only accept regular languages. The key aspects of PDAs are: - They have states, input symbols, a stack, transitions between states based on input and top of stack - Transitions may involve pushing or popping symbols from the stack - A string is accepted if processing the input leads to an accepting state with an empty stack Examples of PDAs are given to accept specific languages involving matching parentheses or symbols.

Uploaded by

ucen staff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 107

Chapter 5

Push-down Automata

By
Dr Zalmiyah Zakaria
Push-Down Automata
• Regular expressions are string generators – they
tell us how to generate all strings in a language L.
• Finite Automata (DFA, NFA) are string acceptors –
they tell us if a specific string w is in L.
• CFGs are string generators
• Are there string acceptors for Context-Free
languages?
• YES! Push-down automata

Sept2011 Theory of Computer Science 2


Push-Down Automata
• DFAs accept regular languages.
• Now, we want to design machines similar to DFAs
that will accept context-free languages.
• These machines will need to be more powerful.
• To handle a language like {anbn | n  0}, the
machine needs to “remember” the number of a’s.
• To do this, we use a stack.
• A push-down automaton (PDA) is essentially an
NFA with a stack.
Push-Down Automata
• A PDA has some memory, but not a
general purpose, random-access
memory. It uses a stack, which has
two operations Push and Pop.
• Using the stack, it allows us to:
– Count
– Match strings

Sept2011 Theory of Computer Science 4


Building a PDA
state control
L = {0n1n: n ≥ 1}
push $
Remember each 0
read 0 by pushing x onto the stack
push x
read 1 When we see a 1, we
pop x pop an x from the stack
read 1
pop x
We want to accept when
pop $ we hit the stack bottom

$ = special marker for bottom


A PDA in action
state control
L = {0n1n: n ≥ 1}
push $ input
read 0
push x 0 0 0 1 1 1
read 1
pop x $ x x x …
read 1
pop x stack
pop $
Notation for PDAs
q0
,  / $
push $
read 0
q1 0,  / x
push x
1, x / 
read 1
pop x
read 1
q2 1, x / 
pop x , $ / 
pop $ q3

read, pop / push


Definition of a PDA
A pushdown automaton (PDA) is a sextuple
(Q, , , , q0, F) where:
– Q is a finite set of states;
–  is the input alphabet;
–  is the stack alphabet
– q0 in Q is the start state;
– F  Q is a set of final states;
–  is the transition function
: Q  (  {})  (  {}) → subsets of Q  (  {})
– PDA accepts w if we end up in a final state
with an empty stack
Example
q0
,  / $  = {0, 1}
q1  = {$, x}
0,  / x
(q0, , ) = {(q1, $)}
1, x / 
(q0, , $) = ∅
q2
1, x /  (q0, , x) = ∅
, $ /  (q0, 0, ) = ∅
q3
...

: Q  (  {})  (  {}) → subsets of Q  (  {})


state input symbol pop symbol state push symbol
The language of a PDA
• A PDA is nondeterministic
Multiple transitions on same pop/input
allowed
• Transitions may but do not have to push
or pop

The language of a PDA is the set of all


strings in S* that can lead the PDA to
an accepting state
Transitions
• Let ((p, a, β), (q, γ))  Δ be a transition.
• It means that we
– Move from state p.
– Read a from the tape,
– Pop the string β from the stack,
– Move to state q,
– Push string γ onto the stack.
• The first three (p, a, β), are input.
• The last two (q, γ) are output.
Transitions
• We will draw it as

or

If at q0 (p), with next input symbol a ()


and top of stack x (), then can
consume a (), pop x (), push y ()
onto stack and move to q1 (q)
Pushing and Popping
• When we push β, we push the symbols
of β as we read them right to left.
– When we push the string abc, we push c,
then push b, then push a.
• When we pop γ, we pop the symbols of
γ as we read them from left to right
(reverse order).
– When we pop the string abc, we pop a,
then pop b, then pop c.
Pushing and Popping
• Thus, if we push the string abc and
then pop it, we will get back abc, not
cba.
• If we wanted to reverse the order, we
would use three separate transitions:
– Push a
– Push b
– Push c
Configurations
• A configuration fully describes the
current state of the PDA.
–The current state p.
–The remaining input w.
–The current stack contents .
• Thus, a configuration is a triple
(p, w, )  (K, *, *).
Computations
• A configuration (p, w, ) yields a
configuration (p', w', ') in one step,
denoted
(p, w, ) ├ (p', w', '),
if there is a transition ((p, a, ), (p', ))
 Δ such that w = aw',  = , and ' =
 for some   *.
• The reflexive, transitive closure of ├ is
denoted ├ *.
Accepting Strings
• After processing the string on the tape,
– The PDA is in either a final or a nonfinal state,
and
– The stack is either empty or not empty.
• The input string is accepted if
– The ending state is a final state, and
– The stack is empty.
• That is, the string w  * is accepted if
(s, w, e) ├* (f, e, e)
for some f  F.
Accepting Strings
• One may define acceptance “by final
state” only.
– The input is accepted if and only if the last
state is a final state, regardless of whether
the stack is empty.
• One may define acceptance “by empty
stack” only.
– The input is accepted if and only if the stack
is empty once the input is processed,
regardless of which state the PDA is in.
Example of a PDA
• Run the following PDA on the input
string aaabbb.
Example of a PDA
• The steps in the processing are
–(s, aaabbb, e) ├ (p, aabbb, A)
├ (p, abbb, AA)
├ (p, bbb, AAA)
├ (p, bb, AA)
├ (p, b, A)
├ (q, e, e).
The Language of a PDA
• The language of a PDA A is
L(A) = {w  *A accepts w}.
• What is the language of the PDA in
the previous example?
Example of a PDA
• What is the language of the following
PDA?
Examples of PDAs

• Let  = {a, b}.


• Design a PDA that accepts the
language {wcw | w   }.
R *

• Design a PDA that accepts the


language {ww | w   }.
R *
Examples of PDAs

• Design a PDA whose language is


{ambn  0  m < n}.
• Design a PDA whose language is
{ambn  0  n < m}.
Examples of PDAs
• Design a PDA whose language is
{ambncndm | m  0, n  0}.
• Design a PDA whose language is
{ambmcndn | m  0, n  0}.
• Design a PDA whose language is
{ambncpdq | m + n = p + q}.
• Design a PDA whose language is
{ambnck | m = n or m = k}.
Pushdown Automata
PDAs

27
Pushdown Automaton -- PDA
Input String

Stack

States

28
Initial Stack Symbol

Stack Stack

stack
$ z top
head

bottom special symbol


Appears at time 0
29
The States
Input Pop Push
symbol symbol symbol

30
a, b  c
q1 q2

input
 a   a 

stack
b top c
h Replace h
e e
$ $ 31
a,   c
q1 q2

input
 a   a 

stack c
b top b
h Push h
e e
$ $ 32
a, b  
q1 q2

input
 a   a 

stack
b top
h Pop h
e e
$ $ 33
a,   
q1 q2

input
 a   a 

stack
b top b
h No Change h
e e
$ $ 34
Empty Stack

a, $  
q1 q2
input
 a   a 

stack empty
$ top Pop

The automaton HALTS


No possible transition after q2
35
A Possible Transition

a, $  b
q1 q2
input
 a   a 

stack
$ top Pop b

36
Non-Determinism
PDAs are non-deterministic
Allowed non-deterministic transitions
q2
a, b  c
, b  c
q1 q1 q2

a, b  c -transition
q3
37
Example PDA

PDA M,
L(M) = {anbn : n  0}

a,   a b, a  

q0 ,    q b, a   q , $  $ q
1 2 3
38
Basic Idea: L(M) = {anbn : n  0}

1. Push the a’s 2. Match the b’s on input


on the stack with a’s on stack

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

q0 ,    q b, a   q , $  $ q
1 2 3
39
Execution Example: Time 0
Input
a a a b b b $
Stack

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

q0  ,    q b, a   q , $  $ q
1 2 3
40
Time 1
Input
a a a b b b
$
Stack

a,   a b, a  

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

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
42
Time 3
Input a
a a a b b b a
$
Stack

a,   a b, a  

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

a,   a b, a  

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

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
45
Time 6
Input a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
46
Time 7
Input
a a a b b b a
$
Stack

a,   a b, a  

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

a,   a b, a  
accept

q0  ,    q1 b, a   q2  , $  $ q3
48
A string is accepted if there is
a computation such that:

All the input is consumed


AND
The last state is an accepting state

At the end of the computation,


we do not care about the stack contents
(the stack can be empty at the last state)
49
The input string aaabbb
is accepted by the PDA:

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
50
In general, L = {anbn : n  0}
is the language accepted by the PDA:

a,   a b, a  

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

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

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

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

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

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

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

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

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

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

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

q0  ,    q1 b, a   q2  , $  $ q3
57
The input string aab
is rejected by the PDA:

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
58
A string is rejected if there is
no computation such that:

All the input is consumed


AND
The last state is an accept state

At the end of the computation,


we do not care about the stack contents
59
Another PDA example

L (M) = {vvR : v  {a,b}*}


PDA M
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
60
Basic Idea: L (M) = {vvR : v  {a,b}*}

1. Push v 3. Match vR on input


2. Guess
on stack with v on stack
middle
of input
a,   a a, a   4. Match
b,   b b, b   found

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

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

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

q0 ,    q1 , $  $ q2
64
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
65
Time 4
Input
b
a b b a a
$
Stack
a,   a a, a  
b,   b b, b  

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

q0 ,    , $  $ q2
q1 67
Time 6
Input
a b b a
$
Stack
a,   a a, a  
b,   b b, b  
Accept

q0 ,    q1 , $  $ q2
68
Rejection Example: Time 0
Input
a b b b
$
Stack
a,   a a, a  
b,   b b, b  

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

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

q0 ,    q1 , $  $ q2
71
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
72
Time 4
Input
b
a b b b a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
73
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 ,    , $  $ q2
q1 74
Another computation on same string: Time 0
Input
a b b b
$
Stack
a,   a a, a  
b,   b b, b  

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

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

q0 ,    q1 , $  $ q2
77
Time 3
Input b
b
a b b b a
$
Stack
a,   a a, a  
b,   b b, b  

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

q0 ,    q1 , $  $ q2
79
Time 5 b
Input b
No final state b
a b b b is reached a
$
Stack
a,   a a, a  
b,   b b, b  

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

abbb  L(M)

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

q0 ,    q1 , $  $ q2
81
Another PDA example
L (M) = {w  {a,b}*:
in every prefix v, na(v)  nb(v)}
a,   a
b, a  
PDA M

q0
82
Execution Example: Time 0
Input
a a b

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

q0
83
Time 1
Input
a a b a
a,   a $
b, a   Stack
b, $  

q0
84
Time 2
Input
a
a a b a
a,   a $
b, a   Stack
b, $  

q0
85
Time 3
Input
a a b a
a,   a $
b, a   Stack
b, $  

Accept
q0
86
Rejection Example: Time 0
Input
a b b b

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

q0
87
Time 1
Input
a b b b a
a,   a $
b, a   Stack
b, $  

q0
88
Time 2
Input
a b b b

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

q0
89
Time 3
Input
a b b b

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

q0
90
Time 4
Input
a b b b

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

Halt and Reject


q0
91
Pushing Strings
Input Pop Push
symbol symbol string

q1
a, b  w q2

92
Example:
a, b  cdf
q1 q2

input
a a

c pushed
stack d
top string
b f
h Push h
e e
$ $ 93
Another PDA example
L (M) = {w  {a,b}*: na(w) = nb(w)}
PDA M
a, $  0$ b, $  1$
a, 0  00 b, 1 11
a, 1   b, 0  

q1
, $  $ q2
94
Execution Example: Time 0
Input
a b b b a a

$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  
current
state
q1
, $  $ q2
95
Time 1
Input
a b b b a a
0
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  

q1
, $  $ q2
96
Time 3
Input
a b b b a a
0
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  

q1
, $  $ q2
97
Time 4
Input
a b b b a a
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  

q1
, $  $ q2
98
Time 5
Input
a b b b a a 1
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  

q1
, $  $ q2
99
Time 6
Input
a b b b a a 1
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  

q1
, $  $ q2
100
Time 7
Input
a b b b a a
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  

q1
, $  $ q2
101
Time 8
Input
a b b b a a

$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1 11
a, 1   b, 0  
Accept

q1
, $  $ q2
102
Formalities for PDAs

103
q1
a, b  w q2

Transition function:

 (q1, a, b)  {(q2 , w)}

104
q2
a, b  w

q1

a, b  w q3

Transition function:

 (q1, a, b)  {(q2 , w), (q3 , w)}


105
Formal Definition
Pushdown Automaton (PDA)

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

Input Stack
alphabet Transition start
Initial
Stack
function state symbol
alphabet
106
Instantaneous Description

( q, u , s )

Current Current
Remaining
state stack
input
contents

107
Example: Instantaneous Description
(q1, bbb, aaa$)
a
Time 4: Input a
a a a b b b a
$

a,   a b, a   Stack

q0  ,    q1 b, a   q2  , $  $ q3
108

You might also like