//求闭包 public List Closure(List lr_item) { List lr_club = new List(100);//记录项目的序号 for (int i = 0; i < lr_item.Count; i++) { lr_club.Add(lr_item[i]); } for (int i = 0; i < lr_item.Count; i++) { int index = lr_item[i]; for (int j = 0; j < SLRobjNum.Count; j++) { if (SLRobjNum[j].Left[0] == SLRobjNum[index].Right[SLRobjNum[index].Right.IndexOf('.') + 1]) { SLRNode Lobj = new SLRNode(SLRobjNum[j].Left, CreObj(SLRobjNum[j].Right, 0)); if (!isnexist(lr_club, j) && !isnexist(lr_item, j)) { lr_club.Add(j); } } } } return lr_club; }

//判断字符是否存在于集合中 public bool exist(List list, char c) { for (int i = 0; i < list.Count; i++) { if (list[i] == c) return true; } return false; }

//判断项目集是否存在 public int isexist(List lr_club) { for (int i = 0; i < proitemset.Count; i++) { if (proitemset[i].Container.Count != lr_club.Count) continue; int flag = 0; for (int j = 0; j < lr_club.Count; j++) { if (isnexist(proitemset[i].Container, lr_club[j])) { flag = 1; break; } } if (flag == 0) return i; } return -1; }

//判断项目集中是否存在该序号 public bool isnexist(List lr_club, int j) { for (int i = 0; i < lr_club.Count; i++) { if (lr_club[i] == j) return false; } return true; }

//构造项目 public string CreObj(string str, int index) { string left = ""; string right = ""; for (int i = 0; i < str.Length; i++) { if (i == index) right += '.'; right += str[i]; } if (index == str.Length) right += '.'; left = " "; return left + "->" + right; }

//判断是否为终结符 public bool isFinalsymbol(char c) { if (c >= 'a' && c <= 'z') return true; if (c >= 'A' && c <= 'Z') return false; return false; }

//构建SLR分析表 public void BuildSLRAna() { SLRAna = new Table[proitemset.Count][]; for (int i = 0; i < proitemset.Count; i++) { SLRAna[i] = new Table[Echar.Count + Nchar.Count];//终结符和非终结符的数量总和 for (int j = 0; j < Echar.Count; j++) { SLRAna[i][j] = new Table(); } for (int j = 0; j < Nchar.Count; j++) { SLRAna[i][j + Echar.Count] = new Table(); } } for (int i = 0; i < proitemset.Count; i++) { for (int j = 0; j < proitemset[i].Container.Count; j++) { int index = proitemset[i].Container[j]; if (SLRobjNum[index].Right.IndexOf('.') != SLRobjNum[index].Right.Length - 1)//移进项目 { char symbol = SLRobjNum[index].Right[SLRobjNum[index].Right.IndexOf('.') + 1]; if (isFinalsymbol(symbol)) { int k = Echar.IndexOf(symbol); int value = dfa[i].to; SLRAna[i][k] = new Table('s', value); } else { int k = Nchar.IndexOf(symbol); int value = dfa[i].to; SLRAna[i][k + Echar.Count] = new Table('g', value); } } else//归约项目 { for (int k = 0; k < Gy_obj.Count; k++) { if (index == Gy_obj[k]) { if (SLRobjNum[index].Left == "S'") { int value = 0; SLRAna[i][Echar.Count - 1] = new Table('a', value); } else { int value = SLRproNum.IndexOf(new SLRNode(SLRobjNum[index].Left, SLRobjNum[index].Right.Replace(".", ""))); for (int m = 0; m < Echar.Count; m++) { SLRAna[i][m] = new Table('r', value); } } } } } } } }

//SLR分析表输出 public void PrintSLRAna() { RStr_SLRAna += "\r\nSLR分析表:\r\n"; RStr_SLRAna += " "; for (int i = 0; i < Echar.Count; i++) { RStr_SLRAna += Echar[i] + " "; } for (int i = 0; i < Nchar.Count; i++) { RStr_SLRAna += Nchar[i] + " "; } RStr_SLRAna += "\r\n"; for (int i = 0; i < proitemset.Count; i++) { RStr_SLRAna += "I" + i + ": "; for (int j = 0; j < Echar.Count + Nchar.Count; j++) { if (SLRAna[i][j].error) RStr_SLRAna += " "; else RStr_SLRAna += SLRAna[i][j].type + SLRAna[i][j].id.ToString().PadLeft(2, '0') + " "; } RStr_SLRAna += "\r\n"; } }

//SLR分析 public void SLRAnalysis(string input) { input += "#"; Stack state_stack = new Stack(); Stack symbol_stack = new Stack(); state_stack.Push(0); symbol_stack.Push('#'); int index = 0; while (true) { int state = state_stack.Peek(); char symbol = input[index]; int col = Echar.IndexOf(symbol); if (col == -1) { col = Nchar.IndexOf(symbol) + Echar.Count; } Table table = SLRAna[state][col]; if (table.error) { RStr_SLRAnalysis += "Error!"; return; } if (table.type == 's') { state_stack.Push(table.id); symbol_stack.Push(symbol); index++; } else if (table.type == 'r') { int len = SLRproNum[table.id].Right.Length; for (int i = 0; i < len; i++) { state_stack.Pop(); symbol_stack.Pop(); } char left = SLRproNum[table.id].Left[0]; symbol_stac

请在下列代码环境中实现SLRAnaly函数及相关调用函数实现构建SLR分析表功能。class SLR 产生式结点类 public class SLRNode public string Left; public string Right; public SLRNodestring

原文地址: https://www.cveoy.top/t/topic/hrgA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录