Example #1
0
File: NFA.cs Project: sebgod/hime
        /// <summary>
        /// Creates and initializes a minimal automaton with an entry state and a separate exit state
        /// </summary>
        /// <returns>The created automaton</returns>
        public static NFA NewMinimal()
        {
            NFA result = new NFA();

            result.StateEntry = result.AddNewState();
            result.StateExit  = result.AddNewState();
            return(result);
        }
Example #2
0
File: NFA.cs Project: sebgod/hime
        /// <summary>
        /// Creates an automaton that is the difference between the left and right sub-automata
        /// </summary>
        /// <param name="left">The left automaton</param>
        /// <param name="right">The right automaton</param>
        /// <param name="useClones">True to completely clone the sub-automata</param>
        /// <returns>The new automaton</returns>
        // TODO: should get rid of static methods
        public static NFA NewDifference(NFA left, NFA right, bool useClones)
        {
            NFA      final         = NewMinimal();
            NFAState statePositive = final.AddNewState();
            NFAState stateNegative = final.AddNewState();

            statePositive.Mark = 1;
            stateNegative.Mark = -1;

            if (useClones)
            {
                left  = left.Clone(true);
                right = right.Clone(true);
            }
            final.states.AddRange(left.states);
            final.states.AddRange(right.states);
            final.StateEntry.AddTransition(NFA.EPSILON, left.StateEntry);
            final.StateEntry.AddTransition(NFA.EPSILON, right.StateEntry);
            left.StateExit.AddTransition(NFA.EPSILON, statePositive);
            right.StateExit.AddTransition(NFA.EPSILON, stateNegative);
            statePositive.AddTransition(NFA.EPSILON, final.StateExit);

            final.StateExit.AddItem(DummyItem.Instance);
            DFA equivalent = new DFA(final);

            equivalent.Prune();
            final           = new NFA(equivalent);
            final.StateExit = final.AddNewState();
            foreach (NFAState state in final.states)
            {
                if (state.Items.Contains(DummyItem.Instance))
                {
                    state.ClearItems();
                    state.AddTransition(NFA.EPSILON, final.StateExit);
                }
            }
            return(final);
        }