Exemple #1
0
        private String CollapseChain(State state)
        {
            if (state.mTransitions.Count == 1)
            {
                State childState = state.mTransitions.Values.First()[0];

                String result = CollapseChain(childState);

                if (state.mAcceptingState == false)
                {
                    if (result != null)
                    {
                        mDFATable.GetStates().Remove(childState);
                        state.mChain = state.mTransitions.Keys.First() + result;

                        state.mChainTokenID = childState.mTokenID;

                        if (childState.mChainDebugTokenName != null && childState.mChainDebugTokenName != String.Empty)
                        {
                            state.mChainDebugTokenName = childState.mChainDebugTokenName;
                        }
                        else
                        {
                            state.mChainDebugTokenName = childState.mDebugTokenName;
                        }
                        //state.mAcceptingState = childState.mAcceptingState;

                        state.mTransitions.Clear();
                        return(state.mChain);
                    }
                }
            }
            else if (state.mTransitions.Count == 0)
            {
                return(String.Empty);
            }
            else
            {
                foreach (List <State> states in state.mTransitions.Values)
                {
                    CollapseChain(states[0]);
                }
            }

            return(null);
        }
Exemple #2
0
        public void SaveGraph(FSATable table, String name)
        {
            StringBuilder strGraph = new StringBuilder("digraph{\n");
            List <State>  faStates = table.GetStates();

            for (int i = 0; i < faStates.Count; ++i)
            {
                State  state      = faStates[i];
                String strStateID = state.mStateID.ToString();
                strGraph.Append("\t" + strStateID + "\t[");

                if (state.mAcceptingState)
                {
                    strGraph.Append("shape=doubleoctagon, ");
                }

                String label = null;

                if (state.mChain != null)
                {
                    strGraph.Append("color=blue, ");
                    label = state.mChain;
                }

                if (state.mTokenID != -1)
                {
                    if (label == null)
                    {
                        label = "(" + state.mDebugTokenName + ")";
                    }
                    else
                    {
                        label += " : (" + state.mDebugTokenName + ")";
                    }
                }

                if (label != null)
                {
                    strGraph.Append("label=\"" + label + "\", ");
                }

                strGraph.Append("];\n");
            }

            strGraph.Append("\n");

            // Record transitions
            for (int i = 0; i < faStates.Count; ++i)
            {
                State        state = faStates[i];
                List <State> states;

                state.GetTransitions(Constants.Epsilon, out states);

                for (int j = 0; j < states.Count; ++j)
                {
                    // Record transition
                    String strStateID1 = state.mStateID.ToString();
                    String strStateID2 = states[j].mStateID.ToString();
                    strGraph.Append("\t" + strStateID1 + " -> " + strStateID2);
                    strGraph.Append("\t[label=\"epsilon\"];\n");
                }

                foreach (char input in mInputSet)
                {
                    state.GetTransitions(input, out states);

                    for (int j = 0; j < states.Count; ++j)
                    {
                        // Record transition
                        String strStateID1 = state.mStateID.ToString();
                        String strStateID2 = states[j].mStateID.ToString();
                        strGraph.Append("\t" + strStateID1 + " -> " + strStateID2);
                        strGraph.Append("\t[label=\"" + input.ToString() + "\"];\n");
                    }
                }
            }

            strGraph.Append("}");

            File.WriteAllText(@"C:\Temp\" + name + @".gv", strGraph.ToString());

            System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Graphviz2.28\bin\dot.exe", @"-Tpng -o""C:\Temp\" + name + @".png"" ""C:\Temp\" + name + @".gv""").Close();
        }
Exemple #3
0
        private void ConvertNFAtoDFA()
        {
            mDFATable = new FSATable();

            if (mNFATable.GetStates().Count == 0)
            {
                return;
            }

            State.mStateIDCounter = 0;

            HashSet <State> dfaStartStates;

            EpsilonClosure(out dfaStartStates, mNFATable.GetFirstState());

            State dfaStartState = new State(dfaStartStates);

            mDFATable.AppendState(dfaStartState);

            List <State> unmarkedDFAStates = new List <State>();

            unmarkedDFAStates.Add(dfaStartState);

            while (unmarkedDFAStates.Count != 0)
            {
                State processingDFAState = unmarkedDFAStates[unmarkedDFAStates.Count - 1];
                unmarkedDFAStates.RemoveAt(unmarkedDFAStates.Count - 1);

                foreach (char c in mInputSet)
                {
                    HashSet <State> moveResult, epsilonClosureResult;
                    Move(c, out moveResult, new HashSet <State>(processingDFAState.mNFAStates));
                    EpsilonClosure(out epsilonClosureResult, moveResult);

                    bool  found = false;
                    State s     = null;

                    for (int i = 0; i < mDFATable.GetStates().Count; ++i)
                    {
                        s = mDFATable.GetState(i);

                        if (s.mNFAStates.SetEquals(epsilonClosureResult))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found == false)
                    {
                        State u = new State(epsilonClosureResult);
                        unmarkedDFAStates.Add(u);
                        mDFATable.AppendState(u);

                        processingDFAState.AddTransition(c, u);
                    }
                    else
                    {
                        processingDFAState.AddTransition(c, s);
                    }
                }
            }
        }