
Lexical Analysis 49
Algorithm 2.3 NFA to DFA Construction
Input: an NFA, N = (Σ, S, s
0
, M, F )
Output: DFA, D = (Σ, S
D
, s
D0
, M
D
, F
D
)
Set S
D0
← -closure(s
0
)
Set S
D
.add(S
D0
)
Moves M
D
Stack stk.push(S
D0
)
i ← 0
while ! stk.empty() do
t ← stk.pop()
for a in Σ do
S
Di+1
← -closure(m(t, a));
if S
Di+1
6= {} then
if S
Di+1
∈ S
D
then
// We have a new state
S
D
.add(S
Di+1
)
stk.push(S
Di+1
)
i ← i + 1
M
D
.add(M
D
(t, a) = i)
else if ∃j, S
j
∈ S
D
∧ S
Di+1
= S
j
then
// In the case that the state already exists
M
D
.add(M
D
(t, a) = j)
end if
end if
end for
end while
Set F
D
for s
D
in S
D
do
for s in s
D
do
if s ∈ F then
F
D
.add(s
D
)
end if
end for
end for
return D = (Σ, S
D
, s
D0
, M
D
, F
D
)
FIGURE 2.17 An initial partition ...