SLR 文法分析器 C# 代码实现
class SLR
{
//产生式结点类
public class LRproNode
{
public string Left;
public string Right;
public LRproNode(string Left, string Right)
{
this.Left = Left;
this.Right = Right;
}
}
//项目集类
public class LR_club
{
public List<int> Container
= new List<int>(100);
//记录项目在项目集合中的序号
}
//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[][] LRAna;//分析表
public List<LRproNode> LRproNum = new List<LRproNode>(50);//产生式 列表
public List<LRproNode> LRobjNum = new List<LRproNode>(50);//项目 列表
public List<LR_club> pro_club = new List<LR_club>(100);//项目集合
public List<int> Gy_obj = new List<int>(50);//归约项目序号集合
public List<int> Gy_club = new List<int>(50);//含有归约项目的集合的序号 的集合
public List<char> Nchar = new List<char>(50);//非终结符集合
public List<char> Echar = new List<char>(50);//终结符集合
public string RETStr = '';
public string RETStr_obclub = '';//输出返回
public string RETStr_DFA = '';
public string RETStr_ANA = '';
//读取传递过来的文件信息str后,对str的或运算加以处理,变成多个产生式
//返回处理过的信息
public void Start(string str)
{
LRproNode Lr;
int i = 0;
string left = '';
string right = '';
left += 'S'';
right += str[0];
Lr = new LRproNode(left, right);//拓广文法开始
LRproNum.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 LRproNode(left, right);
LRproNum.Add(Lr);
right = ''; //产生式左边相同 右边重新积累
i++; //跳过'|'
continue;
}
if ((i - j) == 0)
{
if (!iscexist(Nchar, str[i]))//如果非终结符集合
Nchar.Add(str[i]);
left += str[i++];
}
else if (i - j <= 2)
i++;
else
{
if (isFinalsymbol(str[i]) && !iscexist(Nchar, str[i]))//如果非终结符集合中不存在str[i],加入Nchar isfinalsymbol 非终结符返回T 终结符返回F
Nchar.Add(str[i]);
else if (!isFinalsymbol(str[i]) && !iscexist(Echar, str[i]))//产生式右边 需要判断终结符
Echar.Add(str[i]);
right += str[i++];
}
}//while
i++;//跳过换行符
if (left != '' && right != '')
{
Lr = new LRproNode(left, right);//构造每一行最后一个产生式,不存在'|'时就是该行产生式本身
LRproNum.Add(Lr);
}
}//while
Echar.Add('#');
//构造项目 对产生式集合LRproNum中的所有产生式都循环插'.'
LRproNode Lobj;
for (i = 0; i < LRproNum.Count; i++)
{
left = '';
right = '';
for (int j = 0; j <= LRproNum[i].Right.Length; j++)//j可以等于length 项目共length+1个
{
left = LRproNum[i].Left;
right = CreObj(LRproNum[i].Right, j);//在第j个位置插入'.'
if (j == LRproNum[i].Right.Length && LRobjNum.Count != 1)
{
//在产生式最后的位置插入. 即为归约项目 项目集中1号位置为接受项目
Gy_obj.Add(LRobjNum.Count);//归约项目在项目集中的序号 不用+1 本身就是从0开始的
}
Lobj = new LRproNode(left, right);
LRobjNum.Add(Lobj);
left = '';//还原
right = '';
}
}
Cre_club();//项目集
RETStr_obclub += '\r\n项目集构建:\r\n';
for (int j = 0; j < pro_club.Count; j++)
{
RETStr_obclub += 'I' + j.ToString() + ':' + '\r\n';
for (i = 0; i < pro_club[j].Container.Count; i++)
{
RETStr_obclub += LRobjNum[pro_club[j].Container[i]].Left.ToString() + '->' + LRobjNum[pro_club[j].Container[i]].Right.ToString() + '\r\n';
}
}
//return RETStr_obclub;
}
//返回分析表 额外功能 可以不要
public Table[][] GET_ANA()
{
LRAnaly();
RETStr_ANA += '\r\nLR0分析表:\r\n ';
int i;
for (i = 0; i < Echar.Count; i++)
{
RETStr_ANA += Echar[i].ToString() + ' ';
}
for (i = 0; i < Nchar.Count; i++)
{
RETStr_ANA += Nchar[i].ToString() + ' ';
}
RETStr_ANA += '\r\n';
for (i = 0; i < pro_club.Count; i++)
{
RETStr_ANA += i.ToString() + ' ';
for (int j = 0; j < Echar.Count + Nchar.Count; j++)
{
if (LRAna[i][j].error)
{
RETStr_ANA += ' ' + ' ';
}
else if (i == 1 && j == Echar.Count - 1)
{
RETStr_ANA += 'AC' + ' ';
}
else if (LRAna[i][j].type != 'N')
{
RETStr_ANA += LRAna[i][j].type.ToString() + LRAna[i][j].id.ToString() + ' ';
}
else
RETStr_ANA += LRAna[i][j].id.ToString() + ' ';
}
RETStr_ANA += '\r\n';
}
return LRAna;
}
//求项目集
public void Cre_club()
{
List<int> lr_club = new List<int>(100);//记录项目的序号
lr_club.Add(0);
lr_club = Closure(lr_club);//构造初始项目集 求闭包
LR_club LR_C = new LR_club();
LR_C.Container = lr_club;//集合----项目集序号的集合
pro_club.Add(LR_C);//集合的集合----存放项目集序号集合 的集合
for (int i = 0; i < pro_club.Count; i++)//整体集合中 第i个项目集
{
pro_club[i].Container.Sort();//排序由小到大 后面用于判断是否存在的比较
int[] flag = new int[pro_club[i].Container.Count];
for (int fi = 0; fi < pro_club[i].Container.Count; fi++)//标志位,用来判断该序号是否已经构造
{
flag[fi] = 0;
}
for (int j = 0; j < pro_club[i].Container.Count; j++)//第i个项目集的第j个项目
{
if (flag[j] == 1)//如果已经访问过 就不再构造 找下一个项目
continue;
int index = pro_club[i].Container[j];
for (int pi = 0; pi < LRobjNum[index].Right.Length - 1; pi++)//length-1是避免匹配到.在最后的规约项目
{
if (LRobjNum[index].Right[pi] == '.')
{
List<int> lr2_club = new List<int>(100);//记录项目的序号
char symbol = LRobjNum[index].Right[pi + 1];//记录.a转移状态a.的符号a
lr2_club.Add((index + 1));//如果遇到.a形式的项目序号为index 那么项目a.的序号为index+1
for (int m1 = j + 1; m1 < pro_club[i].Container.Count; m1++)
{
//在第i个项目集中找到了可以移动的.:.a 重新遍历第i个项目集j项目之后的 找到同样可以移动a的项目集
int index2 = pro_club[i].Container[m1];
for (int m2 = 0; m2 < LRobjNum[index2].Right.Length - 1; m2++)
{
if (LRobjNum[index2].Right[m2] == '.' && LRobjNum[index2].Right[m2 + 1] == symbol)
{
flag[m1] = 1;//标记位置为1 已经访问 之后不再访问
lr2_club.Add(index2 + 1);
}
}
}
lr2_club = Closure(lr2_club);//求闭包
int value = isexist(lr2_club);
if (value == -1)//-1表示不存在相同的
{
for (int m3 = 0; m3 < Gy_obj.Count; m3++)
{
if (isnexist(lr2_club, Gy_obj[m3]))
{
Gy_club.Add(pro_club.Count);
}
}
LR_club LR_C2 = new LR_club();
dfa[Pindex++] = new DFA(i, symbol, pro_club.Count);//count不用加1 本身从0开始
LR_C2.Container = lr2_club;
pro_club.Add(LR_C2);
}
else
{
dfa[Pindex++] = new DFA(i, symbol, value);
}
break;
}
}
}//end-forj
}//end-fori
}//end-Cre_club
//分析表
public void LRAnaly()
{
Table tnode = new Table();
LRAna = new Table[pro_club.Count][];
for (int i = 0; i < pro_club.Count; i++)
LRAna[i] = new Table[Echar.Count + Nchar.Count];
for (int i = 0; i < pro_club.Count; i++)//初始化 赋予ERROR属性
for (int j = 0; j < Echar.Count + Nchar.Count; j++)//为终结符加r状态
LRAna[i][j] = tnode;
tnode = new Table('A', 0);
LRAna[1][FindID(Echar, '#')] = tnode;//项目集1必定是接受项目 构建[1][#]:acc的情况 先直接赋值好 dfa里没有
for (int i = 0; i < Gy_club.Count; i++)
{
tnode = new Table('r', Find_pro(LRobjNum[pro_club[Gy_club[i]].Container[0]]));//归约项目 找到原产生式序号 添加状态r
for (int j = 0; j < Echar.Count; j++)
{
LRAna[Gy_club[i]][j] = tnode;
}
}
for (int i = 0; i < Pindex; i++)
{
if (isFinalsymbol(dfa[i].symbol))//symbol为非终结符 添加状态N
{
int CID = FindID(Nchar, dfa[i].symbol);
tnode = new Table('N', dfa[i].to);
LRAna[dfa[i].from][CID + Echar.Count] = tnode;
}
else //不是归约项目 添加状态S
{
int CID = FindID(Echar, dfa[i].symbol);
tnode = new Table('S', dfa[i].to);
LRAna[dfa[i].from][CID] = tnode;
}
}
}
//以下代码省略,需要根据具体需求添加SLR算法的实现
// ...
}
上述代码是LR0文法的实现,需要修改为SLR文法的实现,主要需要修改以下部分:
- Closure 函数:
- 在LR0文法的Closure函数中,只需要考虑 '.' 前面的符号,即如果 '.' 前面是 A,则需要添加形如 A -> .β 的项目。
- 在SLR文法中,需要考虑 '.' 前面的符号和所有 FOLLOW(A) 集合中的符号,即如果 '.' 前面是 A,则需要添加形如 A -> .β 和 A -> .βα 的项目,其中 α ∈ FOLLOW(A)。
- 分析表构建:
- LR0文法只需要根据 DFA 的转移关系和项目集的归约项目来构建分析表。
- SLR文法需要考虑 FOLLOW 集合。如果一个项目集包含了形如 A -> β. 的项目,则对于所有 α ∈ FOLLOW(A),分析表中对应状态的 α 项应该为 r,表示归约到 A -> β 的产生式。
修改后的代码示例:
// ... (代码同上)
// 计算FOLLOW集合
public List<char> FOLLOW(char A)
{
List<char> follow = new List<char>();
// ... (实现 FOLLOW 集合的计算逻辑)
return follow;
}
// 修改后的Closure函数
public List<int> Closure(List<int> lr_club)
{
// ... (LR0 Closure 逻辑)
// 添加SLR的Closure逻辑:
for (int i = 0; i < lr_club.Count; i++)
{
int index = lr_club[i];
if (LRobjNum[index].Right.Length > 0 && LRobjNum[index].Right[LRobjNum[index].Right.Length - 1] == '.')
{
// 找到形如 A -> β. 的项目
char A = LRobjNum[index].Left[0]; // 提取非终结符
List<char> followA = FOLLOW(A);
foreach (char alpha in followA)
{
// 添加形如 A -> .βα 的项目
// ... (实现添加项目逻辑)
}
}
}
return lr_club;
}
// 修改后的分析表构建函数
public void LRAnaly()
{
// ... (LR0 分析表构建逻辑)
// 添加SLR分析表构建逻辑
for (int i = 0; i < pro_club.Count; i++)
{
for (int j = 0; j < pro_club[i].Container.Count; j++)
{
int index = pro_club[i].Container[j];
if (LRobjNum[index].Right.Length > 0 && LRobjNum[index].Right[LRobjNum[index].Right.Length - 1] == '.')
{
char A = LRobjNum[index].Left[0];
List<char> followA = FOLLOW(A);
foreach (char alpha in followA)
{
int alphaID = FindID(Echar, alpha);
if (alphaID != -1)
{
tnode = new Table('r', Find_pro(LRobjNum[index]));
LRAna[i][alphaID] = tnode;
}
}
}
}
}
}
注意:以上代码仅供参考,具体的实现需要根据具体的文法和语言进行调整。
原文地址: https://www.cveoy.top/t/topic/f1TL 著作权归作者所有。请勿转载和采集!