示例#1
0
 public NFA(IList mStates, List<char> mSymbols, State mStartState, List<State> mFinalStates, List<TransitionFunction> mTransitionFunctions)
 {
     m_states = mStates;
     m_symbols = mSymbols;
     m_start_state = mStartState;
     m_final_states = mFinalStates;
     m_transition_functions = mTransitionFunctions;
 }
示例#2
0
 public DFA(IList states, List<char> symbols, State startstate, List<State> finalstates,
     List<TransitionFunction> transitionfunctions)
 {
     m_states = states;
     m_symbols = symbols;
     m_start_state = startstate;
     m_final_states = finalstates;
     m_transition_functions = transitionfunctions;
     IsDFA(this);
 }
示例#3
0
 private void button2_Click(object sender, EventArgs e)
 {
     //states
     var newState =new State(textBox2.Text);
     if (!string.IsNullOrEmpty(textBox2.Text) && !States.Contains(newState))
     {
         listBox2.Items.Add(newState);
         States.Add(newState);
         textBox2.Text = "";
     }
 }
示例#4
0
        public DFA ConvertToDfa()
        {
            var DfaSymbols = this.Symbols;
            var DfaInitialState = this.StartState;

            var DfaStates = new List<State> {DfaInitialState};

            List<State> DfaFinalStates = FinalStates;
            List<TransitionFunction> DfaTransitions = new List<TransitionFunction>();

            for (int i = 0; i < DfaStates.Count; i++)// i cant use foreach
            {
                var dfaCurrentState = DfaStates[i];
                foreach (var symbol in DfaSymbols)
                {
                    var result = TransitionFunctions.FindAll(transitionFunction => transitionFunction.InputState.Name == dfaCurrentState.Name && transitionFunction.InputSymbol == symbol);
                    if (result.Count == 1)
                    {
                        DfaTransitions.Add(new TransitionFunction(dfaCurrentState, result[0].OutputState, symbol));
                        if (!DfaStates.Contains(result[0].OutputState))
                            DfaStates.Add(result[0].OutputState);
                    }
                    if (result.Count == 0)
                    {
                        DfaTransitions.Add(new TransitionFunction(dfaCurrentState,
                            GetTrapState(DfaStates, DfaTransitions), symbol));
                    }
                    if (result.Count > 1)
                    {
                        string name = null; // create name to state from two states
                        foreach (var function in result)
                            name = name + function.InputState.Name;
                        var combinedState = new State(name);
                        DfaTransitions.Add(new TransitionFunction(dfaCurrentState, combinedState, symbol));
                        if (!DfaStates.Contains(combinedState))
                            DfaStates.Add(combinedState);
                        //need completion
                    }
                }
            }

            return new DFA(DfaStates, DfaSymbols, DfaInitialState, DfaFinalStates, DfaTransitions);
        }
示例#5
0
 private State GetTrapState(List<State> dfaStates, List<TransitionFunction> dfaTransitions)
 {
     var result= dfaStates.Find(state => state.Name == "*");
     if (result != null)
         return result;
     else
     {
         var trapStat = new State("*");
         foreach (var symbol in Symbols)
         {
             dfaTransitions.Add(new TransitionFunction(trapStat,trapStat,symbol));
         }
         dfaStates.Add(trapStat);
         return trapStat;
     }
 }
示例#6
0
 private List<TransitionFunction> GetAllTransitions(State currentState, char c)
 {
     return TransitionFunctions.FindAll(tf => tf.InputState.Name == currentState.Name && tf.InputSymbol == c);
 }
示例#7
0
 private bool Accepts(State currentState, string input, StringBuilder steps)
 {
     if (input.Length > 0)
     {
         var transitions = GetAllTransitions(currentState, input[0]);
         foreach (var transition in transitions)
         {
             var currentSteps = new StringBuilder(steps.ToString() + transition+ "\r\n");
             if (Accepts(transition.OutputState, input.Substring(1), currentSteps))
             {
                 return true;
             }
         }
         return false;
     }
     if (FinalStates.Contains(currentState))
     {
         Log += ConsoleWriter.Success("Successfully accepted the input " + input + " " +
                                "in the final state " + currentState +
                                " with steps:\r\n" + steps);
         return true;
     }
     return false;
 }
示例#8
0
 private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
     //initial State
     if(listBox4.Items.Count==1) return;
     if (listBox3.SelectedIndex >= 0)
         try
         {
             listBox4.Items.Add(listBox3.Items[listBox3.SelectedIndex]);
             InitialState=new State(listBox3.Items[listBox3.SelectedIndex].ToString());
         }
         catch (Exception exception)
         {
             ConsoleWriter.Failure(exception.Message);
         }
     
 }