public bool addState(State state) { if (isStatePresent(state)) { return false; } state.QLabel = "q" + qcounter++.ToString(); States.Add(state); return true; }
public DFA(RegularExpression re) { States = new List<State>(); InitState = new State(re, DFABuilder.IsExpressionFinal(re)); InitState.QLabel = "q0"; Transitions = new List<Transition>(); States.Add(InitState); Alphabet = new HashSet<char>(); foreach (char c in re.Value) { if (Char.IsLetter(c)) Alphabet.Add(c); } }
public bool addTransition(State to, char trans) { bool present = false; foreach(KeyValuePair<char,State> s in transistions) { if (s.Value.RegexLabel == to.RegexLabel && s.Key == trans) { present = true; break; } } if (!present) { transistions.Add(trans, to); return true; } else return false; }
private bool isStatePresent(State state) { foreach (State s in States) { if (s.RegexLabel == state.RegexLabel) return true; } return false; }
public void buildDFA(string re) { Dfa = new DFA(new RegularExpression(re)); var newStates = new HashSet<State>(); var newTransitions = new List<Transition>(); var newSteps = new List<Step>(); int added = 0; do { added = 0; newStates.Clear(); newTransitions.Clear(); newSteps.Clear(); foreach (State state in Dfa.States) { foreach (char c in Dfa.Alphabet) { RegularExpression newRegEx = Derive(new RegularExpression(state.RegexLabel), c); State newState = null; newState = new State(newRegEx, IsExpressionFinal(newRegEx)); newStates.Add(newState); newTransitions.Add(new Transition(state.RegexLabel, newState.RegexLabel, c)); Step newStep = new Step(state, newState, c); newSteps.Add(newStep); } } foreach (State state in newStates) { bool hasSubstring = false; foreach (State s1 in Dfa.States) { if(state.RegexLabel.Contains(s1.RegexLabel) && state.RegexLabel != s1.RegexLabel) { hasSubstring = true; break; } } State equivalent = null; if (hasSubstring) { var testWindow = new UserInteraction(state.RegexLabel, Dfa.States); if (testWindow.ShowDialog() == false) { equivalent = testWindow.GetSelectedState(); } if (equivalent == null) { if (Dfa.addState(state)) { added++; } } else { // update transitions foreach (Transition t in Dfa.Transitions) { if (t.From == state.RegexLabel) t.From = equivalent.RegexLabel; if (t.To == state.RegexLabel) t.To = equivalent.RegexLabel; } foreach (Transition t in newTransitions) { if (t.From == state.RegexLabel) t.From = equivalent.RegexLabel; if (t.To == state.RegexLabel) t.To = equivalent.RegexLabel; } //update steps foreach (Step step in Steps) { if (step.From.RegexLabel == state.RegexLabel) step.From.RegexLabel = equivalent.RegexLabel; if (step.To.RegexLabel == state.RegexLabel) step.To.RegexLabel = equivalent.RegexLabel; } foreach (Step step in newSteps) { if (step.From.RegexLabel == state.RegexLabel) step.From.RegexLabel = equivalent.RegexLabel; if (step.To.RegexLabel == state.RegexLabel) step.To.RegexLabel = equivalent.RegexLabel; } if (Dfa.addState(equivalent)) { added++; } } } if (Dfa.addState(state)) { added++; } } foreach (Transition t in newTransitions) { bool isTransitionAlreadyPresent = false; foreach (Transition t1 in Dfa.Transitions) { if (t1.From == t.From && t1.To == t.To && t.Over == t1.Over) { isTransitionAlreadyPresent = true; break; } } if (isTransitionAlreadyPresent) continue; Dfa.Transitions.Add(t); } foreach (Step step in newSteps) { bool isStepAlreadyPresent = false; foreach (Step s1 in Steps) { if (step.From.RegexLabel == s1.From.RegexLabel && step.To.RegexLabel == s1.To.RegexLabel && step.Over == s1.Over) { isStepAlreadyPresent = true; break; } } if (isStepAlreadyPresent) continue; Steps.Add(step); } } while (added != 0); }
private void btnStartSimulation_Click(object sender, RoutedEventArgs e) { if (txtWord.Text.Length == 0) { MessageBox.Show("Enter a word first."); return; } _word = txtWord.Text; foreach (char c in _word) { if (!_dfa.Alphabet.Contains(c)) { MessageBox.Show("Not accepted - your word contains illegal characters or letters, that do not belong to the alphabet."); return; } } _currentChar = 0; _currentState = _dfa.InitState; string dot = generateDot(); BitmapImage bmp = dot2bmp(dot); imgGraph.Source = bmp; btnNextStep.IsEnabled = true; btnStartSimulation.IsEnabled = false; if (_word.Length == 1) { if (_currentState.IsFinal) { MessageBox.Show("The word \"" + _word + "\" is accepted."); } else { MessageBox.Show("The word \"" + _word + "\" is not accepted."); } btnNextStep.IsEnabled = false; btnStartSimulation.IsEnabled = true; } }
private string generateDot() { State newCurrentState = null; // find new current state foreach (Transition t in _dfa.Transitions) { if (t.From == _currentState.RegexLabel && t.Over == _word[_currentChar]) { foreach (State s in _dfa.States) { if (s.RegexLabel == t.To) { newCurrentState = s; break; } } break; } } StringBuilder sbFsm = new StringBuilder(); sbFsm.Append("digraph finite_state_machine { rankdir=LR; size=\"7,5\" "); StringBuilder sbFinalStates = new StringBuilder(); foreach (State state in _dfa.FinalStates) { sbFinalStates.Append("\""); sbFinalStates.Append(state.QLabel); sbFinalStates.Append("\" "); } //sbFinalStates.Append("; "); sbFsm.Append("node [shape = doublecircle]; "); sbFsm.Append(sbFinalStates.ToString()); sbFsm.Append("node [shape = circle]; "); for (int i = 0; i < _dfaBuilder.Steps.Count; i++) { State from = _dfa.States.LastOrDefault(s => s.RegexLabel == _steps[i].From.RegexLabel); State to = _dfa.States.LastOrDefault(s => s.RegexLabel == _steps[i].To.RegexLabel); sbFsm.Append(from.QLabel); sbFsm.Append(" -> "); sbFsm.Append(to.QLabel); sbFsm.Append(" [ label = \""); sbFsm.Append(_steps[i].Over); sbFsm.Append("\""); if(from.RegexLabel == _currentState.RegexLabel && to.RegexLabel == newCurrentState.RegexLabel && _steps[i].Over == _word[_currentChar]) sbFsm.Append(", color=red"); sbFsm.Append("] ; "); } sbFsm.Append("\"" + _currentState.QLabel + "\" [color=red];"); sbFsm.Append("\"" + newCurrentState.QLabel + "\" [color=red]"); sbFsm.Append("}"); _currentChar++; _currentState = newCurrentState; return sbFsm.ToString(); }
private void lstStates_SelectionChanged(object sender, SelectionChangedEventArgs e) { _selectedState = (State) lstStates.SelectedItem; }
public Step(State from, State to, char over) { From = from; To = to; Over = over; }