예제 #1
0
        private void ConvertNFAToDFA()
        {
            var nfaStates = m_nfa.States;

            //state 0 is an empty state. All invalid inputs go to state 0
            DFAState state0 = new DFAState();

            AddDFAState(state0);

            //state 1 is closure(nfaState[0])
            DFAState preState1     = new DFAState();
            int      nfaStartIndex = m_nfa.EntryEdge.TargetState.Index;

            Debug.Assert(nfaStartIndex >= 0);

            preState1.NFAStateSet.Add(nfaStartIndex);

            DFAState state1 = GetClosure(preState1);

            AddDFAState(state1);

            //begin algorithm
            int p = 1, j = 0;
            var newStates = new DFAState[CompactCharSetManager.MaxClassIndex + 1];

            while (j <= p)
            {
                var sourceState = m_dfaStates[j];

                Parallel.For(CompactCharSetManager.MinClassIndex, CompactCharSetManager.MaxClassIndex + 1,
                             symbol =>
                {
                    DFAState e = GetDFAState(sourceState, symbol);

                    newStates[symbol] = e;
                }
                             );

                for (int symbol = CompactCharSetManager.MinClassIndex; symbol <= CompactCharSetManager.MaxClassIndex; symbol++)
                {
                    DFAState e = newStates[symbol];

                    bool isSetExist = false;
                    for (int i = 0; i <= p; i++)
                    {
                        if (e.NFAStateSet.SetEquals(m_dfaStates[i].NFAStateSet))
                        {
                            //an existing dfa state

                            DFAEdge newEdge = new DFAEdge(symbol, m_dfaStates[i]);
                            sourceState.AddEdge(newEdge);

                            isSetExist = true;
                        }
                    }

                    if (!isSetExist)
                    {
                        //a new set of nfa states (a new dfa state)
                        p += 1;
                        AddDFAState(e);

                        DFAEdge newEdge = new DFAEdge(symbol, e);
                        sourceState.AddEdge(newEdge);
                    }
                }
                j += 1;
            }
        }
예제 #2
0
        internal void AddEdge(DFAEdge edge)
        {
            CodeContract.RequiresArgumentNotNull(edge, "edge");

            m_outEdges.Add(edge);
        }