请在下列代码环境中实现SLRAnaly函数及相关调用函数实现构建SLR分析表功能。class SLR 产生式结点类 public class SLRNode public string Left; public string Right; public SLRNodestring
//求闭包
public List
//判断字符是否存在于集合中
public bool exist(List
//判断项目集是否存在
public int isexist(List
//判断项目集中是否存在该序号
public bool isnexist(List
//构造项目 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
原文地址: https://www.cveoy.top/t/topic/hrgA 著作权归作者所有。请勿转载和采集!