SLR 分析器 C# 代码实现
完整代码如下:
class SLR
{
//产生式结点类
public class SLRNode
{
public string Left;
public string Right;
public SLRNode(string Left, string Right)
{
this.Left = Left;
this.Right = Right;
}
}
//项目集类
public class SLRitemsets
{
public List
//DFA结点
public struct DFA
{
public int from;
public char symbol;
public int to;
public DFA(int from, char symbol, int to)
{
this.from = from;
this.symbol = symbol;
this.to = to;
}
}
//分析表 结点
public class Table
{
public bool error;//是否为ERROR
public char type;//结点类型
public int id;//数值
public Table()
{
this.error = true;
}
public Table(char type, int id)
{
this.type = type;
this.id = id;
this.error = false;
}
}
public DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] SLRAna;//分析表
public bool Success = false;
public List<SLRNode> SLRproNum = new List<SLRNode>(50);//产生式 列表
public List<SLRNode> SLRobjNum = new List<SLRNode>(50);//项目 列表
public List<SLRitemsets> proitemset = new List<SLRitemsets>(100);//项目集合
public List<int> Gy_obj = new List<int>(50);//归约项目序号集合
public List<int> Gy_itemset = new List<int>(50);//含有归约项目的集合的序号 的集合
public List<char> Nchar = new List<char>(50);//非终结符集合
public List<char> Echar = new List<char>(50);//终结符集合
public string RStr_obitemset = "";
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('#');
//构造项目 对产生式集合LRproNum中的所有产生式都循环插'.'
SLRNode Lobj;
for (i = 0; i < SLRproNum.Count; i++)
{
left = "";
right = "";
for (int j = 0; j <= SLRproNum[i].Right.Length; j++)//j可以等于length 项目共length+1个
{
left = SLRproNum[i].Left;
right = CreObj(SLRproNum[i].Right, j);//在第j个位置插入'.'
if (j == SLRproNum[i].Right.Length && SLRobjNum.Count != 1)
{//在产生式最后的位置插入. 即为归约项目 项目集中1号位置为接受项目
Gy_obj.Add(SLRobjNum.Count);//归约项目在项目集中的序号 不用+1 本身就是从0开始的
}
Lobj = new SLRNode(left, right);
SLRobjNum.Add(Lobj);
left = "";//还原
right = "";
}
}
Creteitemsets();//项目集
RStr_obitemset += "\r\n项目集构建:\r\n";
for (int j = 0; j < proitemset.Count; j++)
{
RStr_obitemset += 'I' + j.ToString() + ':' + "\r\n";
for (i = 0; i < proitemset[j].Container.Count; i++)
{
RStr_obitemset += SLRobjNum[proitemset[j].Container[i]].Left.ToString() + "->" + SLRobjNum[proitemset[j].Container[i]].Right.ToString() + "\r\n";
}
}
//return RStr_obitemset;
}
public Table[][] GET_ANA()
{
return SLRAna;
}
//求项目集
public void Creteitemsets()
{
SLRitemsets I0 = new SLRitemsets();
I0.Container.Add(0);//加入拓广文法的0
I0.Container = Closure(I0.Container);//求I0的闭包
proitemset.Add(I0);
for (int i = 0; i < proitemset.Count; i++)//遍历项目集合
{
for (int j = 0; j < Echar.Count; j++)//遍历终结符集合
{
List<int> GOTO = Goto(proitemset[i].Container, Echar[j]);//求GOTO(Ii,X)
if (GOTO.Count != 0 && isnexist(proitemset, GOTO))//如果GOTO(Ii,X)非空,且不在项目集合中
{
SLRitemsets I = new SLRitemsets();
I.Container = Closure(GOTO);//求闭包
proitemset.Add(I);//将其加入项目集合
}
}
}
}
//求GOTO(I,X)
public List<int> Goto(List<int> I, char X)
{
List<int> GOTO = new List<int>();
for (int i = 0; i < I.Count; i++)//遍历I中的每个项目
{
int index = I[i];
for (int j = 0; j < SLRobjNum[index].Right.Length - 1; j++)//遍历该项目右侧的每个字符
{
if (SLRobjNum[index].Right[j] == X && SLRobjNum[index].Right[j + 1] == '.')//如果该字符为X且其后面一个字符为'.'
{
SLRNode Lobj = new SLRNode(SLRobjNum[index].Left, CreObj(SLRobjNum[index].Right, j + 1));//构造新项目
int num = isexist(SLRobjNum, Lobj);//查找该项目是否已经在项目列表中
if (num != -1 && isnexist(GOTO, num))//如果该项目已经在项目列表中且未被加入GOTO集合
GOTO.Add(num);//将其加入GOTO集合
}
}
}
return GOTO;
}
//判断字符是否为终结符
public bool isFinalsymbol(char ch)
{
if (ch >= 'a' && ch <= 'z')
return true;
if (ch >= 'A' && ch <= 'Z')
return false;
return false;
}
//判断字符是否存在于集合中
public bool exist(List<char> list, char ch)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i] == ch)
return true;
}
return false;
}
//判断序号是否存在于集合中
public bool isnexist(List<int> list, int num)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i] == num)
return false;
}
return true;
}
//判断项目是否存在于项目集合中
public bool isnexist(List<SLRitemsets> list, List<int> I)
{
for (int i = 0; i < list.Count; i++)
原文地址: https://www.cveoy.top/t/topic/f0lS 著作权归作者所有。请勿转载和采集!