The following pseudocode is from Introduction to Compiler Construction in a Java World book. This algorithm is supposed to output the epsilon closure of a set of a states of a nondeterministic finite machine (to convert it to a deterministic one eventually).
# Input: a set of states, S
# Output: epsilon_closure(S)
Stack P.addAll(S) #a stack containing all states in S
Set C.addAll(S) #the closure initially contains the states in S
while ! P.empty() do
s = P.pop()
for r in m(s, epsilon) do
# m(s, epsilon) is a set of states
if r not in C then
P.push(r)
C.add(r)
end if
end for
end while
return C
I know what epsilon closure is but unfortunately I'm having a hard time to understand how this code works.
Note: m()
in the code is the transition function of the machine.