//copies the nodes from the NDFA to local list private static void CopyNodes(NDFA ndfa) { foreach (Node item in ndfa.Nodes) { Nodes.Add(new Node(item.name, item.nodeType)); } foreach (Node node in Nodes) { foreach (Node item in ndfa.Nodes) { if (node.name.Equals(item.name)) { foreach (Connection connection in item.connections) { foreach (Node connectednode in Nodes) { if (connection.node.name.Equals(connectednode.name)) { node.AddConnection(new Connection(connection.letter, connectednode)); } } } } } } }
public void BeginsWith(string s, int n) { string alphabet = new String(s.Distinct().ToArray()); alphabet = new String(alphabet.Where(Char.IsLetter).ToArray()); List <string> strings = GenerateStrings.GenerateString(n, alphabet); Console.WriteLine("-------- Generated --------"); List <Node> BeginsWithBABAANodes = GenerateDFA.GenerateDFABeginsWith(s, alphabet); Node beginsstartnode = null; foreach (Node item in BeginsWithBABAANodes) { if (item.nodeType == NodeType.StartNode) { beginsstartnode = item; } } DFA GEN_BeginsWithBABAA = new DFA(beginsstartnode, BeginsWithBABAANodes); foreach (string item in strings) { Console.WriteLine(GEN_BeginsWithBABAA.Check(item)); } List <String> correctWordsbw = GEN_BeginsWithBABAA.geefTaalTotN(n, alphabet); Console.WriteLine("-------- Correct words Begins With " + s + " --------"); foreach (String item in correctWordsbw) { Console.WriteLine(item); } List <String> inCorrectWordsbw = GEN_BeginsWithBABAA.geefFoutieveTaalTotN(n, alphabet); Console.WriteLine("-------- incorrect words Begins With " + s + " --------"); foreach (String item in inCorrectWordsbw) { Console.WriteLine(item); } CreateGraph(GEN_BeginsWithBABAA.Nodes, "GEN_DFABW" + s); Console.WriteLine("-------- Begins With (reversed) --------"); NDFA reverse = DFAReverse.Reverse2(GEN_BeginsWithBABAA); foreach (string item in strings) { Console.WriteLine(reverse.Check(item)); } CreateGraph(reverse.Nodes, "GEN_reversedDFABW" + s); Console.WriteLine("-------- Begins With (reversed -> DFA) --------"); DFA dfa = NDFAtoDFA.ToDFA2(reverse); foreach (string item in strings) { Console.WriteLine(dfa.Check(item)); } CreateGraph(dfa.Nodes, "GEN_reverseddfaDFABW" + s); }
//this function handles the conversion from DFA to NDFA in the form of a reverse public static NDFA Reverse2(DFA orignal) { List <Node> nodes = new List <Node>(); List <Node> startnodes = new List <Node>(); //copies the nodes and swap the start/end nodes foreach (Node item in orignal.Nodes) { NodeType type = NodeType.NormalNode; if (item.nodeType == NodeType.StartNode) { type = NodeType.EndNode; } else if (item.nodeType == NodeType.EndNode) { type = NodeType.StartNode; } Node newnode = new Node(item.name, type); nodes.Add(newnode); if (newnode.nodeType == NodeType.StartNode) { startnodes.Add(newnode); } } //this reverses the connections from the original nodes foreach (Node node in orignal.Nodes) { foreach (Connection connection in node.connections) { foreach (Node newnode in nodes) { if (newnode.name.Equals(connection.node.name)) { foreach (Node fromnode in nodes) { if (fromnode.name.Equals(node.name)) { newnode.AddConnection(new Connection(connection.letter, fromnode)); } } } } } } //the NDFA that is reversed NDFA ndfa = new NDFA(startnodes, nodes); return(ndfa); }
//the functions that handles the conversion from NDFA to DFA public static DFA ToDFA2(NDFA ndfa) { Nodes.Clear(); letters.Clear(); List <Row> myList = GenerateTable(ndfa); if (Fuik == null) { Fuik = new Node("q999999999", NodeType.NormalNode); //TODO make connections dynamic foreach (var item in letters) { Fuik.connections.Add(new Connection(item, Fuik)); } Fuik.connections = new List <Connection> { new Connection('a', Fuik), new Connection('b', Fuik) }; } CopyNodes(ndfa); Filltable(myList, Nodes); return(ConvertTableToDFA2(myList)); }
//Generates current table of the NDFA private static List <Row> GenerateTable(NDFA ndfa) { List <Row> myList = new List <Row>(); //adds all the diffent kind of letters to local list foreach (Node item in ndfa.Nodes) { foreach (Connection connection in item.connections) { if (!letters.Contains(connection.letter) && !connection.letter.Equals('ϵ')) { letters.Add(connection.letter); } } } //for every node in the copied nodes foreach (Node item in ndfa.Nodes) { Row row = new Row(item.name); //makes a column for every letter foreach (char letter in letters) { row.row.Add(new List <Node>()); } //for every connection in the node foreach (Connection connection in item.connections) { //for every letter for (int i = 0; i < letters.Count; i++) { //if the node has a connection with the corresponding letter if (connection.letter.Equals(letters[i])) { //add node row.row[i].Add(connection.node); //check for epsilon connections foreach (Connection connection1 in connection.node.connections) { //check for any epsilon nodes, if so add all the nodes that are connected through epsilons if (connection1.letter.Equals('ϵ')) { List <Node> secondlist = EndOnNodeWithEpsilon(connection1.node); foreach (Node epsilonnode in secondlist) { if (!row.row[i].Contains(epsilonnode)) { row.row[i].Add(epsilonnode); } } } } } //if epsilon connection, add the node and check that one for epsilons as well else if (connection.letter.Equals('ϵ')) { List <List <Node> > results = EpsilonConnection(connection.node); for (int i2 = 0; i2 < letters.Count; i2++) { foreach (Node node in results[i2]) { if (!row.row[i2].Contains(node)) { row.row[i2].Add(node); } } } } } } //add row to the table myList.Add(row); } //return table return(myList); }
public void Regexparse(string s) { Console.WriteLine("entered string: " + s); RegexTester tester; try { tester = new RegexTester(s); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } string answer = new String(s.Distinct().ToArray()); answer = new String(answer.Where(Char.IsLetter).ToArray()); List <string> strings = GenerateStrings.GenerateString(5, answer); RegExp exp = RegexParser.parse(s); List <Node> ndfa = Thompson.CreateAutomaat(exp); NDFA NDFAregularexpression = new NDFA(new List <Node>() { ndfa[0] }, ndfa); List <string> regexcorrect = tester.geefTaalTotN(5, answer); List <string> regexincorrect = tester.geefFoutieveTaalTotN(5, answer); CreateGraph(NDFAregularexpression.Nodes, "REGEX_NDFA"); DFA DFAregularexpression = NDFAtoDFA.ToDFA2(NDFAregularexpression); CreateGraph(DFAregularexpression.Nodes, "REGEX_DFA"); NDFA Reverse = DFAReverse.Reverse2(DFAregularexpression); CreateGraph(Reverse.Nodes, "REGEX_DFA_Reverse"); Console.WriteLine("-------- Correct words Contains --------"); foreach (String item in regexcorrect) { Console.WriteLine(item); } Console.WriteLine("-------- incorrect words --------"); foreach (String item in regexincorrect) { Console.WriteLine(item); } Console.WriteLine("-------- regex/ndfa/dfa/ndfa Reversed --------"); foreach (string item in strings) { Console.WriteLine(tester.Check(item)); Console.WriteLine(NDFAregularexpression.Check(item)); Console.WriteLine(DFAregularexpression.Check(item)); Console.WriteLine("reverse " + Reverse.Check(item)); } }
public void HardcodedExamples() { List <string> strings = GenerateStrings.GenerateString(5, "ab"); #region DFA //------------------------------- DFA ------------------------------- Console.WriteLine("---------- DFA -------------"); Console.WriteLine("------- hardcoded ----------"); Console.WriteLine("---- begins with babaa -----"); List <Node> DFA_BWB_ABAA_Nodes = new List <Node>() { new Node("q0", NodeType.StartNode), new Node("q1", NodeType.NormalNode), new Node("q2", NodeType.NormalNode), new Node("q3", NodeType.NormalNode), new Node("q4", NodeType.NormalNode), new Node("q5", NodeType.EndNode), new Node("q6", NodeType.NormalNode) }; DFA_BWB_ABAA_Nodes[0].AddConnections(new List <Connection>() { new Connection('b', DFA_BWB_ABAA_Nodes[1]), new Connection('a', DFA_BWB_ABAA_Nodes[6]) }); DFA_BWB_ABAA_Nodes[1].AddConnections(new List <Connection>() { new Connection('b', DFA_BWB_ABAA_Nodes[6]), new Connection('a', DFA_BWB_ABAA_Nodes[2]) }); DFA_BWB_ABAA_Nodes[2].AddConnections(new List <Connection>() { new Connection('b', DFA_BWB_ABAA_Nodes[3]), new Connection('a', DFA_BWB_ABAA_Nodes[6]) }); DFA_BWB_ABAA_Nodes[3].AddConnections(new List <Connection>() { new Connection('b', DFA_BWB_ABAA_Nodes[6]), new Connection('a', DFA_BWB_ABAA_Nodes[4]) }); DFA_BWB_ABAA_Nodes[4].AddConnections(new List <Connection>() { new Connection('b', DFA_BWB_ABAA_Nodes[6]), new Connection('a', DFA_BWB_ABAA_Nodes[5]) }); DFA_BWB_ABAA_Nodes[5].AddConnections(new List <Connection>() { new Connection('b', DFA_BWB_ABAA_Nodes[5]), new Connection('a', DFA_BWB_ABAA_Nodes[5]) }); DFA_BWB_ABAA_Nodes[6].AddConnections(new List <Connection>() { new Connection('b', DFA_BWB_ABAA_Nodes[6]), new Connection('a', DFA_BWB_ABAA_Nodes[6]) }); DFA BeginsWithBABAA = new DFA(DFA_BWB_ABAA_Nodes[0], DFA_BWB_ABAA_Nodes); foreach (string item in strings) { Console.WriteLine(BeginsWithBABAA.Check(item)); } CreateGraph(DFA_BWB_ABAA_Nodes, "DFA_BWBABAA"); Console.WriteLine("---- starts with abb or ends with baab -----"); List <Node> DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes = new List <Node>() { new Node("q0", NodeType.StartNode), new Node("q1", NodeType.NormalNode), new Node("q2", NodeType.NormalNode), new Node("q3", NodeType.EndNode), new Node("q4", NodeType.NormalNode), new Node("q5", NodeType.NormalNode), new Node("q6", NodeType.NormalNode), new Node("q7", NodeType.NormalNode), new Node("q8", NodeType.EndNode) }; DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[0].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[1]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[4]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[1].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[6]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[2]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[2].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[6]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[3]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[3].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[3]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[3]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[4].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[4]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[5]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[5].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[6]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[5]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[6].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[7]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[5]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[7].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[4]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[8]) }); DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[8].AddConnections(new List <Connection>() { new Connection('a', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[4]), new Connection('b', DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[5]) }); DFA StartsWithABBorEndsWithBAAB = new DFA(DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes[0], DFA_STARTW_ABB_OR_ENDSW_BAAB_Nodes); foreach (string item in strings) { Console.WriteLine(StartsWithABBorEndsWithBAAB.Check(item)); } CreateGraph(StartsWithABBorEndsWithBAAB.Nodes, "DFA_STARTWABBORENDSWBAAB"); #endregion #region HopCroft List <Node> HopCraftNodes = new List <Node>() { new Node("q0", NodeType.StartNode), new Node("q1", NodeType.NormalNode), new Node("q2", NodeType.EndNode), new Node("q3", NodeType.NormalNode), new Node("q4", NodeType.NormalNode) }; HopCraftNodes[0].AddConnections(new List <Connection>() { new Connection('a', HopCraftNodes[1]), new Connection('b', HopCraftNodes[2]) }); HopCraftNodes[1].AddConnections(new List <Connection>() { new Connection('a', HopCraftNodes[3]), new Connection('b', HopCraftNodes[0]) }); HopCraftNodes[2].AddConnections(new List <Connection>() { new Connection('a', HopCraftNodes[2]), new Connection('b', HopCraftNodes[4]) }); HopCraftNodes[3].AddConnections(new List <Connection>() { new Connection('a', HopCraftNodes[2]), new Connection('b', HopCraftNodes[1]) }); HopCraftNodes[4].AddConnections(new List <Connection>() { new Connection('a', HopCraftNodes[2]), new Connection('b', HopCraftNodes[1]) }); DFA HopCraft = new DFA(HopCraftNodes[0], HopCraftNodes); HopCraft = HopCroft.MinimizeHopCroft(HopCraft); CreateGraph(HopCraft.Nodes, "DFA_HopCroft"); #endregion #region NDFA //------------------------------- NDFA ------------------------------- Console.WriteLine("---------- NDFA ------------"); Console.WriteLine("---- Contains aa or bb -----"); List <Node> NDFA_C_AAoBB_Nodes = new List <Node>() { new Node("q0", NodeType.StartNode), new Node("q1", NodeType.NormalNode), new Node("q2", NodeType.NormalNode), new Node("q3", NodeType.EndNode), }; NDFA_C_AAoBB_Nodes[0].AddConnections(new List <Connection>() { new Connection('b', NDFA_C_AAoBB_Nodes[0]), new Connection('a', NDFA_C_AAoBB_Nodes[0]), new Connection('b', NDFA_C_AAoBB_Nodes[2]), new Connection('a', NDFA_C_AAoBB_Nodes[1]) }); NDFA_C_AAoBB_Nodes[1].AddConnections(new List <Connection>() { new Connection('a', NDFA_C_AAoBB_Nodes[3]), new Connection('b', NDFA_C_AAoBB_Nodes[0]) }); NDFA_C_AAoBB_Nodes[2].AddConnections(new List <Connection>() { new Connection('b', NDFA_C_AAoBB_Nodes[3]), new Connection('a', NDFA_C_AAoBB_Nodes[0]) }); NDFA_C_AAoBB_Nodes[3].AddConnections(new List <Connection>() { new Connection('b', NDFA_C_AAoBB_Nodes[3]), new Connection('a', NDFA_C_AAoBB_Nodes[3]) }); NDFA ContainsAAorBB = new NDFA(new List <Node>() { NDFA_C_AAoBB_Nodes[0] }, NDFA_C_AAoBB_Nodes); foreach (string item in strings) { Console.WriteLine(ContainsAAorBB.Check(item)); } CreateGraph(NDFA_C_AAoBB_Nodes, "NDFA_CAAoBB"); #endregion #region regex //---------------------------- regex ------------------------------- List <string> regexstrings = GenerateStrings.GenerateString(5, "abc"); Console.WriteLine("---------- regex ------------"); Console.WriteLine("--------- (a|bc)* ----------"); RegexTester regexTester = new RegexTester(@"(a|bc)*"); foreach (string item in regexstrings) { Console.WriteLine(regexTester.Check(item)); } Console.WriteLine("--------- wrong ----------"); List <string> foutievetaal = regexTester.geefFoutieveTaalTotN(5, "abc"); foreach (string item in foutievetaal) { Console.WriteLine(regexTester.Check(item)); } Console.WriteLine("--------- correct ----------"); List <string> correctetaal = regexTester.geefTaalTotN(5, "abc"); foreach (string item in correctetaal) { Console.WriteLine(regexTester.Check(item)); } #endregion #region GenerateBeginsWith Console.WriteLine("-------- Generated --------"); List <Node> BeginsWithBABAANodes = GenerateDFA.GenerateDFABeginsWith("babaa", "ab"); Node beginsstartnode = null; foreach (Node item in BeginsWithBABAANodes) { if (item.nodeType == NodeType.StartNode) { beginsstartnode = item; } } DFA GEN_BeginsWithBABAA = new DFA(beginsstartnode, BeginsWithBABAANodes); foreach (string item in strings) { Console.WriteLine(GEN_BeginsWithBABAA.Check(item)); } List <String> correctWordsbw = GEN_BeginsWithBABAA.geefTaalTotN(5, "ab"); Console.WriteLine("-------- Correct words BeginsWithBABAA --------"); foreach (String s in correctWordsbw) { Console.WriteLine(s); } List <String> inCorrectWordsbw = GEN_BeginsWithBABAA.geefFoutieveTaalTotN(5, "ab"); Console.WriteLine("-------- incorrect words BeginsWithBABAA --------"); foreach (String s in inCorrectWordsbw) { Console.WriteLine(s); } CreateGraph(GEN_BeginsWithBABAA.Nodes, "GEN_DFABWBABAA"); #endregion #region GenerateEndsWith List <Node> EndsWithBABAANodes = GenerateDFA.GenerateDFAEndsWith("aabab", "ab"); Node EndsWithstartnode = null; foreach (Node item in EndsWithBABAANodes) { if (item.nodeType == NodeType.StartNode) { EndsWithstartnode = item; } } DFA GEN_EndsWithBABAA = new DFA(EndsWithstartnode, EndsWithBABAANodes); foreach (string item in strings) { Console.WriteLine(GEN_EndsWithBABAA.Check(item)); } List <String> correctWordsew = GEN_EndsWithBABAA.geefTaalTotN(5, "ab"); Console.WriteLine("-------- Correct words EndsWithAABAB --------"); foreach (String s in correctWordsew) { Console.WriteLine(s); } List <String> inCorrectWordsew = GEN_EndsWithBABAA.geefFoutieveTaalTotN(5, "ab"); Console.WriteLine("-------- incorrect words EndsWithAABAB --------"); foreach (String s in inCorrectWordsew) { Console.WriteLine(s); } CreateGraph(GEN_EndsWithBABAA.Nodes, "GEN_DFAEWAABAB"); #endregion #region Contains List <Node> ContainsBABAANodes = GenerateDFA.GenerateDFAContains("bab", "ab"); Node Containsstartnode = null; foreach (Node item in ContainsBABAANodes) { if (item.nodeType == NodeType.StartNode) { Containsstartnode = item; } } DFA GEN_ContainsBABAA = new DFA(Containsstartnode, ContainsBABAANodes); foreach (string item in strings) { Console.WriteLine(GEN_ContainsBABAA.Check(item)); } List <String> correctWordsc = GEN_ContainsBABAA.geefTaalTotN(5, "ab"); Console.WriteLine("-------- Correct words ContainsBAB --------"); foreach (String s in correctWordsc) { Console.WriteLine(s); } List <String> inCorrectWordsc = GEN_ContainsBABAA.geefFoutieveTaalTotN(5, "ab"); Console.WriteLine("-------- incorrect words ContainsBAB --------"); foreach (String s in inCorrectWordsc) { Console.WriteLine(s); } CreateGraph(GEN_ContainsBABAA.Nodes, "GEN_DFAEWBAB"); #endregion #region Regex -> NDFA Console.WriteLine("---------- Regular expression tester with Regex and Thompson ------------"); Console.WriteLine("---------- (a|bc)* -----------"); regexTester = new RegexTester(@"(a|bc)*"); RegExp reg = new RegExp("a").or(new RegExp("b").dot(new RegExp("c"))).star(); List <Node> ndfa = Thompson.CreateAutomaat(reg); NDFA NDFAregularexpression2 = new NDFA(new List <Node>() { ndfa[0] }, ndfa); foreach (string item in strings) { Console.WriteLine(NDFAregularexpression2.Check(item)); Console.WriteLine("Regex test: " + regexTester.Check(item)); } CreateGraph(NDFAregularexpression2.Nodes, "NDFA_RegexThompsonToNDFA"); #endregion #region NDFAtoDFA //--------------------------- NDFA -> DFA --------------------------- Console.WriteLine("---- test NDFA to DFA -----"); Console.WriteLine("---------- NDFA -----------"); List <Node> NDFATODFA = new List <Node>() { new Node("q1", NodeType.StartNode), new Node("q2", NodeType.EndNode), new Node("q3", NodeType.EndNode), new Node("q4", NodeType.NormalNode), new Node("q5", NodeType.NormalNode) }; NDFATODFA[0].AddConnections(new List <Connection>() { new Connection('b', NDFATODFA[1]), new Connection('a', NDFATODFA[1]), new Connection('b', NDFATODFA[2]), }); NDFATODFA[1].AddConnections(new List <Connection>() { new Connection('a', NDFATODFA[2]), new Connection('ϵ', NDFATODFA[3]), new Connection('b', NDFATODFA[3]) }); NDFATODFA[2].AddConnections(new List <Connection>() { new Connection('a', NDFATODFA[1]) }); NDFATODFA[3].AddConnections(new List <Connection>() { new Connection('a', NDFATODFA[4]), new Connection('a', NDFATODFA[1]) }); NDFATODFA[4].AddConnections(new List <Connection>() { new Connection('b', NDFATODFA[4]), new Connection('ϵ', NDFATODFA[2]) }); NDFA TESTNDFATODFA = new NDFA(new List <Node>() { NDFATODFA[0] }, NDFATODFA); CreateGraph(TESTNDFATODFA.Nodes, "NDFA_NDFAtoDFA"); foreach (string item in strings) { //Console.WriteLine(TESTNDFATODFA.Check(item)); } Console.WriteLine("----------- DFA -----------"); DFA TESTDFA = NDFAtoDFA.ToDFA2(TESTNDFATODFA); CreateGraph(TESTDFA.Nodes, "DFA_NDFAtoDFA"); foreach (string item in strings) { Console.WriteLine(TESTNDFATODFA.Check(item)); Console.WriteLine(TESTDFA.Check(item)); } #endregion #region Reverse Console.WriteLine("---- (NDFA) reverse begins with babaa -----"); NDFA reversedBABAA = DFAReverse.Reverse2(BeginsWithBABAA); foreach (string item in strings) { Console.WriteLine(reversedBABAA.Check(item)); } CreateGraph(reversedBABAA.Nodes, "ReversedBABAA"); #endregion }