本文整理汇总了C++中NFA::get_num_states方法的典型用法代码示例。如果您正苦于以下问题:C++ NFA::get_num_states方法的具体用法?C++ NFA::get_num_states怎么用?C++ NFA::get_num_states使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NFA
的用法示例。
在下文中一共展示了NFA::get_num_states方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: nfa_to_dfa
//==========================================================================================================
// 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()