コード例 #1
0
ファイル: NFA.cs プロジェクト: wyb314/RegexEngine
        /////////////////////////////////////////////////////////////////
        //
        // NFA building functions
        //
        // Using Thompson Construction, build NFAs from basic inputs or
        // compositions of other NFAs.
        //

        /// <summary>
        /// Builds a basic, single input NFA
        /// </summary>
        /// <param name="in"></param>
        /// <returns></returns>
        public static NFA BuildNFABasic(input @in)
        {
            NFA basic = new NFA(2, 0, 1);

            basic.AddTrans(0, 1, @in);

            return(basic);
        }
コード例 #2
0
ファイル: NFA.cs プロジェクト: wyb314/RegexEngine
        /// <summary>
        /// Builds a star (kleene closure) of nfa (nfa*)
        /// How this is done: First will come the new initial state, then NFA, then the
        /// new final state
        /// </summary>
        /// <param name="nfa"></param>
        /// <returns></returns>
        public static NFA BuildNFAStar(NFA nfa)
        {
            // Makes room for the new initial state
            nfa.ShiftStates(1);

            // Makes room for the new final state
            nfa.AppendEmptyState();

            // Adds new transitions
            nfa.AddTrans(nfa.final, nfa.initial, (char)Constants.Epsilon);
            nfa.AddTrans(0, nfa.initial, (char)Constants.Epsilon);
            nfa.AddTrans(nfa.final, nfa.size - 1, (char)Constants.Epsilon);
            nfa.AddTrans(0, nfa.size - 1, (char)Constants.Epsilon);

            nfa.initial = 0;
            nfa.final   = nfa.size - 1;

            return(nfa);
        }
コード例 #3
0
ファイル: NFA.cs プロジェクト: wyb314/RegexEngine
        /// <summary>
        /// Builds an alternation of nfa1 and nfa2 (nfa1|nfa2)
        /// </summary>
        /// <param name="nfa1"></param>
        /// <param name="nfa2"></param>
        /// <returns></returns>
        public static NFA BuildNFAAlter(NFA nfa1, NFA nfa2)
        {
            // How this is done: the new nfa must contain all the states in
            // nfa1 and nfa2, plus a new initial and final states.
            // First will come the new initial state, then nfa1's states, then
            // nfa2's states, then the new final state

            // make room for the new initial state
            nfa1.ShiftStates(1);

            // make room for nfa1
            nfa2.ShiftStates(nfa1.size);

            // create a new nfa and initialize it with (the shifted) nfa2
            NFA newNFA = new NFA(nfa2);

            // nfa1's states take their places in new_nfa
            newNFA.FillStates(nfa1);

            // Set new initial state and the transitions from it
            newNFA.AddTrans(0, nfa1.initial, (char)Constants.Epsilon);
            newNFA.AddTrans(0, nfa2.initial, (char)Constants.Epsilon);

            newNFA.initial = 0;

            // Make up space for the new final state
            newNFA.AppendEmptyState();

            // Set new final state
            newNFA.final = newNFA.size - 1;

            newNFA.AddTrans(nfa1.final, newNFA.final, (char)Constants.Epsilon);
            newNFA.AddTrans(nfa2.final, newNFA.final, (char)Constants.Epsilon);

            return(newNFA);
        }