ToA - Lecture 19 20 - Push Down Automata

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 93

Lecture 19 & 20

Pushdown Automata
 Input TAPE
 The part of an FA, where the
input string is placed before it
is run, is called the input TAPE.
The input TAPE is supposed to
accommodate all possible
strings.
 The input TAPE is partitioned
with cells, so that each letter
of the input string can be
placed in each cell. The input
string abbaa is shown in the
following input TAPE
 The START state
 This state is like initial state of
an FA and is represented by
 An ACCEPT state
 This state is like a final state
of an FA and is expressed by
 A REJECT state
 This state is like dead-end non
final state and is expressed by
 A READ state
 This state is to read an input
letter and lead to some other
state. The READ state is
expressed by
 FA accepts the language of
strings, expressed by
(a+b)*a.
 Following is the new
format of the above FA
 FA accepts the language
expressed by
(a+b)*bb(a+b)*
 PUSHDOWN STACK or
PUSHDOWN STORE
PUSHDOWN STACK
 is a place where the input
letters can be placed until
these letters are referred
again. It can store as many
letters as one can in a long
column. Initially the STACK
is supposed to be empty i.e.
each of its storage location
contains a blank.
 PUSH
 A PUSH operator adds a
new letter at the top of
STACK, for e.g. if the letters
a, b, c and d are pushed to
the STACK that was initially
blank, the STACK can be
shown as
 POP and STACK POP
 is an operation that takes
out a letter from the top of
the STACK.
 The rest of the letters are
moved one location up.
 POP state is expressed as
 The string aaabbb is to be
run on this machine.
 Before the string is
processed, the string is
supposed to be placed on
the TAPE and the STACK
is supposed to be empty
as shown below
Pushdown Automaton -- PDA
Input String

Stack

States
Initial Stack Symbol

Stack Stack

stack
head top
$ z

bottom special symbol

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

q1 a, b → c q2
a, b → c
q1 q2

input

 a   a 

stack

b top c
Replace
h h
e e
$ $
a,  → c
q1 q2

input

 a   a 

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

input

 a   a 

stack

b top
Pop
h h
e e
$ $
a,  → 
q1 q2

input

 a   a 

stack

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

a, $ → 
q1 q2
input

 a   a 

stack empty

top Pop
$
The automaton HALTS
No possible transition after

q2
A Possible Transition

a, $ → b
q1 q2
input

 a   a 

stack

top Pop
$ b
Non-Determinism
PDAs are non-deterministic
Allowed non-deterministic transitions

a, b → c q2

, b → c
q1 q1 q2

a, b → c
 − transition
q3
Example PDA
PDA
M
L(M ) = {a b : n  0}
n n

a,  → a b, a → 

q0 ,  →  q b, a →  q , $ → $ q
1 2 3
L(M ) = {a b : n  0}
n n

Basic Idea:

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


on the stack with a’s on stack

3. Match
found

a,  → a b, a → 

q0 ,  →  q b, a →  q , $ → $ q
1 2 3
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
Time 1

Input

a a a b b b
$
Stack

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
Time 2

Input

a a a b b b a
$
Stack

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
Time 3

Input
a
a a a b b b a
$
Stack

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
Time 4

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

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
Time 5

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

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
Time 6

Input
a
a a a b b b a
$
Stack

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
Time 7

Input

a a a b b b a
$
Stack

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
Time 8

Input

a a a b b b
$
Stack

a,  → a b, a → 
accept

q0  ,  →  q1 b, a →  q2  , $ → $ q3
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)
The input string
is accepted by the PDA: aaabbb

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
In general,

L = {a b : n  0}
n n

is the language accepted by the PDA:

a,  → a b, a → 

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

Input

a a b
$
Stack

current
state a,  → a b, a → 

q0  ,  →  q b, a →  q , $ → $ q
1 2 3
Rejection Example: Time 1

Input

a a b
$
Stack

current
state a,  → a b, a → 

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

Input

a
a a b
$
Stack

current
state a,  → a b, a → 

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

Input a
a
a a b
$
Stack

current
state a,  → a b, a → 

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

Input a
a
a a b
$
Stack

current
state a,  → a b, a → 

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

Input a
a
a a b
$
Stack

reject
current
state a,  → a b, a → 

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

a,  → a b, a → 

q0  ,  →  q1 b, a →  q2  , $ → $ q3
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
Another PDA example

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

PDA
M

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2

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

1. Push v
on stack 2. Guess 3. Match on input
middle with v on stack
of input

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

q0 ,  →  q1 , $ → $ q2
Execution Example: Time 0

Input

a b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 1

Input

a b b a
a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 2

Input

b
a b b a
a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
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
Time 4

Input

b
a b b a
a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 5

Input

a b b a
a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  , $ → $ q2
q1
Time 6

Input

a b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 
accept

q0 ,  →  q1 , $ → $ q2
Rejection Example: Time 0

Input

a b b b
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 1

Input

a b b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 2

Input

b
a b b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
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
Time 4

Input

b
a b b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 5

Input There is no possible transition.

a b b b Input is not consumed


a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  , $ → $ q2
q1
Another computation on same string:

Input
Time 0

a b b b
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 1

Input

a b b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 2

Input

b
a b b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Time 3

Input b
b
a b b b a
$
Stack

a,  → a a, a → 
b,  → b b, b → 

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

a,  → a a, a → 
b,  → b b, b → 

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

a,  → a a, a → 
b,  → b b, b → 

q0 ,  →  q1 , $ → $ q2
Another PDA example
L( M ) = {w {a, b} :
*

in every prefix v, na (v)  nb (v)}

a,  → a
b, a → 
PDA
M
q0
Execution Example: Time 0

Input

a a b
a,  → a $
b, a →  Stack

b, $ → 

q0
Time 1

Input

a a b a
a,  → a $
b, a →  Stack

b, $ → 

q0
Time 2

Input

a
a a b a
a,  → a $
b, a →  Stack

b, $ → 

q0
Time 3

Input

a a b a
a,  → a $
b, a →  Stack

b, $ → 

accept

q0
Rejection example: Time 0

Input

a b b b
$
Stack

q0
Time 1

Input

a b b b a
a,  → a $
b, a →  Stack

b, $ → 

q0
Time 2

Input

a b b b

a,  → a $
b, a →  Stack

b, $ → 

q0
Time 3

Input

a b b b

a,  → a
b, a →  Stack

b, $ → 

q0
Time 4

Input

a b b b

a,  → a
b, a →  Stack

b, $ → 

Halt and Reject

q0
Pushing Strings
Input Pop Push
symbol symbol string

q1 a, b → w q2
Example:

a, b → cdf
q1 q2

input

a a

c pushed
stack string

top
d
b f
Push
h h
e e
$ $
Context-Free
Languages
(Grammars)
 Languages
Accepted by
PDAs

Convert any context-free grammar


to a PDA with: G
M L(G ) = L( M )
We will convert grammar
G
to a PDA M such that:

M simulates leftmost derivations of G


G to PDA M
Convert grammar

Production in G Terminal in G
A→w a

, A → w a, a → 

q0 ,  → S q1 , $ → $ q2
Grammar Example
S → aSTb
PDA
S →b
T → Ta  , S → aSTb
T → , S → b
 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Grammar
PDA computation
derivation

(q0 , abab,$)
S  (q1 , abab, S $)
 aSTb  (q1 , bab, STb$)
 (q1 , bab, bTb$)
 abTb
 (q1 , ab, Tb$)
 abTab  (q1 , ab, Tab$)
 abab  (q1 , ab, ab$)
 (q1 , b, b$)
 (q1 ,  ,$)
 (q2 ,  ,$)
Derivation:

Input
a b a b

Time 0
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

,  → S , $ → $
q0 q1 q2
Derivation:
S
Input
a b a b
S
Time 0
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb a
Input S
a b a b T
b
Time 1
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb a
Input S
a b a b T
b
Time 2
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb  abTb
Input b
a b a b T
b
Time 3
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb  abTb
Input b
a b a b T
b
Time 4
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb  abTb  abTab
Input T
a b a b a
b
Time 5
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb  abTb  abTab  abab
Input T
a b a b a
b
Time 6
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb  abTb  abTab  abab
Input
a b a b a
b
Time 7
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb  abTb  abTab  abab
Input
a b a b
b
Time 8
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 

q0 ,  → S q1 , $ → $ q2
Derivation:
S  aSTb  abTb  abTab  abab
Input
a b a b

Time 9
 , S → aSTb $
, S → b Stack

 , T → Ta a, a → 
, T →  b, b → 
accept

q0 ,  → S q1 , $ → $ q2
In general, it can be shown that:

Grammar
generates G
PDA
string
accepts M
w If and
Only if
w
* (q0 , w,$)  (q2 ,  ,$)
Sw

Therefore
L(G) = L(M )

You might also like