//获取某个项目集闭包 public List <ClosureItem> getClosure(ClosureItem clo) { List <ClosureItem> lco = new List <ClosureItem>(); lco.Add(clo); int count = 0; while (count != lco.Count) { count = lco.Count; for (int i = 0; i < count; i++) { ClosureItem c1 = lco[i]; String right = c1.Right; String a = ""; int dot = c1.Dot; if (dot != c1.Right.Length) { a = right[dot].ToString(); } foreach (ClosureItem key in ClosureI) { if (key.Left.Equals(a) && key.Dot == 0) { lco.Add(key); } } } lco = lco.Distinct().ToList(); //清除List中的相同项,只保留一个 } return(lco); }
public Boolean equal(ClosureItem clo1) { if (clo1.Dot == dot && clo1.Left.Equals(left) && clo1.Right.Equals(right)) { return(true); } else { return(false); } }
public void getAllClosure(ClosureItem firstclo) { Closure clo = new Closure(); List <ClosureItem> lc = new List <ClosureItem>(); lc = getClosure(firstclo); clo.Clo = lc; clo.Item = 0; ClosureP.Add(clo); int countp = 0; int count = -1; foreach (Closure cc in ClosureP) { countp += cc.Dic.Count; } countp += ClosureP.Count; int item = 1; while (count != countp) { count = countp; for (int i = 0; i < ClosureP.Count; i++) { Dictionary <string, List <ClosureItem> > begin = getKind(ClosureP[i]); List <String> f = begin.Keys.ToList(); int count1 = ClosureP[i].Clo.Count; Dictionary <String, int> d = ClosureP[i].Dic; for (int j = 0; j < begin.Count; j++) { Closure cc = new Closure(); List <ClosureItem> lc1 = new List <ClosureItem>(); String a = f[j]; lc1 = begin[a]; cc.Item = item; ClosureItem c = lc1[0]; string right = c.Right; int dot = c.Dot; Dictionary <string, int> dd = new Dictionary <string, int>(); for (int m = 0; m < lc1.Count; m++) { ClosureItem ci = lc1[m]; ClosureItem tclo1 = new ClosureItem(); List <ClosureItem> lco = new List <ClosureItem>(); tclo1.Left = ci.Left; tclo1.Right = ci.Right; tclo1.Dot = ci.Dot + 1; tclo1.Fromitem = ci.Fromitem; Console.WriteLine("the closure:"); lco = getClosure(tclo1); foreach (ClosureItem key in lco) { String c2 = key.getStrClo(); Console.WriteLine(c2); } cc.Clo.AddRange(lco); lco = cc.Clo.Distinct().ToList(); cc.Clo = lco; } int n = 0; //判断状态是否已经存在 for (; n < ClosureP.Count; n++) { Closure clo1 = ClosureP[n]; if (clo1.equals(cc)) { if (!ClosureP[i].Dic.ContainsKey(right[dot].ToString()) && right[dot] != '$') { ClosureP[i].Dic.Add(right[dot].ToString(), n); } break; } } if (n == ClosureP.Count && !ClosureP[i].Dic.ContainsKey(right[dot].ToString()) && right[dot] != '$') { ClosureP[i].Dic.Add(right[dot].ToString(), item); ClosureP.Add(cc); item++; } } } countp = 0; foreach (Closure cc in ClosureP) { countp += cc.Dic.Count; } countp += ClosureP.Count; } String str = ""; foreach (Closure cl in ClosureP) { str += cl.getStrCLosure(); } }
//获取所有的项目,存储在 ClosureI 这个list中 public void getClosureItem(List <String> LineList) { listView2.Clear(); ColumnHeader ch = new ColumnHeader(); ch.Text = "Syntax:"; //设置列标题 ch.Width = 1000; //设置列宽度 ch.TextAlign = HorizontalAlignment.Center; //设置列的对齐方式 listView2.Columns.Add(ch); //将列头添加到ListView控件。 int row = 1; foreach (String aline in LineList) { if (row == 1) { strVT = aline; VT = aline.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); String str1 = ""; for (int i = 0; i < aline.Length; i++) { if (aline[i] != ',') { str1 += convert[aline[i].ToString()] + " "; } } String vt = "VT:" + str1; ListViewItem lvi = listView2.Items.Add(vt); } else if (row == 2) { strVN = aline; VN = aline.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); String str1 = ""; int i = 0; for (; i < aline.Length / 2; i++) { if (aline[i] != ',') { str1 += convert[aline[i].ToString()] + " "; } } String vt = "VN:" + str1; ListViewItem lvi = listView2.Items.Add(vt); str1 = ""; for (; i < aline.Length; i++) { if (aline[i] != ',') { str1 += convert[aline[i].ToString()] + " "; } } lvi = listView2.Items.Add(str1); } else if (row == 3) { beginC = aline; String vt = "S:" + convert[aline[0].ToString()]; ListViewItem lvi = listView2.Items.Add(vt); ListViewItem lv = listView2.Items.Add("Productions:"); } else { int count = row - 4; String aline1 = count.ToString() + " "; for (int i = 0; i < aline.Length; i++) { if (aline[i] != '-' && aline[i] != '>') { aline1 += convert[aline[i].ToString()] + " "; } else { aline1 += aline[i]; } } ListViewItem lvi = listView2.Items.Add(aline1); grammar += aline + "\r\n"; Production p = new Production(); List <String> temp = new List <string>(); temp = aline.Split(new string[] { "->" }, StringSplitOptions.RemoveEmptyEntries).ToList(); p.Left = temp[0]; p.Right = temp[1]; lp.Add(p); int lenth = temp[1].Length; for (int i = 0; i <= lenth; i++) { ClosureItem clo = new ClosureItem(); clo.Left = temp[0]; clo.Right = temp[1]; clo.Dot = i; clo.Fromitem = row - 4; ClosureI.Add(clo); } } row++; } foreach (ClosureItem c in ClosureI) { Console.WriteLine(c.getStrClo()); } }