//========================================================================================================== // Create a DFA from an NFA using the Subset-Construction algorithm //========================================================================================================== void DFA::nfa_to_dfa(NFA& nfa) { vector<set<int>> epsilon_closures; // Epsilon closures per NFA state //------------------------------------------------------------------------------------------------------ // Calculate epsilon-closures for each state in the NFA //------------------------------------------------------------------------------------------------------ for(int i = 0; i < nfa.get_num_states(); ++i) { epsilon_closures.push_back(calc_epsilon_closure(i, nfa)); } // Map between DFA state, and a set of NFA states it was constructed from (the index in the vector is the DFA state) vector<set<int>> nfa_states; nfa_states.push_back(epsilon_closures[0]); // Initial, starting state table.push_back(vector<int>(NUM_SYMBOLS, -1)); // Adding the start state //------------------------------------------------------------------------------------------------------ // As long as there are new states in the DFA, calcuate the transitions for them, while creating new // states if needed //------------------------------------------------------------------------------------------------------ for(int i = 0; i < table.size(); ++i) { for(int sym = 0; sym < NUM_SYMBOLS; ++sym) { // For each possible symbol //---------------------------------------------------------------------------------------------- // Get the set of reachable NFA states from the current DFA state for the current symbol //---------------------------------------------------------------------------------------------- set<int> reachable_set = nfa.get_rechable_set(nfa_states[i], Symbol(sym)); for(auto& s: reachable_set) { // Add the epsilon-closure reachable_set.insert(epsilon_closures[s].begin(), epsilon_closures[s].end()); } if(reachable_set.empty()) continue; //---------------------------------------------------------------------------------------------- // Check if this set already exists as a DFA state //---------------------------------------------------------------------------------------------- int state = table.size(); for(int j = 0; j < nfa_states.size(); ++j) { if(reachable_set == nfa_states[j]) { state = j; break; } } table[i][sym] = state; // Add this transition if(state == table.size()) { // New state nfa_states.push_back(reachable_set); table.push_back(vector<int>(NUM_SYMBOLS, -1)); } } // for each symbol } // for each new state //------------------------------------------------------------------------------------------------------ // Mark a state as accepting if any of the NFA states corresponding to it are accepting //------------------------------------------------------------------------------------------------------ accepting.resize(get_num_states(), -1); for(int i = 0; i < nfa_states.size(); ++i) { for(auto s: nfa_states[i]) { if(nfa.accepting[s] >= 0) { if(accepting[i] >= 0) throw string("DFA state contain accepting NFA states with different production rules"); accepting[i] = nfa.accepting[s]; break; } } } } // nfa_to_dfa()