private static Automaat <int> EindigtOp(string rulesymbols, char[] alphabet) { var a = new Automaat <int>(alphabet); var stateCounter = 0; var startState = ++stateCounter; var fromState = startState; a.DefineAsStartState(fromState); var word = ""; foreach (var s in rulesymbols.ToCharArray()) { stateCounter++; word += s; foreach (var letter in a.GetAlphabet()) { if (s == letter) { a.AddTransition(new Transition <int>(fromState, s, stateCounter)); } else { int i; for (i = 0; i < word.Length; i++) { if (word[i] != letter) { break; } } a.AddTransition(new Transition <int>(fromState, letter, i + startState)); } } fromState = stateCounter; } foreach (var letter in a.GetAlphabet()) { int i; for (i = 0; i < rulesymbols.Length; i++) { if (word[i] != letter) { break; } } a.AddTransition(new Transition <int>(fromState, letter, i + startState)); } a.DefineAsFinalState(stateCounter); return(a); }
private static void FindNextState(ref Automaat <State> a, ref int counter, Automaat <T> a1, Automaat <T> a2, State s) { foreach (var c in a.GetAlphabet()) { var a1State = a1.GetTransitions(s.StateAutomaat1, c).First().ToState; var a2State = a2.GetTransitions(s.StateAutomaat2, c).First().ToState; var newState = default(State); var stateExsists = false; foreach (var state in a._states) { if (!state.HaseSameStates(a1State, a2State)) { continue; } newState = state; stateExsists = true; break; } if (!stateExsists) { newState = new State(++counter, a1State, a2State); a.AddTransition(new Transition <State>(s, c, newState)); FindNextState(ref a, ref counter, a1, a2, newState); } else { a.AddTransition(new Transition <State>(s, c, newState)); } } }
private static Automaat <int> BegintMet(string symbols, char[] alphabet) { var a = new Automaat <int>(alphabet); var stateCounter = 0; var wrongState = stateCounter++; var fromState = stateCounter; a.DefineAsStartState(fromState); foreach (var s in symbols.ToCharArray()) { stateCounter++; a.AddTransition(new Transition <int>(fromState, s, stateCounter)); foreach (var letter in a.GetAlphabet()) { if (s == letter) { continue; } a.AddTransition(new Transition <int>(fromState, letter, wrongState)); } fromState = stateCounter; } // on this point, the statecounter is the final state foreach (var s in a.GetAlphabet()) { a.AddTransition(new Transition <int>(wrongState, s)); a.AddTransition(new Transition <int>(stateCounter, s)); } a.DefineAsFinalState(stateCounter); return(a); }
private static Automaat <State> BetweenAutomaat(Automaat <T> a1, Automaat <T> a2) { var alphabet = new SortedSet <char>(a1.GetAlphabet()); a2.GetAlphabet().ToList().ForEach(c => alphabet.Add(c)); var newA = new Automaat <State>(alphabet); var stateCounter = 0; var beginState = new State(stateCounter, a1._startStates.First(), a2._startStates.First()); newA.DefineAsStartState(beginState); FindNextState(ref newA, ref stateCounter, a1, a2, beginState); return(newA); }
public static Automaat <int> MakeDfa <T>(Automaat <T> ndfa) where T : IComparable { var betweenDfa = new Automaat <State <T> >(ndfa.GetAlphabet()); var table = MakeTable(ndfa); // PrintTable(table); var stateCounter = 1; // Define start state var startStates = new SortedSet <T>(ndfa._startStates); ndfa._startStates.ToList().ForEach(sState => GetToStatesByEpsilon(ref startStates, ndfa, sState)); var startState = new State <T>(stateCounter) { States = startStates }; betweenDfa.DefineAsStartState(startState); BuildBetweenDfa(ref betweenDfa, table, ref stateCounter, startState); // Define final state var finalStates = new SortedSet <State <T> >(); foreach (var state in betweenDfa._states) { foreach (var stateInAState in state.States) { if (ndfa._finalStates.Contains(stateInAState)) { finalStates.Add(state); } } } foreach (var finalState in finalStates) { betweenDfa.DefineAsFinalState(finalState); } return(BuildDfa(betweenDfa)); }
public static RegGram <T> NdfaToRegGram(Automaat <T> ndfa) { var regGram = new RegGram <T>(ndfa.GetAlphabet()); foreach (var trans in ndfa._transitions) { regGram.AddProductionRule(new PRule <T>(trans.FromState, trans.Symbol, trans.ToState)); if (ndfa._finalStates.Contains(trans.ToState)) { regGram.AddProductionRule(new PRule <T>(trans.FromState, trans.Symbol) { ToSymbolIsFinalSymbol = true }); } } regGram.DefineStartSymbol(ndfa._startStates.First()); return(regGram); }
private static Automaat <int> BuildDfa <T>(Automaat <State <T> > stateAutomaat) where T : IComparable { var dfa = new Automaat <int>(stateAutomaat.GetAlphabet()); foreach (var startState in stateAutomaat._startStates) { dfa.DefineAsStartState(startState.Id); } foreach (var finalState in stateAutomaat._finalStates) { dfa.DefineAsFinalState(finalState.Id); } foreach (var trans in stateAutomaat._transitions) { dfa.AddTransition(new Transition <int>(trans.FromState.Id, trans.Symbol, trans.ToState.Id)); } if (!dfa.IsDfa()) { throw new Exception("Created dfa is not a dfa :("); } return(dfa); }