当前位置: 首页>>代码示例>>C++>>正文


C++ NFA::get_rechable_set方法代码示例

本文整理汇总了C++中NFA::get_rechable_set方法的典型用法代码示例。如果您正苦于以下问题:C++ NFA::get_rechable_set方法的具体用法?C++ NFA::get_rechable_set怎么用?C++ NFA::get_rechable_set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NFA的用法示例。


在下文中一共展示了NFA::get_rechable_set方法的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()
开发者ID:,项目名称:,代码行数:71,代码来源:


注:本文中的NFA::get_rechable_set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。