Languages and Finite Automata
Download
Report
Transcript Languages and Finite Automata
Pushdown Automata
PDAs
Prof. Busch - LSU
1
Pushdown Automaton -- PDA
Input String
Stack
States
Prof. Busch - LSU
2
Initial Stack Symbol
stack
head
Stack
Stack
$
z
top
bottom special symbol
Appears at time 0
Prof. Busch - LSU
3
The States
Input
symbol
Pop
symbol
Push
symbol
a,
b
c
q1
q2
Prof. Busch - LSU
4
q1
a, b c
q2
input
a
a
stack
b
h
e
$
top
Replace
Prof. Busch - LSU
c
h
e
$
5
q1
a, c
q2
input
a
stack
b
h
e
$
top
Push
Prof. Busch - LSU
a
c
b
h
e
$
6
q1
a, b
q2
input
a
a
stack
b
h
e
$
top
Pop
Prof. Busch - LSU
h
e
$
7
q1
a,
q2
input
a
a
stack
b
h
e
$
top
No Change
Prof. Busch - LSU
b
h
e
$
8
Pop from Empty Stack
q1
input
a
a, b c
q2
Pop
stack
Automaton halts!
top
If the automaton attempts to pop from
empty stack then it halts and rejects input
Prof. Busch - LSU
9
Non-Determinism
PDAs are non-deterministic
Allowed non-deterministic transitions
a, b c
q2
q1
a, b c
q1
, b c
q2
transition
q3
Prof. Busch - LSU
10
Example PDA
PDA
M:
L(M ) {a b : n 0}
n n
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
11
L(M ) {a b : n 0}
n n
Basic Idea:
1. Push the a’s
on the stack
2. Match the b’s on input
with a’s on stack
a, a
b, a
3. Match
found
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
12
Execution Example: Time 0
Input
a a a b b b
$
Stack
current
state
a, a
b, a
, q b, a q , $ $ q
q0
3
2
1
Prof. Busch - LSU
13
Time 1
Input
a a a b b b
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
14
Time 2
Input
a a a b b b
a
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
15
Time 3
Input
a
a
$
a a a b b b
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
16
Time 4
Input
a a a b b b
a
a
a
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
17
Time 5
Input
a a a b b b
a
a
a
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
18
Time 6
Input
a
a
$
a a a b b b
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
19
Time 7
Input
a
$
a a a b b b
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
20
Time 8
Input
a a a b b b
$
Stack
a, a
b, a
accept
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
21
A string is accepted if there is
a computation such that:
All the input is consumed
AND
The last state is an accepting state
we do not care about the stack contents
at the end of the accepting computation
Prof. Busch - LSU
22
Rejection Example:
Time 0
Input
a a b
$
Stack
current
state
a, a
b, a
, q b, a q , $ $ q
q0
3
2
1
Prof. Busch - LSU
23
Rejection Example:
Time 1
Input
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Prof. Busch - LSU
24
Rejection Example:
Time 2
Input
a
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Prof. Busch - LSU
25
Rejection Example:
Time 3
Input
a
a
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Prof. Busch - LSU
26
Rejection Example:
Time 4
Input
a
a
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Prof. Busch - LSU
27
Rejection Example:
Time 4
Input
a
a
a a b
$
reject
current
state
a, a
Stack
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Prof. Busch - LSU
28
There is no accepting computation for
The string
aab
aab
is rejected by the PDA
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
29
Another PDA example
PDA
M:
a, a
b, b
q0
L(M ) {vv : v {a, b} }
R
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
q2
30
Basic Idea:
1. Push v
on stack
a, a
b, b
q0
L(M ) {vv : v {a, b} }
R
3. Match v R on input
with v on stack
2. Guess
middle
of input
,
a, a
b, b
q1
Prof. Busch - LSU
4. Match
found
, $ $
q2
31
Execution Example:
Time 0
Input
a b b a
$
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
32
Time 1
Input
a b b a
a, a
b, b
q0
,
a
$
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
33
Time 2
Input
b
a
$
a b b a
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
34
Time 3
Input
a b b a
Guess the middle
of string
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
b
a
$
Stack
q2
35
Time 4
Input
b
a
$
a b b a
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
36
Time 5
Input
a b b a
a, a
b, b
q0
,
a
$
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
37
Time 6
Input
a b b a
$
a, a
b, b
a, a
b, b
Stack
accept
q0
,
q1
Prof. Busch - LSU
, $ $
q2
38
Rejection Example:
Time 0
Input
a b b b
$
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
39
Time 1
Input
a b b b
a, a
b, b
q0
,
a
$
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
40
Time 2
Input
b
a
$
a b b b
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
41
Time 3
Input
a b b b
Guess the middle
of string
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
b
a
$
Stack
q2
42
Time 4
Input
b
a
$
a b b b
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
43
Time 5
There is no possible transition.
Input
a b b b
a, a
b, b
q0
,
Input is not
consumed
a, a
b, b
q1
Prof. Busch - LSU
, $ $
a
$
Stack
q2
44
Another computation on same string:
Input
Time 0
a b b b
$
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
45
Time 1
Input
a b b b
a, a
b, b
q0
,
a
$
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
46
Time 2
Input
b
a
$
a b b b
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
47
Time 3
b
b
a
$
Input
a b b b
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
48
Time 4
b
b
b
a
$
Input
a b b b
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
Stack
q2
49
Time 5
Input
a b b b
a, a
b, b
q0
,
No accept state
is reached
a, a
b, b
q1
Prof. Busch - LSU
, $ $
b
b
b
a
$
Stack
q2
50
There is no computation
that accepts string abbb
abbb L(M )
a, a
b, b
q0
,
a, a
b, b
q1
Prof. Busch - LSU
, $ $
q2
51
Pushing & Popping Strings
Pop
string
Input
symbol
q1
a , w1 w 2
Prof. Busch - LSU
Push
string
q2
52
Example:
q1
a , eb cdf
q2
input
a
a
stack
pop
string
e
b
h
e
$
top
top
Replace
Prof. Busch - LSU
c
d
f
h
e
$
push
string
53
q1
a , eb cdf
Equivalent
transitions
pop
q1
a, e
a, b
,
a, f
q2
a, d
Prof. Busch - LSU
push
a, c q
2
54
Another PDA example
L(M ) {w {a, b} : na (w) nb (w)}
*
PDA
a, $ 0$
a, 0 00
a, 1
M
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
q2
55
Execution Example:
Time 0
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
current
state
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
$
Stack
q2
56
Time 1
Input
a b
b b
a, $ 0$
a, 0 00
a, 1
a a
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
0
$
Stack
q2
57
Time 3
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
0
$
Stack
q2
58
Time 4
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
1
$
Stack
q2
59
Time 5
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
1
1
$
Stack
q2
60
Time 6
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
1
1
$
Stack
q2
61
Time 7
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
1
$
Stack
q2
62
Time 8
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Prof. Busch - LSU
$
Stack
accept
q2
63
Formalities for PDAs
Prof. Busch - LSU
64
q1
a , w1 w 2
q2
Transition function:
(q1, a ,w1 ) {(q2,w2 )}
Prof. Busch - LSU
65
a , w1 w2
q2
q1
a , w1 w3
q3
Transition function:
(q1, a ,w1 ) {(q2,w2 ), (q3,w3 )}
Prof. Busch - LSU
66
Formal Definition
Pushdown Automaton (PDA)
M (Q, Σ, Γ, δ, q0 , z, F )
Accept
states
Input
alphabet
Transition Initial
Stack
function
state
alphabet
Stack
start
symbol
States
Prof. Busch - LSU
67
Instantaneous Description
( q, u , s )
Current
state
Remaining
input
Prof. Busch - LSU
Current
stack
contents
68
Example:
Instantaneous Description
(q1, bbb, aaa$)
Time 4:
Input
a a a b b b
a, a
b, a
a
a
a
$
Stack
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
69
Example:
Instantaneous Description
(q2 , bb, aa$)
Time 5:
Input
a a a b b b
a, a
b, a
a
a
a
$
Stack
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
70
We write:
(q1, bbb, aaa$) (q2 , bb, aa$)
Time 4
Time 5
Prof. Busch - LSU
71
A computation:
(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
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
72
(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 , ,$)
Prof. Busch - LSU
73
Language of PDA
Language
L(M )
accepted by PDA M :
L(M ) {w : (q0 ,w , z ) (qf , , s )}
Initial state
Prof. Busch - LSU
Accept state
74
Example:
(q0 , aaabbb,$) (q3 , ,$)
aaabbb L(M )
PDA M :
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
75
(q0 , a b ,$) (q3 , ,$)
n n
a b L(M )
n n
PDA M :
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
76
Therefore:
L( M ) {a b : n 0}
n n
PDA M :
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Prof. Busch - LSU
77