public void traverse(nfa n) { // var comparer = new Comparer // n.states.Sort(); // IComparer<state> a= new sComparer(); // n.states.Sort(a); richTextBox1.Text = ""; foreach (state i in n.states) { richTextBox1.Text += "State " + i.name + "\n"; foreach (char c in i.trans.Keys) { richTextBox1.Text += "d(" + i.name + "," + c.ToString() + ")->" + i.trans[c].name + "\n"; } richTextBox1.Text += "e-closure : "; foreach (state j in i.eps) { richTextBox1.Text += j.name + " , "; } richTextBox1.Text += "\n_______________\n"; } richTextBox1.Text += "\nInitial State: " + n.start.name + "\nFinal State: " + n.end.name; List <state> dfa1 = eps_close(n.start); dfa1 = new HashSet <state>(dfa1).ToList <state>(); String ans = ""; foreach (state x in dfa1) { ans += x.name + "_"; } //MessageBox.Show(ans); convert_dfa(n); }
public void convert_dfa(nfa n) { dfa d1 = new dfa(); List <state> dfst = new HashSet <state>(eps_close(n.start)).ToList <state>(); d1.start = dfst; bool a = d1.states.Add(d1.start); //bool b = d1.states.Add(eps_close(n.start)); SortedSet <char> symbols = new SortedSet <char>(); foreach (state i in n.states) { foreach (char c in i.trans.Keys) { symbols.Add(c); } }/* * String ans = ""; * foreach (char x in symbols.ToList<char>()) * ans += x + "_"; * MessageBox.Show(ans);*/ //MessageBox.Show(a.ToString() + "__" + b.ToString()); if (dfst.Contains(n.end)) { d1.end.Add(dfst); } String x = ""; //int ctr = 0; //bool b = true; List <state> loop = new List <state>(); //::::PROBLEM IDENTIFIED REMEMBER TO PASS ALL GIVEN STATES THROUGH IT do not update dfst inside foreach loop like dat:::: //noe recursion noe stack Stack <List <state> > steck = new Stack <List <state> >(); steck.Push(dfst); while (steck.Count > 0) { // bool r = false; foreach (char c in symbols) { String l = STR(dfst); loop = newdfastate(c, dfst); foreach (var d in loop.ToList()) { loop.AddRange(eps_close(d)); } loop = new HashSet <state>(loop).ToList <state>(); richTextBox2.Text += "d( " + l + " , " + c + " )->" + STR(loop) + "\n"; if (loop.Contains(n.end)) { d1.end.Add(loop); } bool ch = d1.states.Add(loop); if (ch) { steck.Push(loop); // foreach (var k in dfst) // richTextBox2.Text += k.name + "_"; // richTextBox2.Text += "\n"; } // r = r | ch; } dfst = steck.Pop(); richTextBox2.Text += "_________\n"; //b=b&r; } // String x=""; //richTextBox2.Text = x; //MessageBox.Show(d1.states.Count.ToString()); x = "\t"; String[] states = new String[d1.states.Count]; String[,] graph = new String[states.Length, states.Length]; for (int g1 = 0; g1 < states.Length; g1++) { for (int g2 = 0; g2 < states.Length; g2++) { graph[g1, g2] = "_"; } } String file = ""; int j = 0; foreach (var st in d1.states) { states[j++] = STR(st); x += states[j - 1] + "\t"; file += j.ToString() + ","; } var rem = new List <state>(); file += "\n"; x += "\n"; int ind1 = 0, ind2; dfst = d1.start; bool flag = true; foreach (char c in symbols) { flag = true; foreach (var v in d1.states) { rem = v; dfst = newdfastate(c, v); foreach (var d in dfst.ToList()) { dfst.AddRange(eps_close(d)); } dfst = new HashSet <state>(dfst).ToList <state>(); ind2 = Array.IndexOf(states, STR(dfst)); if (ind2 == -1) { flag = false; //MessageBox.Show(STR(dfst)); } else { if (ind1 != ind2) { graph[ind1, ind2] += c.ToString(); } else if (flag == true) { graph[ind1, ind2] += c.ToString(); } ind1 = ind2;//er jonne last er sobai * hoe jache; } } } //MessageBox.Show(STR(newdfastate('b', rem)) + "__" + STR(rem)); for (int g1 = 0; g1 < states.Length; g1++) { x += states[g1] + "\t"; //file += g1.ToString()+","; for (int g2 = 0; g2 < states.Length; g2++) { file += graph[g1, g2] + ","; x += graph[g1, g2] + "\t"; } file += "\n"; x += "\n"; //file_and_png(graph,file); } file_and_png(graph, file); // richTextBox2.Text+="\n"+x; richTextBox2.Text += "\nSTART STATE: " + STR(d1.start); richTextBox2.Text += "\nEND STATE(S):\n"; HashSet <String> endz = new HashSet <string>(); foreach (var t in d1.end) { endz.Add(STR(t)); } //d1.end =new HashSet<List<state>>(d1.end).ToList<List<state>>(); foreach (var p in endz) { richTextBox2.Text += p; richTextBox2.Text += "____"; } // file_and_png(graph,file); }
public void convert(nfa n) { }
void create_graph(String s) { Stack <nfa> eval = new Stack <nfa>(); int d = 0; foreach (char c in s) { if (char.IsLetterOrDigit(c)) { String[] arr1 = { "S" + d++, "S" + d++ }; //char[] arr = { c }; nfa n = new nfa(); n.start = n.add_state(arr1[0]); n.end = n.add_state(arr1[1]); n.add_connect(n.start, n.end, c); //n.table[0, 0] = "1"; eval.Push(n); } if (c == '.') { nfa n1 = eval.Pop(); nfa n2 = eval.Pop(); nfa n3 = new nfa(); //n2.end = n1.end; foreach (state i in n2.states) { if (i.name != n2.end.name) { state dd = n3.add_state(i.name); dd.trans = i.trans; dd.eps = i.eps; } } foreach (state i in n1.states) { if (i.name != n1.start.name) { state dd = n3.add_state(i.name); dd.trans = i.trans; dd.eps = i.eps; } } n3.states.Remove(n1.start); //n3.states.Remove(n2.end); n2.end.eps = n1.start.eps; n2.end.trans = n1.start.trans; n3.states.Add(n2.end); //n3.add_connect(n2.end, n1.start, 'E'); n3.start = n2.start; n3.end = n1.end; eval.Push(n3); } if (c == '*') { nfa n1 = eval.Pop(); //new states to be added state s1 = new state("S" + d++); state s2 = new state("S" + d++); nfa n2 = new nfa(); n2.start = s1; n2.end = s2; n2.states.Add(n2.start); //n2.add_state(s1.name); foreach (state i in n1.states) { state dd = n2.add_state(i.name); dd.eps = i.eps; dd.trans = i.trans; } n2.add_state(s2.name); //n2.start.eps.Add(n2.end); //n2.start.eps.Add(y.start); //n2.states.Remove(n2.start); //n2.states.Add(n2.start); n2.add_connect(n2.start, n1.start, 'E'); n2.add_connect(n2.start, n2.end, 'E'); n2.add_connect(n1.end, n1.start, 'E'); n2.add_connect(n1.end, n2.end, 'E'); eval.Push(n2); } if (c == '+') { //a+b* nfa x = eval.Pop(); nfa y = eval.Pop(); state s1 = new state("S" + d++); state s2 = new state("S" + d++); nfa n2 = new nfa(); //MessageBox.Show(s1.name + "_" + s2.name); n2.end = s2; //n2.add_state(s2.name); n2.start = s1; x.end.eps.Add(n2.end); String p = x.end.name + " " + x.end.eps[0].name; //MessageBox.Show(p); y.end.eps.Add(n2.end); //y.states.Remove(y.end); //n2.states.Add(n2.end); //MessageBox.Show(x.end.name+"__"+y.end.name); foreach (state i in x.states) { if (i.name != x.end.name) { state dd = n2.add_state(i.name); dd.eps = i.eps; dd.trans = i.trans; } else { state dd = n2.add_state(x.end.name); dd.eps = x.end.eps; dd.trans = x.end.trans; } } //x.end.eps.Add(n2.end); //y.end.eps.Add(n2.end); foreach (state i in y.states) { if (i.name != y.end.name) { state dd = n2.add_state(i.name); dd.eps = i.eps; dd.trans = i.trans; } else { state dd = n2.add_state(i.name); dd.eps = y.end.eps; dd.trans = y.end.trans; } } //n2.add_state(s2.name); //MessageBox.Show(n2.start.name + "_" + x.start.name + "_" + y.start.name); n2.start.eps.Add(x.start); n2.start.eps.Add(y.start); n2.states.Remove(n2.start); n2.states.Add(n2.start); n2.states.Add(n2.end); //n2.states.Remove(n2.start); //n2.states.Add(n2.end); //n2.add_connect(n2.start, x.start, 'E'); //n2.add_connect(n2.start, y.start, 'E'); //MessageBox.Show(n2.end.name + "_" + x.end.name + "_" + y.end.name); //n2.add_connect( x.end, n2.end,'E'); //n2.add_connect( y.end, n2.end,'E'); eval.Push(n2); } } nfa final = eval.Pop(); traverse(final); convert(final); //after parsing the string add by yourself a final and initial state }