コード例 #1
0
ファイル: RegexBuilder.cs プロジェクト: jodavis42/ZeroCore
        private bool Pop(out FSATable tableOut)
        {
            if (mOperandStack.Count != 0)
            {
                tableOut = mOperandStack[mOperandStack.Count - 1];
                mOperandStack.RemoveAt(mOperandStack.Count - 1);
                return(true);
            }

            tableOut = null;
            return(false);
        }
コード例 #2
0
ファイル: RegexBuilder.cs プロジェクト: jodavis42/ZeroCore
        private void Push(char input)
        {
            State s0 = new State();
            State s1 = new State();

            s0.AddTransition(input, s1);

            FSATable nfaTable = new FSATable();

            nfaTable.AppendState(s0);
            nfaTable.AppendState(s1);

            mOperandStack.Add(nfaTable);

            mInputSet.Add(input);
        }
コード例 #3
0
 public void AppendFSATable(FSATable table)
 {
     mStates.AddRange(table.mStates);
 }
コード例 #4
0
ファイル: RegexBuilder.cs プロジェクト: jodavis42/ZeroCore
        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();
        }
コード例 #5
0
ファイル: RegexBuilder.cs プロジェクト: jodavis42/ZeroCore
        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);
                    }
                }
            }
        }