Esempio n. 1
0
        public NDFA <int, char> toNDFA(ref int i)
        {
            NDFA <int, char> ndfaLeft  = left?.toNDFA(ref i);
            NDFA <int, char> ndfaRight = right?.toNDFA(ref i);
            var ndfa = new NDFA <int, char>('$', ndfaLeft, ndfaRight);

            switch (op)
            {
            case Operator.ONE:
                var currState = ++i;
                ndfa.addStartState(currState);
                foreach (var terminal in terminals)
                {
                    ndfa.addTransition(terminal, currState, currState = ++i);
                }
                ndfa.addEndState(currState);
                break;

            case Operator.OR:
                var orStartState = ++i;
                var orEndState   = ++i;
                ndfa.addStartState(orStartState);
                ndfa.addTransition(ndfa.Epsilon, orStartState, ndfaLeft.StartStates.First());
                ndfa.addTransition(ndfa.Epsilon, orStartState, ndfaRight.StartStates.First());
                ndfa.addTransition(ndfa.Epsilon, ndfaLeft.EndStates.Last(), orEndState);
                ndfa.addTransition(ndfa.Epsilon, ndfaRight.EndStates.Last(), orEndState);
                ndfa.addEndState(orEndState);
                break;

            case Operator.DOT:
                ndfa.addStartState(ndfaLeft.StartStates.First());
                ndfa.addEndState(ndfaRight.EndStates.Last());
                ndfa.addTransition(ndfa.Epsilon, ndfaLeft.EndStates.Last(), ndfaRight.StartStates.First());
                break;

            case Operator.PLUS:
                ndfa = plusAndStarOperatorHandlerCalledByToNDFA(ndfa, ndfaLeft, ref i);
                break;

            case Operator.STAR:
                ndfa = plusAndStarOperatorHandlerCalledByToNDFA(ndfa, ndfaLeft, ref i);
                break;

            default:
                break;
            }
            return(ndfa);
        }
Esempio n. 2
0
        public NDFA <T, U> reverse(U epsilon)
        {
            NDFA <T, U> result = new NDFA <T, U>(epsilon);

            // Add DFA's startstate as endstate
            result.addEndState(StartState);
            foreach (T endState in EndStates)
            {
                result.addStartState(endState);
            }

            forEachTransition((terminal, fromState, toState)
                              => result.addTransition(terminal, toState, fromState));
            return(result);
        }
Esempio n. 3
0
        private NDFA <int, char> plusAndStarOperatorHandlerCalledByToNDFA(NDFA <int, char> ndfa, NDFA <int, char> left, ref int i)
        {
            var startState = ++i;
            var endState   = ++i;

            ndfa.addStartState(startState);
            ndfa.addEndState(endState);
            ndfa.addTransition(ndfa.Epsilon, startState, left.StartStates.First());
            ndfa.addTransition(ndfa.Epsilon, left.EndStates.Last(), left.StartStates.First());
            ndfa.addTransition(ndfa.Epsilon, left.EndStates.Last(), endState);
            if (op == Operator.STAR)
            {
                ndfa.addTransition(ndfa.Epsilon, startState, endState);
            }
            return(ndfa);
        }
Esempio n. 4
0
        public static void init()
        {
            // Starts with abb or ends with baab
            dfa.addStartState(0);
            dfa.addEndState(3);
            dfa.addEndState(8);
            dfa.addTransition('a', 0, 1);
            dfa.addTransition('b', 0, 4);
            dfa.addTransition('a', 1, 4);
            dfa.addTransition('b', 1, 2);
            dfa.addTransition('a', 2, 4);
            dfa.addTransition('b', 2, 3);
            dfa.addTransition('a', 3, 3);
            dfa.addTransition('b', 3, 3);
            dfa.addTransition('a', 4, 4);
            dfa.addTransition('b', 4, 5);
            dfa.addTransition('a', 5, 6);
            dfa.addTransition('b', 5, 5);
            dfa.addTransition('a', 6, 7);
            dfa.addTransition('b', 6, 5);
            dfa.addTransition('a', 7, 4);
            dfa.addTransition('b', 7, 8);
            dfa.addTransition('a', 8, 6);
            dfa.addTransition('b', 8, 5);

            // Ends with a, or bc
            ndfa.addStartState(1);
            ndfa.addEndState(9);
            ndfa.addTransition('a', 1, 1);
            ndfa.addTransition('b', 1, 1);
            ndfa.addTransition('$', 1, 2);
            ndfa.addTransition('$', 1, 9);
            ndfa.addTransition('$', 2, 4);
            ndfa.addTransition('$', 2, 6);
            ndfa.addTransition('a', 4, 5);
            ndfa.addTransition('$', 5, 3);
            ndfa.addTransition('$', 2, 6);
            ndfa.addTransition('b', 6, 7);
            ndfa.addTransition('c', 7, 8);
            ndfa.addTransition('$', 8, 9);
            ndfa.addTransition('$', 3, 2);
            ndfa.addTransition('$', 3, 9);
            ndfa.addTransition('$', 1, 9);

            ndfa2.addStartState(1);
            ndfa2.addEndState(3);
            ndfa2.addEndState(5);
            ndfa2.addTransition('a', 1, 3);
            ndfa2.addTransition('b', 1, 3);
            ndfa2.addTransition('b', 1, 2);
            ndfa2.addTransition('b', 2, 3);
            ndfa2.addTransition('$', 2, 3);
            ndfa2.addTransition('a', 3, 4);
            ndfa2.addTransition('b', 3, 4);
            ndfa2.addTransition('a', 3, 5);
            ndfa2.addTransition('a', 4, 3);
            ndfa2.addTransition('a', 4, 2);
            ndfa2.addTransition('$', 5, 4);
            ndfa2.addTransition('b', 5, 5);

            // ends with 'bbab'
            dfa1.addStartState(1);
            dfa1.addEndState(5);
            dfa1.addTransition('b', 1, 2);
            dfa1.addTransition('a', 1, 1);
            dfa1.addTransition('b', 2, 3);
            dfa1.addTransition('a', 2, 1);
            dfa1.addTransition('b', 3, 3);
            dfa1.addTransition('a', 3, 4);
            dfa1.addTransition('b', 4, 5);
            dfa1.addTransition('a', 4, 1);
            dfa1.addTransition('a', 5, 1);
            dfa1.addTransition('b', 5, 3);

            // Even amount of b's
            dfa2.addStartState(1);
            dfa2.addEndState(1);
            dfa2.addTransition('a', 1, 1);
            dfa2.addTransition('b', 1, 2);
            dfa2.addTransition('a', 2, 2);
            dfa2.addTransition('b', 2, 1);
        }