public static NFA CreateAutomaton(string a)
        {
            var result = new NFA();
            var start  = 0;
            var next   = 1;

            result.AddState(start, false);

            for (int i = 0; i < a.Length; ++i)
            {
                if (a[i] == '*')
                {
                    result.AddTransition(next - 1, next - 1, NFA.Any);
                }
                else
                {
                    result.AddState(next, false);
                    result.AddTransition(next - 1, next, (a[i] != '?' ? CharRange.SingleChar(a[i]) : NFA.Any));
                    ++next;
                }
            }

            result.AddState(next, true);
            result.AddTransition(next - 1, next, NFA.Epsilon);

            return(result);
        }
Beispiel #2
0
        public void AddExpression(string name, string regex)
        {
            using (var stream = new StringReader(InfixToPostfix.Convert(regex)))
            {
                while (stream.Peek() != -1)
                {
                    var c = (char) stream.Read();
                    switch (c)
                    {
                        case '.': _stack.Concatenate(); break;
                        case '|': _stack.Unite(); break;
                        case '*': _stack.Iterate(); break;
                        case '+': _stack.AtLeast(); break;
                        case '?': _stack.Maybe(); break;
                        default:
                            var a = new NFA<char>();
                            a.AddTransition(a.Start, new State(), c == '\\' ? Escape((char) stream.Read()) : c);
                            _stack.Push(a);
                            break;
                    }
                }

                var top = _stack.Peek();
                top.LastAdded.Final = true;
                top.SetName(top.LastAdded, name);
            }
        }
Beispiel #3
0
        public void AddExpression(string name, string regex)
        {
            using (var stream = new StringReader(InfixToPostfix.Convert(regex)))
            {
                while (stream.Peek() != -1)
                {
                    var c = (char)stream.Read();
                    switch (c)
                    {
                    case '.': _stack.Concatenate(); break;

                    case '|': _stack.Unite(); break;

                    case '*': _stack.Iterate(); break;

                    case '+': _stack.AtLeast(); break;

                    case '?': _stack.Maybe(); break;

                    default:
                        var a = new NFA <char>();
                        a.AddTransition(a.Start, new State(), c == '\\' ? Escape((char)stream.Read()) : c);
                        _stack.Push(a);
                        break;
                    }
                }

                var top = _stack.Peek();
                top.LastAdded.Final = true;
                top.SetName(top.LastAdded, name);
            }
        }
Beispiel #4
0
        public void NFATests()
        {
            var a = new NFA<char>();

            // "a" automaton
            a.AddTransition(a.Start, new State { Final = true }, 'a');
            a.Initial();

            a.Trigger('a');
            Assert.That(a.Current.Contains(a.LastAdded), Is.True);

            a.Initial();
            Assert.That(() => a.Trigger('b'), Throws.TypeOf<StateNotFoundException>());

            // "abcd" automaton
            a = new NFA<char>();
            a.AddTransition(a.Start, new State(), 'a');
            a.AddTransition(a.LastAdded, new State(), 'b');
            a.AddTransition(a.LastAdded, new State(), 'c');
            a.AddTransition(a.LastAdded, new State { Final = true }, 'd');
            a.Initial();

            foreach (char c in "abcd") {
                Assert.That(a.Current.Contains(a.LastAdded), Is.False);
                a.Trigger(c);
            }
            Assert.That(a.Current.Contains(a.LastAdded), Is.True);
            Assert.That(() => a.Trigger('b'), Throws.TypeOf<StateNotFoundException>());
        }
Beispiel #5
0
        public override NFA ToNFA()
        {
            var start  = new State();
            var accept = new State();
            var nfa    = new NFA(start, accept);

            nfa.AddTransition(start, accept, Sym);
            return(nfa);
        }
Beispiel #6
0
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            var startState = nfa.AddState();
            var endState   = nfa.AddState();

            foreach (var equivalenceClass in equivalenceClasses.GetClasses(CodePoints))
            {
                nfa.AddTransition(startState, equivalenceClass, endState);
            }

            return(new StateRange(startState, endState));
        }
        public override NFA ToNFA()
        {
            var leftNfa  = Left.ToNFA();
            var rightNfa = Right.ToNFA();

            var nfa = new NFA(leftNfa.Start, rightNfa.Accept);

            foreach (var transition in leftNfa.Transitions)
            {
                nfa.AddTransition(transition.Source, transition.Target, transition.Label);
            }

            foreach (var transition in rightNfa.Transitions)
            {
                nfa.AddTransition(transition.Source, transition.Target, transition.Label);
            }

            nfa.AddTransition(leftNfa.Accept, rightNfa.Start, null);

            return(nfa);
        }
Beispiel #8
0
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            var startState = nfa.AddState();
            var endState   = startState;

            foreach (var codePoint in Value.GetCodePoints())
            {
                var lastState = endState;
                endState = nfa.AddState();
                nfa.AddTransition(lastState, equivalenceClasses.GetClass(codePoint), endState);
            }
            return(new StateRange(startState, endState));
        }
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            // Create a separate NFA
            var tempNFA = new NFA <State?>(equivalenceClasses.Count);
            var states  = Expression.AddTo(tempNFA, equivalenceClasses);

            tempNFA.SetStart(states.Start);
            tempNFA.SetFinal(states.End);

            // Convert to a DFA
            var dfa = tempNFA.ToDFA(stateData => null).Item2;

            dfa = dfa.Minimize().Item2;
            dfa.MakeComplete();             // Can only do complement on complete DFA

            // Now add the complement to this nfa
            var startState = nfa.AddState();
            var endState   = nfa.AddState();

            foreach (var dfaState in dfa.States)
            {
                var nfaState = GetMatchingState(nfa, dfa, dfaState);
                if (dfa.IsStart(dfaState))
                {
                    nfa.AddEpsilonTransition(startState, nfaState);
                }
                if (!dfa.IsFinal(dfaState))
                {
                    nfa.AddEpsilonTransition(nfaState, endState);
                }
                // Copy transitions
                foreach (var input in nfa.Inputs)
                {
                    var toDFAState = dfa.GetTransition(dfaState, input);
                    if (toDFAState != null)
                    {
                        var toNFAState = GetMatchingState(nfa, dfa, toDFAState.Value);
                        nfa.AddTransition(nfaState, input, toNFAState);
                    }
                }
            }

            return(new StateRange(startState, endState));
        }
Beispiel #10
0
 public void AddExpression(string name, string regex)
 {
     var stream = new StringReader(InfixToPostfix.Convert(regex));
     while (stream.Peek() != -1)
     {
         var c = (char) stream.Read();
         NFA<char> a;
         switch (c)
         {
             case '.':
                 _stack.Concatenate();
                 break;
             case '|':
                 _stack.Unite();
                 break;
             case '*':
                 _stack.Iterate();
                 break;
             case '+':
                 _stack.AtLeast();
                 break;
             case '?':
                 _stack.Maybe();
                 break;
             case '\\':
                 char n = (char) stream.Read();
                 a = new NFA<char>();
                 a.AddTransition(a.Start, new State(), Escape(n));
                 _stack.Push(a);
                 break;
             default:
                 a = new NFA<char>();
                 a.AddTransition(a.Start, new State(), c);
                 _stack.Push(a);
                 break;
         }
     }
     var automaton = _stack.Peek();
     automaton.LastAdded.Final = true;
     automaton.SetName(automaton.LastAdded, name);
 }