补充函数 Buildprod、Table[][] GET_ANA、Creteitemsets()等的完整的代码如下:

public void Buildprod(string str) { SLRNode Lr; int i = 0; string left = ""; string right = ""; left += "S'"; right += str[0]; Lr = new SLRNode(left, right);//拓广文法开始 SLRproNum.Add(Lr); while (i < str.Length) { left = right = "";//还原 int j = i; while (i < str.Length && str[i] != '\r' && str[i] != '\n')//换行符‘\r\n’ { if (str[i] == ' ') { i++; continue; } if (str[i] == '|') // 遇到“|”可构造一条产生式 { Lr = new SLRNode(left, right); SLRproNum.Add(Lr); right = ""; //产生式左边相同 右边重新积累 i++; //跳过“|” continue; } if ((i - j) == 0) { if (!exist(Nchar, str[i]))//如果非终结符集合中不存在str[i],加入Nchar 产生式左边 只有非终结符 不必判断终结符 Nchar.Add(str[i]); left += str[i++]; } else if (i - j <= 2) i++; else { if (isFinalsymbol(str[i]) && !exist(Nchar, str[i]))//如果非终结符集合中不存在str[i],加入Nchar isfinalsymbol 非终结符返回T 终结符返回F Nchar.Add(str[i]); else if (!isFinalsymbol(str[i]) && !exist(Echar, str[i]))//产生式右边 需要判断终结符 Echar.Add(str[i]); right += str[i++]; } }//while i++;//跳过换行符 if (left != "" && right != "") { Lr = new SLRNode(left, right);//构造每一行最后一个产生式,不存在"|"时就是该行产生式本身 SLRproNum.Add(Lr); } }//while Echar.Add('#');

//构造项目 对
SLR1 分析器代码实现 - 项目集构建,闭包计算,分析表生成

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

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