You are on page 1of 26

Formal Languages & Finite Theory of

Automata
BS Course

Slide # : 06

Muhammad Faizan Tahir


Pushdown Automaton
(PDA)
A Pushdown Automaton is a nondeterministic finite
state automaton (NFA) that permits ε-
transitions and a stack.

2
Pushdown Automaton (PDA)
P   Q, , ,  ( qi , a, m )   ( qk , n ),..., q0 ,  0 , F 
Q: A finite set of states.
S : A finite set of input symbols.
G: A finite stack alphabet.
d: The transition function with input:
qi is a state in Q.
a is a symbol in S or a = e (the empty string).
tm is a stack symbol, tm Î G.
and the output is a finite set of pairs:
qk the new state.
tn is the string of stack symbols that replaces tm at the top of the stack. If
tn = e, then the stack is popped.
q0: The start state.
t0 : Initially, the PDA’s stack consists this symbol and nothing else.
F : The set of accepting states. 3
PDA Example: Lwwr  {wwR | w  (0  1)*}
The language, Lwwr, is the even-length palindromes over alphabet
{0,1}.

Lwwr is a Context-Free Language (CFL) generated by the grammar:

S  0S 0 | 1S1 | 

One PDA for Lwwr is given on the following


slide...
4
P   Q , , ,  , q0 ,  0 , F 
PDA for Lwwr
Q  {q0 , q1 , q2 , q3}   {0,1}   {0,1, 0 } F  {q3}

1)  ( q0 ,0, 0 )  ( q0 ,0 0 )  ( q0 ,1, 0 )  ( q0 ,1 0 )

2)  ( q0 ,0,0)  ( q0 ,00)  ( q0 ,0,1)  ( q0 ,01)


 ( q0 ,1,0)  ( q0 ,10)  ( q0 ,1,1)  ( q0 ,11)

3)  ( q0 ,  , 0 )  ( q1 , 0 )  ( q0 ,  ,0)  ( q1 ,0)  ( q0 ,  ,1)  ( q1 ,1)

4)  ( q1 ,0,0)  ( q1 ,  )  ( q1 ,1,1)  ( q1 ,  )

5)  ( q1 ,  , 0 )  ( q2 , 0 )

6)  ( q2 , Read_Past_ End_Of_Input, 0 )  ( q3 , 0 )
5
A Graphical Notation for PDA’s
1. The nodes correspond to the states of the PDA.
2. An arrow labeled Start indicates the unique start state.
3. Doubly circled states are accepting states.
4. Edges correspond to transitions in the PDA as follows:
5. An edge labeled (ai, t m)/tn from state q to state p means
that d(q, ai, tm) contains the pair (p, tn), perhaps among
other pairs.

6
Graphical Notation
(0, 0)/00
for PDA of L wwr
(0, 1)/01 (0,0)/ε
(0, (1, 0)/10 (1,1)/ε
t0)/0t0 (1, 1)/11
(1,
start
t0)/1t0 (ε,t0) / t0
q0 q1 q2
(ε, t0) /
t0
(ε, 0) / 0
(EOF,t0) / t0
(ε, have
All possibilities that do not 1) / 1explicit
edges, have implicit edges that go to an
implicit reject state.
q3
• This is a nondeterministic machine.
• Think of the machine as following all possible paths.
• Kill a path if it leads to a reject state.
• If any path leads to an accept state, then the machine accepts.
7
Exercise 1

PDA
M
:
L( M )  {a b : n  0}
n n

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
8
L( M )  {a b : n  0}
n n

Basic Idea:

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


with a’s on stack
on the stack
same like a, ε  aa
3. Match
found

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
9
Execution Example: Time 0

Input

a a a b b b
$
Stack

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

q0  ,    q1 b, a   q2  , $  $ q3
10
Time 1

Input

a a a b b b
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
11
Time 2

Input

a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
12
Time 3

Input
a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
13
Time 4

Input
a
a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
14
Time 5

Input
a
a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
15
Time 6

Input
a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
16
Time 7

Input

a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
17
Time 8

Input

a a a b b b
$
Stack

a,   a b, a  
accept

q0  ,    q1 b, a   q2  , $  $ q3
18
Rejection Example: Time 0

Input

a a b
$
Stack

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

q0  ,    q1 b, a   q2  , $  $ q3
19
Rejection Example: Time 1

Input

a a b
$
Stack

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

q0  ,    q1 b, a   q2  , $  $ q3
20
Rejection Example: Time 2

Input

a a b a
$
Stack

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

q0  ,    q1 b, a   q2  , $  $ q3
21
Rejection Example: Time 3

Input a
a a b a
$
Stack

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

q0  ,    q1 b, a   q2  , $  $ q3
22
Rejection Example: Time 4

Input a
a a b a
$
Stack

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

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

Input a
a a b a
$
Stack

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

q0  ,    q1 b, a   q2  , $  $ q3
24
Exercise 2

Design a PDA that recognizes legal sequences of ‘if’ and ‘else’


statements in a C program.

In the PDA, let ‘i’ stands for ‘if’ and ‘e’ stands for ‘else’.

Hint: There is a problem whenever the number of ‘else’ statements in


any prefix exceeds the number of ‘if’ statements in that prefix.

25
Exercise 3
Design a PDA to accept the language:

{a b c | i  j or j  k }
i j k

26

You might also like