Beispiel #1
0
 static bool CheckAutomaton(Automaton automaton, string input)
 {
     automaton.ResetState();
     foreach (var symbol in input)
     {
         automaton.Transit(symbol);
         if (automaton.IsFailed)
             return false;
     }
     return automaton.IsSuccess;
 }
Beispiel #2
0
 public State(string name, Automaton automaton)
     : this(automaton)
 {
     Name = name;
 }
Beispiel #3
0
 public State(Automaton automaton)
 {
     Automaton = automaton;
 }
Beispiel #4
0
 public FailedState(Automaton automaton)
     : base("Failed", automaton)
 {
     Terminal = true;
 }
Beispiel #5
0
        private void InitAutomata()
        {
            {
                // pattern ^\d+$
                _automaton1 = new Automaton();
                var initialState = new State("Initial", _automaton1);
                var digitState = new State("Digit", _automaton1);

                initialState.SetTransition("0123456789", digitState);

                digitState.SetTransition("0123456789", digitState);
                digitState.Terminal = true;

                _automaton1.InitialState = initialState;
                _automaton1.AddState(digitState);
            }

            {
                // pattern ^\d*\.\d+$
                _automaton2 = new Automaton();
                var initialState = new State("Initial", _automaton2);
                var dotState = new State("Dot", _automaton2);
                var digitState = new State("Digit", _automaton2);

                initialState.SetTransition("0123456789", initialState);
                initialState.SetTransition(".", dotState);

                dotState.SetTransition("0123456789", digitState);

                digitState.SetTransition("0123456789", digitState);
                digitState.Terminal = true;

                _automaton2.InitialState = initialState;
                _automaton2.AddState(dotState);
                _automaton2.AddState(digitState);
            }

            {
                // pattern ^(\s*)[-+]?\d*\.\d+([eE][+-]?\d+)$
                _automaton3 = new Automaton();

                var initialState = new State("Initial", _automaton3);
                var q1State = new State("q1", _automaton3);
                var q2State = new State("q2", _automaton3);
                var q3State = new State("q3", _automaton3);
                var q4State = new State("q4", _automaton3);
                var q5State = new State("q5", _automaton3);
                var q6State = new State("q6", _automaton3);

                initialState.SetTransition(" \t", initialState);
                initialState.SetTransition("-+0123456789", q1State);
                initialState.SetTransition(".", q2State);

                q1State.SetTransition("0123456789", q1State);
                q1State.SetTransition(".", q2State);

                q2State.SetTransition("0123456789", q3State);

                q3State.SetTransition("0123456789", q3State);
                q3State.SetTransition("eE", q4State);
                q3State.Terminal = true;

                q4State.SetTransition("0123456789", q6State);
                q4State.SetTransition("-+", q5State);

                q5State.SetTransition("0123456789", q6State);

                q6State.SetTransition("0123456789", q6State);
                q6State.Terminal = true;

                _automaton3.InitialState = initialState;
                _automaton3.AddState(q1State);
                _automaton3.AddState(q2State);
                _automaton3.AddState(q3State);
                _automaton3.AddState(q4State);
                _automaton3.AddState(q5State);
                _automaton3.AddState(q6State);
            }
        }