/// <summary> /// Determinates if the automaton accepts current word from alphabet /// </summary> /// <param name="input">a word from alphabet</param> /// <returns>returns true if the automaton accepted the word</returns> public bool Accepts(string input) { if (!IsValidAutomaton()) { Console.WriteLine("Automaton was not defined corectly"); return(false); } List <int> currentStateIds = new List <int>(InitialStateIds); ExpandCurrentStatesByEpsilonTransitions(currentStateIds); foreach (char c in input) { while (HasEpsilonTransition(currentStateIds)) { GoThroughEpsilon(currentStateIds); } currentStateIds = DoDeltaFunction(currentStateIds, c); if (currentStateIds.Count == 0) { return(false); } } ExpandCurrentStatesByEpsilonTransitions(currentStateIds); return(AcceptStates.Any(x => currentStateIds.Contains(x.Id))); }
private bool Validate() { return(!(Initial is null) && AcceptStates.Length != 0 && States.Length != 0 && States.Contains(Initial) && AcceptStates.Aggregate(true, (acc, s) => acc && States.Contains(s)) && States.Aggregate(true, (acc, s) => acc && s.Transitions.Keys.SequenceEqual(Alphabet))); }
public bool Accepts(string input) { int currenentStateId = this.InitialStateId; foreach (char c in input) { currenentStateId = DoDeltaFunction(currenentStateId, c); } return(AcceptStates.Any(x => x.Id == currenentStateId)); }
/// <summary> /// Converts nondeterministic automaton into deterministic automaton /// </summary> /// <returns>A new instance of deterministic automaton</returns> public DeterministicFiniteAutomaton ConvertToDeterministicFiniteAutomaton() { if (!IsValidAutomaton()) { Console.WriteLine("Automaton was not defined corectly"); return(null); } List <DeltaFunctionTriplet> NewDFT = new List <DeltaFunctionTriplet>(); Dictionary <string, int> StateRecorder = new Dictionary <string, int>(); List <State> NewStates = new List <State>(); List <int> currentStateIds = new List <int>(InitialStateIds); int stateCounter = 0; for (int i = 0; i <= stateCounter; i++) { if (i == stateCounter && i != 0) { break; } if (i != 0) { foreach (KeyValuePair <string, int> kvp in StateRecorder) { if (kvp.Value == i + 1) { string key = kvp.Key; currentStateIds.Clear(); string[] arrayOfIds = key.Split("+"); foreach (string s in arrayOfIds) { currentStateIds.Add(int.Parse(s)); } break; } } } ExpandCurrentStatesByEpsilonTransitions(currentStateIds); string keyStateId = BuildStateId(currentStateIds); if (i == 0) { stateCounter++; int value = stateCounter; StateRecorder.Add(keyStateId, value); NewStates.Add(new State( StateRecorder[keyStateId], "q" + StateRecorder[keyStateId], true, AcceptStates.Any(x => currentStateIds.Contains(x.Id)))); } HashSet <int> AllEndStatesIds = new HashSet <int>(); foreach (char c in Alphabet) { foreach (int id in currentStateIds) { if (!DeltaFunction.ContainsKey(id)) { continue; } SortedList <char, List <int> > EndStates = DeltaFunction[id]; if (!EndStates.ContainsKey(c)) { continue; } ExpandCurrentStatesByEpsilonTransitions(EndStates[c]); AllEndStatesIds.UnionWith(EndStates[c]); } string valueStateId = BuildStateId(AllEndStatesIds.ToList()); if (valueStateId.Equals("")) { valueStateId = "0"; } if (!StateRecorder.ContainsKey(valueStateId)) { if (!valueStateId.Equals("0")) { string[] arrayOfIds = valueStateId.Split("+"); bool isAccept = AcceptStates.Any(x => arrayOfIds.ToList().Contains(x.Id.ToString())); stateCounter++; StateRecorder.Add(valueStateId, stateCounter); NewStates.Add(new State( StateRecorder[valueStateId], "q" + StateRecorder[valueStateId], false, isAccept)); } else { StateRecorder.Add(valueStateId, 0); NewStates.Add(new State(0, char.ConvertFromUtf32(216).ToString(), false, false)); } } AllEndStatesIds.Clear(); NewDFT.Add(new DeltaFunctionTriplet(StateRecorder[keyStateId], c, StateRecorder[valueStateId])); } } if (StateRecorder.ContainsKey("0")) { foreach (char c in Alphabet) { NewDFT.Add(new DeltaFunctionTriplet(0, c, 0)); } } return(new DeterministicFiniteAutomaton(NewStates, Alphabet, NewDFT)); }
public int ProcessLine(string line) { currentState = StartState; var tempState = currentState; currentInput = line; int i; var tempI = 0; var currentWord = ""; var tempCurrentWord = ""; for (i = 0; i < currentInput.Length; i++) { var currentChar = currentInput[i]; if (char.IsWhiteSpace(currentChar)) { continue; } if (AcceptStates.Contains(currentState)) { tempState = currentState; tempI = i; tempCurrentWord = currentWord; } var transition = Transitions.FirstOrDefault(t => t.FromState == currentState && t.Symbol == currentChar); if (transition == null) { if (AcceptStates.Contains(currentState)) { currentState = StartState; i--; tempState = null; Console.WriteLine(currentWord + " " + LanguageWords.FirstOrDefault(kvp => kvp.Value == currentWord).Key); currentWord = ""; continue; } if (tempState == null || tempState == StartState) { return(i); } currentState = tempState; currentWord = tempCurrentWord; i = tempI - 1; i--; tempState = null; continue; } currentState = transition.ToState; currentWord += currentChar; } if (!AcceptStates.Contains(currentState)) { return(i); } Console.WriteLine(currentWord + " " + LanguageWords.FirstOrDefault(kvp => kvp.Value == currentWord).Key); return(-1); }
public bool Accepts(string input) { var finalState = Initial.Compute(input); return(AcceptStates.Contains(finalState)); }