SLR1 分析器代码实现:生成 SLR1 分析器功能函数
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<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 class Analyze
{
public List<string> stack_state = new List<string>(100);//记录状态栈
public List<string> stack_symbol = new List<string>(100);//记录符号栈
public List<string> Input_str = new List<string>(100);//记录输入串
public List<string> Tran_pro = new List<string>(100);//记录所用产生式
}
public DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] SLRAna;//分析表
public Analyze Jz;
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);//终结符集合
Dictionary<char, HashSet<char>> follow = new Dictionary<char, HashSet<char>>();//非终结符的Follow集
public string RStr = '';
public string RStr_obitemset = '';//输出返回
public string RStr_DFA = '';
public string RStr_ANA = '';
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('#');
//构造项目 对产生式集合SLRproNum中的所有产生式都循环插'.'
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 void Creteitemsets()
{
//初始化
SLRitemsets I0 = new SLRitemsets();
I0.Container.Add(0);//将拓广文法的产生式加入项目集
proitemset.Add(I0);//将I0加入项目集合
int i = 0;
while (i < proitemset.Count)
{
SLRitemsets I = proitemset[i];
//寻找I中所有项目的点后面的符号,将其加入集合C中
List<char> C = new List<char>();
for (int j = 0; j < I.Container.Count; j++)
{
int index = SLRobjNum[I.Container[j]].Right.IndexOf('.');
if (index == SLRobjNum[I.Container[j]].Right.Length - 1)//如果点在产生式最后,跳过
continue;
char ch = SLRobjNum[I.Container[j]].Right[index + 1];
if (!exist(C, ch))
C.Add(ch);
}
//对于集合C中的每个符号,构造新的项目集I'
for (int j = 0; j < C.Count; j++)
{
SLRitemsets I_new = new SLRitemsets();
for (int k = 0; k < I.Container.Count; k++)
{
int index = SLRobjNum[I.Container[k]].Right.IndexOf('.');
if (index == SLRobjNum[I.Container[k]].Right.Length - 1)//如果点在产生式最后,跳过
continue;
char ch = SLRobjNum[I.Container[k]].Right[index + 1];
if (ch == C[j])
{
SLRNode Lobj = new SLRNode(SLRobjNum[I.Container[k]].Left, CreObj(SLRobjNum[I.Container[k]].Right, index + 1));
int num = isexist(I_new.Container);
if (num == -1)//如果I'不存在,加入
{
I_new.Container.Add(SLRobjNum.Count);
SLRobjNum.Add(Lobj);
}
else//如果I'已存在,加入
{
I_new.Container.Add(num);
}
}
}
//如果I'不存在于项目集合中,加入
int num1 = isexist(I_new.Container);
if (num1 == -1)
{
proitemset.Add(I_new);
num1 = proitemset.Count - 1;
}
//构造DFA
dfa[Pindex] = new DFA(i, C[j], num1);
Pindex++;
}
i++;
}
}
//获取LR0分析表
public Table[][] GET_ANA()
{
int i, j;
//获取非终结符的Follow集
follow = GetFollow(Nchar, Echar, SLRproNum, SLRobjNum);
//构造分析表
SLRAna = new Table[proitemset.Count][];
for (i = 0; i < proitemset.Count; i++)
{
SLRAna[i] = new Table[Echar.Count + Nchar.Count];
for (j = 0; j < Echar.Count; j++)
{
SLRAna[i][j] = new Table();
}
for (j = 0; j < Nchar.Count; j++)
{
SLRAna[i][j + Echar.Count] = new Table();
}
}
//填充分析表
for (i = 0; i < proitemset.Count; i++)
{
for (j = 0; j < proitemset[i].Container.Count; j++)
{
int index = SLRobjNum[proitemset[i].Container[j]].Right.IndexOf('.');
if (index == SLRobjNum[proitemset[i].Container[j]].Right.Length - 1)//如果点在产生式最后
{
if (SLRobjNum[proitemset[i].Container[j]].Left == 'S'')
{
//接受状态
SLRAna[i][Echar.Count - 1] = new Table('A', -1);
}
else
{
//归约状态
int pro_index = Find_pro(SLRobjNum[proitemset[i].Container[j]]);
HashSet<char> follow_set = follow[SLRobjNum[proitemset[i].Container[j]].Left[0]];
foreach (char c in follow_set)
{
int col = FindID(Echar, c);
SLRAna[i][col] = new Table('R', pro_index);
}
}
}
else
{
char ch = SLRobjNum[proitemset[i].Container[j]].Right[index + 1];
if (isFinalsymbol(ch))
{
//移进状态
int col = FindID(Echar, ch);
int to = dfa[i * (Echar.Count + Nchar.Count) + col].to;
SLRAna[i][col] = new Table('S', to);
}
}
}
}
return SLRAna;
}
//构造分析表
public void SLRAnaly()
{
GET_ANA();
RStr_ANA += '\r\n分析表:\r\n';
RStr_ANA += ' |';
for (int i = 0; i < Echar.Count; i++)
{
RStr_ANA += ' ' + Echar[i] + ' |';
}
for (int i = 0; i < Nchar.Count; i++)
{
RStr_ANA += ' ' + Nchar[i] + ' |';
}
RStr_ANA += '\r\n';
for (int i = 0; i < proitemset.Count; i++)
{
RStr_ANA += i.ToString().PadLeft(2) + '|';
for (int j = 0; j < Echar.Count + Nchar.Count; j++)
{
if (SLRAna[i][j].error)
RStr_ANA += ' |';
else
{
RStr_ANA += ' ';
RStr_ANA += SLRAna[i][j].type.ToString();
RStr_ANA += SLRAna[i][j].id.ToString().PadLeft(2);
RStr_ANA += ' |';
}
}
RStr_ANA += '\r\n';
}
}
public void sen_Analyze(string text){}//分析句子
//构造项目 在right[index]位置插入'.'
public string CreObj(string right, int index)
{
int i = 0;
string Restr = '';
while (i < right.Length)
{
if (i == index)
Restr += '.';
Restr += right[i];
i++;
}
if (i == index)
Restr += '.';
return Restr;
}
//判断ch是否为非终结符
public bool isFinalsymbol(char ch1)
{
//char ch1=ch[0];
if (ch1 >= 'A' && ch1 <= 'Z')
return true;
else
return false;
}
//判断集合是I否存在于proitemset
//如果存在就返回已存在的项目集序号
//如果不存在就返回-1
public int isexist(List<int> I)
{
I.Sort();
for (int i = 0; i < proitemset.Count; i++)
{
proitemset[i].Container.Sort();
if (I.SequenceEqual(proitemset[i].Container))
{
return i;
}
}
return -1;
}
//判断num是否存在于I
//存在返回true 不存在返回False
public bool isnexist(List<int> I, int num)
{
for (int i = 0; i < I.Count; i++)
{
if (I[i] == num)
return true;
}
return false;
}
//判断ch是否存在于I
//存在返回true 不存在返回False
public bool exist(List<char> I, char ch)
{
for (int i = 0; i < I.Count; i++)
{
if (I[i] == ch)
return true;
}
return false;
}
//寻找ch在I中的序号
public int FindID(List<char> I, char ch)
{
for (int i = 0; i < I.Count; i++)
{
if (I[i] == ch)
return i;
}
return -1;
}
//寻找项目最初的产生式序号 E->.ab ====> E->a
public int Find_pro(SLRNode SLR)
{
string s = '';
for (int i = 0; i < SLR.Right.Length; i++)
{
if (SLR.Right[i] != '.')
s += SLR.Right[i];
}
for (int i = 0; i < SLRproNum.Count; i++)
{
if (SLRproNum[i].Left == SLR.Left && SLRproNum[i].Right == s)
return i;
}
return -1;
}
}```
```csharp
//获取非终结符的Follow集
public Dictionary<char, HashSet<char>> GetFollow(List<char> Nchar, List<char> Echar, List<SLRNode> SLRproNum, List<SLRNode> SLRobjNum)
{
Dictionary<char, HashSet<char>> follow = new Dictionary<char, HashSet<char>>();
//初始化
for (int i = 0; i < Nchar.Count; i++)
{
HashSet<char> set = new HashSet<char>();
follow.Add(Nchar[i], set);
}
follow[Nchar[0]].Add('#');//将#加入开始符号的Follow集
bool flag = true;//是否修改了Follow集
while (flag)
{
flag = false;
for (int i = 0; i < SLRproNum.Count; i++)
{
string right = SLRproNum[i].Right;
char left = SLRproNum[i].Left[0];
for (int j = 0; j < right.Length; j++)
{
if (right[j] >= 'A' && right[j] <= 'Z')
{
if (j == right.Length - 1)//如果是最后一个符号
{
if (follow[right[j]].Except(follow[left]).Count() > 0)
{
//如果right[j]的Follow集有修改
follow[left].UnionWith(follow[right[j]]);
flag = true;
}
}
else
{
HashSet<char> set = new HashSet<char>();
for (int k = j + 1; k < right.Length; k++)
{
if (right[k] >= 'A' && right[k] <= 'Z')
{
set.UnionWith(First(right[k], SLRproNum, SLRobjNum));
if (set.Contains('ε'))
{
set.Remove('ε');
set.UnionWith(follow[left]);
}
}
else
{
set.Add(right[k]);
break;
}
}
if (follow[right[j]].Except(set).Count() > 0)
{
follow[right[j]].UnionWith(set);
flag = true;
}
}
}
}
}
}
return follow;
}
//获取非终结符的First集
public HashSet<char> First(char ch, List<SLRNode> SLRproNum, List<SLRNode> SLRobjNum)
{
HashSet<char> first = new HashSet<char>();
for (int i = 0; i < SLRproNum.Count; i++)
{
if (SLRproNum[i].Left[0] == ch)
{
string right = SLRproNum[i].Right;
if (right.Length == 0)
{
first.Add('ε');
}
else
{
for (int j = 0; j < right.Length; j++)
{
if (right[j] >= 'A' && right[j] <= 'Z')
{
first.UnionWith(First(right[j], SLRproNum, SLRobjNum));
if (first.Contains('ε'))
{
if (j == right.Length - 1)
{
first.Remove('ε');
}
}
else
{
break;
}
}
else
{
first.Add(right[j]);
break;
}
}
}
}
}
return first;
}
原文地址: https://www.cveoy.top/t/topic/f0NF 著作权归作者所有。请勿转载和采集!