Chapter 7: Pushdown Automata
Quan Thanh Tho
2
Pushdown Automata
•
There are context-free languages that are not regular.
•
Finite automata cannot recognize all context-free languages.
3
Example 7.1
•
{a, b}* is regular.
•
{akbk | k is a constant} is regular.
•
{anbn | n ≥ 0} is not regular.
4
Pushdown Automata
Control unit
q
0
Input file
yes/no
Stack
5
Non-deterministic Pushdown
Automata (NPDA)
M = (Q, ∑, Γ, δ, q
0
, z, F)
Q: finite set of internal states
∑: finite set of symbols - input alphabet
Γ: finite set of symbols - stack alphabet
δ: Q × (∑∪{λ}) × Γ → finite subsets of Q × Γ* transition function
q
0
∈ Q: initial state
z ∈ Γ: stack start symbol
F ⊆ Q: set of final states
6
Transition Function
δ: Q × (∑∪{λ}) × Γ → finite subsets of Q × Γ*
stack top stack top
replacement
7
Example 7.2
δ(q
1
, a, b) = {(q
2
, cd), (q
3
, λ)}
b
d
c
q
1
q
2
q
3
8
Example 7.3
M = (Q, ∑, Γ, δ, q
0
, z, F)
Q = {q
0
, q
1
, q
2
, q
3
} δ(q
0
, a, 0) = {(q
1
, 10), (q
3
, λ)}
∑ = {a, b} δ(q
0
, λ, 0) = {(q
3
, λ)}
Γ = {0, 1} δ(q
1
, a, 1) = {(q
1
, 11)}
z = 0 δ(q
1
, b, 1) = {(q
2
, λ)}
F = {q
3
} δ(q
2
, b, 1) = {(q
2
, λ)}
δ(q
2
, λ, 0) = {(q
3
, λ)}
9
Instantaneous Description
(q, w, u)
current state unread part of stack contents
input string
10
Instantaneous Description Move
move: (q
1
, aw, bx) | (q
2
, w, yx)
iff (q
2
, y) ∈ δ(q
1
, a, b)
11
Instantaneous Description Transition
(q
1
, x, y) |∗
Μ
(q
2
, u, v)
(q
1
, x, y) |+
Μ
(q
2
, u, v)
12
Language accepted by NPDA
Let M = (Q, ∑, Γ, δ, q
0
, z, F) be an NPDA.
L(M) = {w ∈ Σ* | (q
0
, w, z) |∗
Μ
(q
f
, λ, u), q
f
∈ F, u ∈ Γ*}
13
Example 7.4
L = {
w ∈ {a, b}* | n
a
(w) = n
b
(w)
}
M = (Q, ∑, Γ, δ, q
0
, z, F)
Q = {q
0
, q
f
} δ(q
0
, λ, z) = {(q
f
, z)}
∑ = {a, b} δ(q
0
, a, z) = {(q
0
, 0z)}
Γ = {0, 1, z} δ(q
0
, b, z) = {(q
0
, 1z)}
F = {q
f
} δ(q
0
, a, 0) = {(q
0
, 00)}
δ(q
0
, b, 0) = {(q
0
, λ)}
δ(q
0
, a, 1) = {(q
0
, λ)}
δ(q
0
, b, 1) = {(q
0
, 11)}
14
Example 7.5
L = {
wwR | w ∈ {a, b}+
}
M = (Q, ∑, Γ, δ, q
0
, z, F)
Q = {q
0
, q
1
, q
2
} ∑ = {a, b} Γ = {a, b, z} F = {q
2
}
δ(q
0
, a, a) = {(q
0
, aa)} δ(q
0
, λ, a) = {(q
1
, a)}
δ(q
0
, b, a) = {(q
0
, ba)} δ(q
0
, λ, b) = {(q
1
, b)}
δ(q
0
, a, b) = {(q
0
, ab)}
δ(q
0
, b, b) = {(q
0
, bb)} δ(q
1
, a, a) = {(q
1
, λ)}
δ(q
0
, a, z) = {(q
0
, az)} δ(q
1
, b, b) = {(q
1
, λ)}
δ(q
0
, b, z) = {(q
0
, bz)} δ(q
1
, λ, z) = {(q
2
, z)}
15
NPDA and Context-Free Languages
Greibach NF
δ(q
0
, λ, z) = {(q
1
, Sz)}
S → aSA | a δ(q
1
, a, S) = {(q
1
, SA), (q
1
, λ)}
A → bB δ(q
1
, b, A) = {(q
1
, B)}
B → b δ(q
1
, b, B) = {(q
1
, λ)}
δ(q
1
, λ, z) = {(q
2
, λ)}
16
Theorem 7.1
For any context-free language L not containing
λ
, there exists
an NPDA M such that L = L(M).
17
Theorem 7.1
Proof:
G = (V, T, S, P)
M = ({q
0
, q
1
, q
f
}, T, V∪{z}, δ, q
0
, z, {q
f
}) z ∉ V
δ(q
0
, λ, z) = {(q
1
, Sz)}
(q
1
, u) ∈ δ(q
1
, a, A) iff A → au ∈ P
δ(q
1
, λ, z) = {(q
f
, z)}
18
Example 7.6
Greibach NF
δ(q
0
, λ, z) = {(q
1
, Sz)}
S → aA δ(q
1
, a, S) = {(q
1
, A)}
A → aABC | bB | a δ(q
1
, a, A) = {(q
1
, ABC), (q
1
, λ)}
δ(q
1
, b, A) = {(q
1
, B)}
B → b δ(q
1
, b, B) = {(q
1
, λ)}
C → c δ(q
1
, c, C) = {(q
1
, λ)}
δ(q
1
, λ, z) = {(q
2
, λ)}
19
Context-Free Grammars for NPDA
M = (Q, ∑, Γ, δ, q
0
, z, F)
G = (V, T, S, P)
L(G) = L(M)
20
Context-Free Grammars for NPDA
M = (Q, ∑, Γ, δ, q
0
, z, F)
M^: Single final state
Final state entered iff the stack is empty
δ(q
i
, a, A) = {c
1
, c
2
, , c
n
}
c
i
= (q
j
, λ) c
i
= (q
j
, BC)
21
Context-Free Grammars for NPDA
δ(q
i
, a, A) = {(q
j
, λ), }
At q
i
erase A and move to q
j
if receiving a
(q
i
Aq
j
) → a
22
Context-Free Grammars for NPDA
δ(q
i
, a, A) = {(q
j
, BC), }
?
At q
i
erase A and move to q
k
if receiving a and
at q
j
erase BC and move to q
k
At q
i
erase A and move to q
k
if receiving a and
at q
j
erase B and move to q
m
and
at q
m
erase C and move to q
k
23
Context-Free Grammars for NPDA
δ(q
i
, a, A) = {(q
j
, BC), }
At q
i
erase A and move to q
k
if receiving a and
at q
j
erase B and move to q
m
and
at q
m
erase C and move to q
k
(q
i
Aq
k
) → a(q
j
Bq
m
)(q
m
Cq
k
)
24
Context-Free Grammars for NPDA
Start symbol: (q
0
zq
f
)
25
Example 7.7
M = (Q, ∑, Γ, δ, q
0
, z, F)
Q = {q
0
, q
1
, q
2
} ∑ = {a} Γ = {A, z} F = {q
2
}
δ(q
0
, a, z) = {(q
0
, Az)}
δ(q
0
, a, A) = {(q
0
, A)}
δ(q
0
, b, A) = {(q
1
, λ)}
δ(q
1
, λ, z) = {(q
2
, λ)}