static public Automata <String> TestNDFA() { char [] alphabet = { 'a', 'b' }; Automata <String> m = new Automata <String>(alphabet); m.addTransition(new Transition <String> ("1", 'a', "2")); m.addTransition(new Transition <String> ("2", 'b', "2")); m.addTransition(new Transition <String> ("2", 'b', "4")); m.addTransition(new Transition <String> ("1", 'a', "3")); m.addTransition(new Transition <String> ("3", 'a', "2")); m.addTransition(new Transition <String> ("3", 'a', "4")); m.addTransition(new Transition <String> ("4", 'a', "3")); // only on start state in a dfa: m.defineAsStartState("1"); // two final states: m.defineAsFinalState("3"); m.defineAsFinalState("4"); return(m); }
static public Automata <String> TestDFA() { char [] alphabet = { 'a', 'b' }; Automata <String> m = new Automata <String>(alphabet); m.addTransition(new Transition <String> ("q0", 'a', "q1")); m.addTransition(new Transition <String> ("q0", 'b', "q4")); m.addTransition(new Transition <String> ("q1", 'a', "q4")); m.addTransition(new Transition <String> ("q1", 'b', "q2")); m.addTransition(new Transition <String> ("q2", 'a', "q3")); m.addTransition(new Transition <String> ("q2", 'b', "q4")); m.addTransition(new Transition <String> ("q3", 'a', "q1")); m.addTransition(new Transition <String> ("q3", 'b', "q2")); // the error state, loops for a and b: m.addTransition(new Transition <String> ("q4", 'a')); m.addTransition(new Transition <String> ("q4", 'b')); // only on start state in a dfa: m.defineAsStartState("q0"); // two final states: m.defineAsFinalState("q2"); m.defineAsFinalState("q3"); return(m); }
static public Automata <String> TestProcedure2() { char [] alphabet = { 'a', 'b' }; Automata <String> m = new Automata <String>(alphabet); m.addTransition(new Transition <String> ("A", 'a', "C")); m.addTransition(new Transition <String> ("A", 'b', "B")); m.addTransition(new Transition <String> ("A", 'b', "C")); m.addTransition(new Transition <String> ("B", 'b', "C")); m.addTransition(new Transition <String> ("B", "C")); m.addTransition(new Transition <String> ("C", 'a', "D")); m.addTransition(new Transition <String> ("C", 'a', "E")); m.addTransition(new Transition <String> ("C", 'b', "D")); m.addTransition(new Transition <String> ("D", 'a', "B")); m.addTransition(new Transition <String> ("D", 'a', "C")); m.addTransition(new Transition <String> ("E", 'a')); m.addTransition(new Transition <String> ("E", "D")); // only on start state in a dfa: m.defineAsStartState("A"); // two final states: m.defineAsFinalState("C"); m.defineAsFinalState("E"); return(m); }
public Automata <String> renameStates(Automata <String> automaton) { Automata <String> renamedAutomaton = new Automata <string>(automaton.getAlphabet()); Dictionary <String, String> referencer = new Dictionary <string, string>(); int counter = 0; foreach (String state in automaton.getStates()) { referencer.Add(state, "q" + counter++); Console.WriteLine("q" + counter); } if (automaton.getStates().Contains("Ø")) { referencer["Ø"] = "Ø"; } foreach (Transition <String> transition in automaton.getTransitions()) { renamedAutomaton.addTransition(new Transition <string>(referencer[transition.getFromState()], transition.getSymbol(), referencer[transition.getToState()])); } foreach (String state in automaton.getFinalStates()) { renamedAutomaton.defineAsFinalState(referencer[state]); } foreach (String state in automaton.getStartStates()) { renamedAutomaton.defineAsStartState(referencer[state]); } return(renamedAutomaton); }
public Automata <String> regexToNDFA(int depth) { Automata <String> automata = new Automata <String>(); regexToNDFA(ref depth, ref automata); return(automata); }
public static void testRegExpThompson() { RegularExpression exp1, exp2, exp3, exp4, exp5, a, b, c, all, why; a = new RegularExpression("a"); b = new RegularExpression("b"); c = new RegularExpression("c"); //exp 1 (baa) exp1 = new RegularExpression("baa"); //exp 2 (ac) exp2 = new RegularExpression("ac"); //exp 4 (baa)+ exp4 = exp1.plus(); //exp 5 (ac)* exp5 = exp2.star(); //exp 3 (a|b|c)* exp3 = (a.or(b).or(c)).star(); //all (a|b|c)* (baa)+ (ca)* all = exp3.dot(exp4.dot(exp5)); why = new RegularExpression("baa").plus().star().plus(); foreach (String o in all.getLanguage(5)) { Console.Write(o + " "); } Automata <String> auto = new Automata <String>(); int num = 0; why.regexToNDFA(ref num, ref auto); return; }
public Automata <String> inverseAutomata(Automata <String> automaton) { Automata <String> invertedAutomaton = new Automata <string>(automaton.getAlphabet()); foreach (String state in automaton.getFinalStates()) { invertedAutomaton.defineAsStartState(state); } foreach (Transition <String> transition in automaton.getTransitions()) { invertedAutomaton.addTransition(transition); } foreach (String state in automaton.getStates()) { if (!automaton.getFinalStates().Contains(state)) { invertedAutomaton.defineAsFinalState(state); } } foreach (String state in automaton.getFinalStates()) { if (state != invertedAutomaton.getStartStates().First()) { invertedAutomaton.addTransition(new Transition <string>(invertedAutomaton.getStartStates().First(), state)); } } return(invertedAutomaton); }
public static void testRegularExpressionThompson2() { //This is the regular expression ( (a|b|c|d)|(ab|ad|bc) )+ (aab) (c|cad|cb)* RegularExpression exp1, exp2, exp3, exp4, a, b, c, d, ab, ad, bc, abb, cad, cb, all; a = new RegularExpression("a"); b = new RegularExpression("b"); c = new RegularExpression("c"); d = new RegularExpression("d"); ab = new RegularExpression("ab"); ad = new RegularExpression("ad"); bc = new RegularExpression("bc"); abb = new RegularExpression("abb"); cad = new RegularExpression("cad"); cb = new RegularExpression("cb"); // (a|b|c|d) exp1 = (a.or(b).or(c).or(d)); // (ab|ad|bc) exp2 = (ab.or(ad).or(bc)); // (c|cad|cb)* exp3 = (c.or(cad).or(cb).star()); // ( (a|b|c|d) | (ab|ad|bc) )+ exp4 = (exp1.or(exp2).plus()); // Merge all = exp4.dot(abb.dot(exp3)); Automata <String> auto = new Automata <String>(); int num = 0; all.regexToNDFA(ref num, ref auto); generateAutomataImage(auto); return; }
static public Automata <String> TestNDFA2() { char [] alphabet = { 'a', 'b', 'c', 'd' }; Automata <String> m = new Automata <String>(alphabet); m.addTransition(new Transition <String> ("0", '$', "1")); m.addTransition(new Transition <String> ("0", '$', "3")); m.addTransition(new Transition <String> ("1", 'b', "2")); m.addTransition(new Transition <String> ("3", 'a', "4")); m.addTransition(new Transition <String> ("2", '$', "5")); m.addTransition(new Transition <String> ("4", '$', "5")); m.addTransition(new Transition <String> ("5", '$', "0")); m.addTransition(new Transition <String> ("5", '$', "6")); m.addTransition(new Transition <String> ("6", 'b', "7")); m.addTransition(new Transition <String> ("7", 'c', "8")); m.addTransition(new Transition <String> ("8", 'd', "9")); // only on start state in a dfa: m.defineAsStartState("0"); // one final state: m.defineAsFinalState("9"); return(m); }
public static Automata <String> testReverse(Automata <String> automata) { AutomataConverter c = new AutomataConverter(); Automata <String> a = c.reverseAutomata(automata); return(a); }
private String answerNDFAToGrammar(Automata <String> automaton) { String result = "\\section{Grammatica}\n" + regexStringToLatexString((automaton.getGrammar().toBeautifulLatexString())) + "\n\n\\clearpage\n"; return(result); }
public Automata(Automata <T> autom) { this.transitions = autom.transitions; this.states = autom.states; this.startStates = autom.startStates; this.finalStates = autom.finalStates; this.setAlphabet(autom.getAlphabet()); }
public Automata <String> Minimization(Automata <String> Automaton) { AutomataConverter c = new AutomataConverter(); Automata <String> miniantwoord = c.NDFAToDFA(c.reverseAutomata(c.NDFAToDFA(c.reverseAutomata(Automaton)))); return(c.renameStates(miniantwoord)); }
static public void Minimalization() { Console.Clear(); Console.WriteLine("1. Minimalization DFA" + Environment.NewLine + "2. Minimalization Random DFA"); var ans = Console.ReadLine(); AutomataMinimalization m = new AutomataMinimalization(); AutomataConverter c = new AutomataConverter(); int choice = 0; if (int.TryParse(ans, out choice)) { switch (choice) { case 1: Console.Clear(); Automata <string> a = Tester.TestDFA2(); Console.WriteLine("De volgende DFA: "); a.printTransitions(); Tester.generateAutomataImage(a); Console.WriteLine("Is in Minimalisatie: (druk op een knop)"); Console.ReadLine(); Automata <String> mini = m.Minimization(a); mini.printTransitions(); Tester.generateAutomataImage(mini); ResetToMenu(); break; case 2: Console.Clear(); Automata <string> b = c.renameStates(Tester.generateRandomDfa()); Console.WriteLine("De volgende DFA: "); b.printTransitions(); Tester.generateAutomataImage(b); Console.WriteLine("Is in Minimalisatie: (druk op een knop)"); Console.ReadLine(); Automata <String> mini2 = m.Minimization(b); mini2.printTransitions(); Tester.generateAutomataImage(mini2); ResetToMenu(); break; default: Console.WriteLine("Deze optie is niet beschikbaar." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); break; } } else { Console.WriteLine("Vul alstublieft het nummer van de keuze in." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); } }
public Automata <String> DFAToNDFA(Automata <String> Automata) { SortedSet <char> alphabet = Automata.getAlphabet(); Automata <String> ndfa = new Automata <String>(alphabet); foreach (var state in Automata.getStates()) { } return(ndfa); }
static public Automata <String> generateRandomNdfa() { int num = 0; Automata <String> auto = new Automata <String>(); char[] alfabet = { 'a', 'b' }; RegularExpression b = Tester.generateRandomRegex(alfabet, 5); Console.WriteLine(b.ToString() + Environment.NewLine + "Geeft:"); b.regexToNDFA(ref num, ref auto); return(auto); }
//For a set of states public SortedSet <String> findMultipleAccessible(Automata <String> Automata, char letter, SortedSet <String> states) { SortedSet <String> foundStates = new SortedSet <String>(); foreach (var state in states) { foreach (var item in Automata.getToStates(state, letter)) { if (!foundStates.Contains(item)) { foundStates.Add(item); } } } return(foundStates); }
static public void BevatWoord() { Console.Clear(); Console.WriteLine("1. Bevat woord"); Automata <string> a = new Automata <string>(); AutomataConverter c = new AutomataConverter(); var ans = Console.ReadLine(); int choice = 0; if (int.TryParse(ans, out choice)) { switch (choice) { case 1: Console.Clear(); Console.WriteLine("De volgende NDFA/DFA is gegenereerd:"); a = c.renameStates(Tester.generateRandomNdfa()); // a.printTransitions(); Tester.generateAutomataImage(a); Console.WriteLine("Geef een string mee en kijk of hij word geaccepteerd. (BV: aaabaabaaa)"); var input = Console.ReadLine(); if (a.isStringAccepted(input)) { Console.WriteLine("De string word geaccepteerd."); } else { Console.WriteLine("De string word NIET geaccepteerd."); } ResetToMenu(); break; default: Console.WriteLine("Deze optie is niet beschikbaar." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); break; } } else { Console.WriteLine("Vul alstublieft het nummer van de keuze in." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); } }
public Automata <String> NDFAToDFA(Automata <String> Automata) { SortedSet <char> alphabet = Automata.getAlphabet(); Automata <String> dfa = new Automata <String>(alphabet); foreach (String state in Automata.getStates()) { foreach (var letter in alphabet) { foreach (var item in Automata.getToStates(state, letter)) { Console.WriteLine("State: " + state + " met Letter: " + letter + " kan ik hier komen: " + item); } } } return(dfa); }
//For a single state public SortedSet <String> findStartState(Automata <String> Automata) { SortedSet <String> foundStates = new SortedSet <String>(); foreach (var start in Automata.getStartStates()) { foundStates.Add(start); foreach (var item in Automata.getToStates(start, '$')) { if (!foundStates.Contains(item)) { foundStates.Add(item); } } } return(foundStates); }
// public Automata<String> reverseAutomata(Automata<String> automaton) // { // Automata<String> reversedAutomaton = new Automata<String>(automaton.getAlphabet()); // // foreach (Transition<String> transition in automaton.getTransitions()) // { // transition.reverseFromToState(); // reversedAutomaton.addTransition(transition); // } // // foreach (String state in automaton.getStartStates()) { // reversedAutomaton.defineAsStartState(state); // } // // foreach (String state in automaton.getFinalStates()) { // reversedAutomaton.defineAsFinalState(state); // } // reversedAutomaton.printTransitions(); // // reversedAutomaton = inverseAutomata(reversedAutomaton); // // reversedAutomaton.printTransitions(); // return reversedAutomaton; // } public Automata <String> reverseAutomata(Automata <String> automaton) { Automata <String> reversedAutomaton = new Automata <string>(automaton.getAlphabet()); foreach (Transition <String> transition in automaton.getTransitions()) { reversedAutomaton.addTransition(new Transition <String> (transition.getToState(), transition.getSymbol(), transition.getFromState())); } foreach (String state in automaton.getFinalStates()) { reversedAutomaton.defineAsStartState(state); } foreach (String state in automaton.getStartStates()) { reversedAutomaton.defineAsFinalState(state); } return(reversedAutomaton); }
static public void RegExToNdfaDfa() { Console.Clear(); Console.WriteLine("1. RegEx -> NDFA"); var ans = Console.ReadLine(); int choice = 0; Automata <String> auto = new Automata <String>(); int num = 0; if (int.TryParse(ans, out choice)) { switch (choice) { case 1: Console.Clear(); char[] alfabet = { 'a', 'b', 'c' }; RegularExpression b = Tester.generateRandomRegex(alfabet, 5); Console.WriteLine(b.ToString() + Environment.NewLine + "Geeft:"); b.regexToNDFA(ref num, ref auto); auto.printTransitions(); Tester.generateAutomataImage(auto); ResetToMenu(); break; default: Console.WriteLine("Deze optie is niet beschikbaar." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); break; } } else { Console.WriteLine("Vul alstublieft het nummer van de keuze in." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); } }
static public Automata <String> TestDFA2() { char [] alphabet = { 'a', 'b' }; Automata <String> m = new Automata <String>(alphabet); m.addTransition(new Transition <String> ("q0", 'a', "q2")); m.addTransition(new Transition <String> ("q0", 'b', "q3")); m.addTransition(new Transition <String> ("q1", 'a', "q3")); m.addTransition(new Transition <String> ("q1", 'b', "q2")); m.addTransition(new Transition <String> ("q2", 'a', "q0")); m.addTransition(new Transition <String> ("q2", 'b', "q4")); m.addTransition(new Transition <String> ("q3", 'a', "q1")); m.addTransition(new Transition <String> ("q3", 'b', "q5")); m.addTransition(new Transition <String> ("q4", 'a', "q6")); m.addTransition(new Transition <String> ("q4", 'b', "q5")); m.addTransition(new Transition <String> ("q5", 'a', "q2")); m.addTransition(new Transition <String> ("q5", 'b', "q0")); m.addTransition(new Transition <String> ("q6", 'a', "q4")); m.addTransition(new Transition <String> ("q6", 'b', "q0")); // only on start state in a dfa: m.defineAsStartState("q0"); // final states: m.defineAsFinalState("q1"); m.defineAsFinalState("q3"); m.defineAsFinalState("q4"); m.defineAsFinalState("q6"); return(m); }
static public void NdfaToDfa() { Console.Clear(); Console.WriteLine("1. NDFA -> DFA (zonder Epsilon)" + Environment.NewLine + "2. NDFA -> DFA (met Epsilon)" + Environment.NewLine + "3. NDFA -> DFA (Random NDFA)"); var ans = Console.ReadLine(); AutomataConverter c = new AutomataConverter(); int choice = 0; if (int.TryParse(ans, out choice)) { switch (choice) { case 1: Console.Clear(); Automata <string> a = Tester.TestNDFA(); Console.WriteLine("De volgende NDFA:"); a.printTransitions(); Tester.generateAutomataImage(a); Console.WriteLine("is deze DFA: (Druk op een knop)"); Console.ReadLine(); c.NDFAToDFA(a).printTransitions(); Tester.generateAutomataImage(c.NDFAToDFA(a)); ResetToMenu(); break; case 2: Console.Clear(); Automata <string> b = Tester.TestNDFA2(); Console.WriteLine("De volgende NDFA:"); b.printTransitions(); Tester.generateAutomataImage(b); Console.WriteLine("is deze DFA: (Druk op een knop)"); Console.ReadLine(); c.NDFAToDFA(b).printTransitions(); Tester.generateAutomataImage(c.NDFAToDFA(b)); ResetToMenu(); break; case 3: Console.Clear(); Automata <string> d = Tester.generateRandomNdfa(); Console.WriteLine("De volgende NDFA:"); d.printTransitions(); Tester.generateAutomataImage(d); Console.WriteLine("is deze DFA: (Druk op een knop)"); Console.ReadLine(); c.NDFAToDFA(d).printTransitions(); Tester.generateAutomataImage(c.NDFAToDFA(d)); ResetToMenu(); break; default: Console.WriteLine("Deze optie is niet beschikbaar." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); break; } } else { Console.WriteLine("Vul alstublieft het nummer van de keuze in." + Environment.NewLine + "Druk op een knop om terug te gaan"); ResetToMenu(); } }
static public void generateAutomataImage(Automata <String> automata, string output_file = "", string fileformat = "svg") { string executable_file = ""; string file_opener = ""; bool no_open = false; if (output_file != "") { no_open = true; } bool windows = false; int p = (int)Environment.OSVersion.Platform; if ((p == 4) || (p == 6) || (p == 128)) //UNIX { if (output_file == "") { if (InputOutput.IsRunningOnMac()) { output_file = Path.GetTempPath() + "generatedAutomata." + fileformat; } else { output_file = "/tmp/tmp." + fileformat; } } executable_file = "dot"; if (InputOutput.IsRunningOnMac()) { file_opener = "open"; } else { file_opener = "xdg-open"; } } else { windows = true; if (output_file == "") { output_file = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); output_file = output_file + "\\img." + fileformat; } executable_file = "C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe"; } string dotinfo = automata.printGraphviz(); try { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; startInfo.FileName = executable_file; startInfo.RedirectStandardInput = true; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.Arguments = " -T" + fileformat + " -o " + output_file; Process exeProcess = new Process(); exeProcess.StartInfo = startInfo; exeProcess.Start(); //exeProcess.WaitForInputIdle(); StreamWriter writer = exeProcess.StandardInput; writer.Write(dotinfo); writer.Flush(); writer.WriteLine(); if (!windows) { exeProcess.WaitForInputIdle(); } writer.Close(); exeProcess.WaitForExit(); if (!no_open) { if (!windows) { Process openProcess = new Process(); openProcess.StartInfo.FileName = file_opener; openProcess.StartInfo.Arguments = output_file; openProcess.Start(); } else { Process.Start(output_file); } } } catch { Console.Write("Random exception"); } }
public Automata <String> NDFAToDFA(Automata <String> Automaton) { SortedSet <char> alphabet = Automaton.getAlphabet(); Automata <String> dfa = new Automata <String>(alphabet); Dictionary <Transition <String>, SortedSet <String> > transitionsDict = new Dictionary <Transition <String>, SortedSet <String> >(); dfa.defineAsStartState(prettyPrint(findStartState(Automaton))); //Set startstates for dfa foreach (char letter in alphabet) //Check all states after the startstate with all the letters in the alphabet { if (findMultipleAccessible(Automaton, letter, findStartState(Automaton)).Count() == 0) { dfa.addTransition(new Transition <string>(prettyPrint(findStartState(Automaton)), letter, "Ø")); } else { SortedSet <String> toStates = findMultipleAccessible(Automaton, letter, findStartState(Automaton)); Transition <String> newTransition = new Transition <string>(prettyPrint(findStartState(Automaton)), letter, prettyPrint(toStates)); transitionsDict.Add(newTransition, toStates); dfa.addTransition(newTransition); } } int tempTransitionsNumber = 0; Dictionary <Transition <String>, SortedSet <String> > tempTransitionsDict = new Dictionary <Transition <String>, SortedSet <String> >(); while (dfa.getTransistionsNumber() != tempTransitionsNumber) { tempTransitionsNumber = dfa.getTransistionsNumber(); foreach (KeyValuePair <Transition <String>, SortedSet <String> > pair in transitionsDict) { foreach (char letter in alphabet) { if (findMultipleAccessible(Automaton, letter, pair.Value).Count() == 0) { if (dfa.getTransitions().Contains(new Transition <string>(pair.Key.getToState(), letter, "Ø")) == false) { dfa.addTransition(new Transition <string>(pair.Key.getToState(), letter, "Ø")); tempTransitionsDict.Add(new Transition <string>(pair.Key.getToState(), letter, "Ø"), findMultipleAccessible(Automaton, letter, pair.Value)); } } else { SortedSet <String> toStates = findMultipleAccessible(Automaton, letter, pair.Value); Transition <String> newTransition = new Transition <string>(pair.Key.getToState(), letter, prettyPrint(toStates)); if (dfa.getTransitions().Contains(new Transition <string>(pair.Key.getToState(), letter, prettyPrint(toStates))) == false) { tempTransitionsDict.Add(newTransition, toStates); dfa.addTransition(newTransition); } } } } transitionsDict = transitionsDict.Union(tempTransitionsDict).ToDictionary(k => k.Key, v => v.Value); } SortedSet <String> tempListFinal = new SortedSet <String>(); foreach (String stat in dfa.getStates()) { foreach (String endState in Automaton.getFinalStates()) { if (stat.Contains(endState)) { tempListFinal.Add(stat); } } } foreach (String finState in tempListFinal) { dfa.defineAsFinalState(finState); } return(dfa); }
static public Automata <String> testMinimalization(Automata <String> automata) { AutomataMinimalization m = new AutomataMinimalization(); return(m.Minimization(automata)); }
public void regexToNDFA(ref int depth, ref Automata <String> automata, string prevstate = null, string nextstate = null) { //THOMPSON if (depth == 0) { prevstate = "1"; automata.defineAsStartState(prevstate); depth += 2; } //depth += 1; bool nonext = false; switch (operate) { case Operator.OR: automata.addTransition(new Transition <string>(prevstate, depth.ToString())); String splitstate = depth.ToString(); depth++; String lnewstate = splitstate; if (left.operate == Operator.ONE) { foreach (char s in left.terminal) { automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString())); automata.addToAlphabet(s); lnewstate = depth.ToString(); //NOT PROPERLY IMPLEMENTED BEHAVIOR depth++; } } else { automata.addTransition(new Transition <string>(lnewstate, depth.ToString())); //print = print + left.terminal;//left.regexToNDFA(depth, automata); lnewstate = depth.ToString(); depth++; String loldstate2 = depth.ToString(); depth++; left.regexToNDFA(ref depth, ref automata, lnewstate, loldstate2); lnewstate = loldstate2; //depth++; } automata.addTransition(new Transition <string>(prevstate, depth.ToString())); splitstate = depth.ToString(); depth++; depth++; String rnewstate = splitstate; if (right.operate == Operator.ONE) { foreach (char s in right.terminal) { automata.addTransition(new Transition <string>(rnewstate, s, depth.ToString())); automata.addToAlphabet(s); rnewstate = depth.ToString(); depth++; } } else { automata.addTransition(new Transition <string>(rnewstate, depth.ToString())); //print = print + left.terminal;//left.regexToNDFA(depth, automata); rnewstate = depth.ToString(); //NOT PROPERLY IMPLEMENTED BEHAVIOR depth++; String roldstate = depth.ToString(); depth++; right.regexToNDFA(ref depth, ref automata, rnewstate, roldstate); rnewstate = roldstate; //depth++; } if (nextstate == null) { automata.addTransition(new Transition <string>(lnewstate, depth.ToString())); automata.addTransition(new Transition <string>(rnewstate, depth.ToString())); automata.defineAsFinalState(depth.ToString()); depth++; } else { automata.addTransition(new Transition <string>(lnewstate, nextstate)); //WRONG automata.addTransition(new Transition <string>(rnewstate, nextstate)); } break; case Operator.DOT: if (nextstate == null) { nextstate = depth.ToString(); depth++; nonext = true; } depth++; String tmpstate = depth.ToString(); if (left.operate == Operator.ONE) { lnewstate = prevstate; depth++; foreach (char s in left.terminal) { automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString())); automata.addToAlphabet(s); lnewstate = depth.ToString(); depth++; } automata.addTransition(new Transition <string>(lnewstate, tmpstate)); } else { left.regexToNDFA(ref depth, ref automata, prevstate, tmpstate); } if (right.operate == Operator.ONE) { lnewstate = tmpstate; depth++; foreach (char s in right.terminal) { automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString())); automata.addToAlphabet(s); lnewstate = depth.ToString(); depth++; } automata.addTransition(new Transition <string>(lnewstate, nextstate)); } else { right.regexToNDFA(ref depth, ref automata, tmpstate, nextstate); } if (nonext) { automata.defineAsFinalState(nextstate); } break; case Operator.ONE: //NOT REALLY IMPLEMENTED depth++; lnewstate = prevstate; foreach (char s in terminal) { automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString())); automata.addToAlphabet(s); lnewstate = depth.ToString(); depth++; } if (nextstate == null) { automata.defineAsFinalState((depth - 1).ToString()); } break; case Operator.PLUS: if (nextstate == null) { nextstate = depth.ToString(); depth++; nonext = true; } depth++; automata.addTransition(new Transition <string>(prevstate, depth.ToString())); lnewstate = depth.ToString(); String loldstate = depth.ToString(); depth++; if (left.operate == Operator.ONE) { foreach (char s in left.terminal) { automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString())); automata.addToAlphabet(s); lnewstate = depth.ToString(); depth++; } automata.addTransition(new Transition <string>(lnewstate, loldstate)); automata.addTransition(new Transition <string>(lnewstate, depth.ToString())); rnewstate = depth.ToString(); } else { rnewstate = depth.ToString(); depth++; left.regexToNDFA(ref depth, ref automata, lnewstate, rnewstate); //NOT CHECKED, PROBABLY WRONG //automata.addTransition(new Transition<string>(loldstate, depth.ToString())); automata.addTransition(new Transition <string>(rnewstate, lnewstate)); automata.addTransition(new Transition <string>(rnewstate, depth.ToString())); rnewstate = depth.ToString(); } if (!nonext) { automata.addTransition(new Transition <string>(depth.ToString(), nextstate)); } else { depth++; automata.addTransition(new Transition <string>(rnewstate, depth.ToString())); automata.defineAsFinalState(depth.ToString()); } depth++; break; case Operator.STAR: if (depth.ToString() == nextstate) { depth++; } if (nextstate == null) { nextstate = depth.ToString(); depth++; nonext = true; } automata.addTransition(new Transition <string>(prevstate, depth.ToString())); lnewstate = depth.ToString(); rnewstate = lnewstate; depth++; if (left.operate == Operator.ONE) { foreach (char s in left.terminal) { automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString())); automata.addToAlphabet(s); lnewstate = depth.ToString(); //NOT PROPERLY IMPLEMENTED BEHAVIOR depth++; } automata.addTransition(new Transition <string>(prevstate, nextstate)); automata.addTransition(new Transition <string>(lnewstate, rnewstate)); } else { automata.addTransition(new Transition <string>(lnewstate, depth.ToString())); //print = print + left.terminal;//left.regexToNDFA(depth, automata); loldstate = lnewstate; lnewstate = depth.ToString(); depth++; String futurestate = depth.ToString(); depth++; left.regexToNDFA(ref depth, ref automata, lnewstate, futurestate); depth++; automata.addTransition(new Transition <string>(futurestate, depth.ToString())); automata.addTransition(new Transition <string>(futurestate, lnewstate)); automata.addTransition(new Transition <string>(loldstate, depth.ToString())); lnewstate = depth.ToString(); depth++; } if (!nonext) { automata.addTransition(new Transition <string>(lnewstate, nextstate)); } else { automata.addTransition(new Transition <string>(lnewstate, depth.ToString())); automata.defineAsFinalState(depth.ToString()); } break; } }
public static Automata <String> testConverter(Automata <String> ndfa) { AutomataConverter c = new AutomataConverter(); return(c.NDFAToDFA(ndfa)); }