11std::set<const NFANode*>
epsilonClosure(
const std::set<const NFANode*>& states) {
12 std::set<const NFANode*> closure;
13 std::queue<const NFANode*> stateQueue;
14 for (
const auto& state : states) stateQueue.push(state);
15 while (!stateQueue.empty()) {
16 auto currentState = stateQueue.front();
18 closure.insert(currentState);
19 currentState->for_transitions([&](
auto c,
auto to) {
21 if (closure.find(to) == closure.end()) stateQueue.push(to);
34 auto dfa = std::make_unique<DFA>();
35 std::map<std::set<const NFANode*>,
DFANode*> dfaStates;
36 std::queue<std::set<const NFANode*>> stateQueue;
38 dfaStates.emplace(startState, dfa->add_state());
39 stateQueue.push(startState);
40 while (!stateQueue.empty()) {
41 auto currentState = stateQueue.front();
43 auto currentDfaState = dfaStates[currentState];
44 std::map<std::uint16_t, std::set<const NFANode*>> nextStates;
46 for (
auto& nfaState : currentState) {
47 nfaState->for_transitions([&](
auto c,
auto to) {
49 if (nextStates.find(c) == nextStates.end())
50 nextStates.emplace(c, std::set<const NFANode*>{to});
52 nextStates[c].insert(to);
56 for (
auto& [c, tos] : nextStates) {
58 if (dfaStates.find(toStateClosure) == dfaStates.end()) {
59 dfaStates.emplace(toStateClosure, dfa->add_state());
60 stateQueue.push(toStateClosure);
62 currentDfaState->add_transition(dfaStates[toStateClosure], c);
65 dfa->set_start(dfaStates[startState]);
66 for (
auto& [state, dfaState] : dfaStates) {
67 for (
auto& nfaState : state) {
68 if (nfaState->is_accepting()) dfaState->set_accepting(
true);
69 if (nfaState->is_erroring()) {
70 dfaState->set_accepting(
false);
71 dfaState->set_erroring(
true);