//Used Ref for pointer use and prevent overflows... private static bool deltaE(ref string toState, string[] states, char symbol, NDFA <string> ndfa) { bool isFinalState = false; SortedSet <string> newStates = new SortedSet <string>(); foreach (string state in states) { List <Transition <string> > trans = ndfa.GetTransition(state); foreach (Transition <string> t in trans) { if (t.symbol == symbol) { eClosure(t.toState, ndfa, ref newStates); if (ndfa.finalStates.Contains(t.toState)) { isFinalState = true; } } } } foreach (string subState in newStates) { toState += subState + "_"; } toState = toState.TrimEnd('_'); return(isFinalState); }
private static int CheckAvailableRoutes(string[] states, char symbol, NDFA <string> ndfa) { //array which shows how many possible routes there are for each sub-state int[] possibleRoutesPerState = new int[states.Length]; // value that shows the amount of routes the ndfa has for all the substates combined. int correctAmountOfRoutes = 0; //reads ndfa for possible routes, saves maximum amount of accessible routes to correctAmountOfRoutes foreach (string state in states) { if (ndfa.GetTransition(state).Count(transition => transition.symbol == symbol) > correctAmountOfRoutes) { correctAmountOfRoutes = ndfa.GetTransition(state).Count(transition => transition.symbol == symbol); } } return(correctAmountOfRoutes); }
//Used Ref for pointer use and prevent overflows... private static void eClosure(string state, NDFA <string> ndfa, ref SortedSet <string> subStateList) { subStateList.Add(state); List <Transition <string> > trans = ndfa.GetTransition(state); foreach (Transition <string> t in trans) { if (t.symbol == '$' && !subStateList.Contains(t.toState)) { eClosure(t.toState, ndfa, ref subStateList); } } }
private static void ConvertAutomata(string currentState, ref DFA <string> dfa, ref NDFA <string> ndfa) { if (dfa.GetTransition(currentState).Count == ndfa.symbols.Length) { return; } string[] states = currentState.Split('_'); foreach (char symbol in ndfa.symbols) { List <Transition <string> > currentTrans = dfa.GetTransition(currentState); foreach (Transition <string> t in currentTrans) { if (t.symbol == symbol) { return; } } int[] counts = new int[states.Length]; for (int i = 0; i < states.Length; i++) { counts[i] = ndfa.GetTransition(states[i]).Count(transition => transition.symbol == symbol); } int amountOfRoutes = 0; foreach (int i in counts) { if (i > amountOfRoutes) { amountOfRoutes = i; } } if (amountOfRoutes == 0) { dfa.addTransition(new Transition <string>(currentState, symbol, "F")); } string toState = ""; bool isFinalState = false; SortedSet <String> newState = new SortedSet <string>(); if (amountOfRoutes >= 1) { foreach (string state in states) { List <Transition <string> > trans = ndfa.GetTransition(state); foreach (Transition <string> t in trans) { if (t.symbol == symbol) { newState.Add(t.toState); if (ndfa.finalStates.Contains(t.toState)) { isFinalState = true; } } } } foreach (string subState in newState) { toState += subState; toState += "_"; } if (newState.Count >= 1) { toState = toState.TrimEnd('_'); } dfa.addTransition(new Transition <string>(currentState, symbol, toState)); if (ndfa.finalStates.Contains(currentState)) { dfa.addFinalState(currentState); } if (isFinalState) { dfa.addFinalState(toState); } if (currentState != toState) { ConvertAutomata(toState, ref dfa, ref ndfa); } } } }