LR(0)和SLR(1)分析表构造算法C#实现
//LRNode类
public class LRNode
{
public string Left;
public string Right;
public LRNode(string Left, string Right)
{
this.Left = Left;
this.Right = Right;
}
}
//项目集类
public class LRitemsets
{
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 class LRParser
{
public DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] LRAna;//分析表
public Analyze Jz;
public bool Success = false;
public List<LRNode> LRproNum = new List<LRNode>(50);//产生式 列表
public List<LRNode> LRobjNum = new List<LRNode>(50);//项目 列表
public List<LRitemsets> proitemset = new List<LRitemsets>(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 = '';
public string RStr_obitemset = '';//输出返回
public string RStr_DFA = '';
public string RStr_ANA = '';
// 计算非终结符A的FIRST集
public HashSet<char> First(char A)
{
HashSet<char> firstSet = new HashSet<char>();
// ... (实现FIRST集计算逻辑)
return firstSet;
}
// 计算非终结符A的FOLLOW集
public HashSet<char> Follow(char A)
{
HashSet<char> followSet = new HashSet<char>();
// ... (实现FOLLOW集计算逻辑,注意考虑#)
return followSet;
}
// 构造LR(0)分析表
public Table[][] GET_ANA()
{
LRAnaly();
RStr_ANA += '\r\nLR(0)分析表:\r\n ';
int i;
for (i = 0; i < Echar.Count; i++)
{
RStr_ANA += Echar[i].ToString() + ' ';
}
for (i = 0; i < Nchar.Count; i++)
{
RStr_ANA += Nchar[i].ToString() + ' ';
}
RStr_ANA += '\r\n';
for (i = 0; i < proitemset.Count; i++)
{
RStr_ANA += i.ToString() + ' ';
for (int j = 0; j < Echar.Count + Nchar.Count; j++)
{
if (LRAna[i][j].error)
{
RStr_ANA += ' ' + ' ';
}
else if (i == 1 && j == Echar.Count - 1)
{
RStr_ANA += 'AC' + ' ';
}
else if (LRAna[i][j].type != 'N')
{
RStr_ANA += LRAna[i][j].type.ToString() + LRAna[i][j].id.ToString() + ' ';
}
else
RStr_ANA += LRAna[i][j].id.ToString() + ' ';
}
RStr_ANA += '\r\n';
}
return LRAna;
}
public void LRAnaly()
{
// ... (实现LR(0)分析表的构造逻辑)
}
public List<int> Closure(List<int> I)
{
for (int index = 0; index < I.Count; index++)
for (int i = 0; i < LRobjNum[I[index]].Right.Length - 1; i++)
{
if (LRobjNum[I[index]].Right[i] == '.' && isFinalsymbol(LRobjNum[I[index]].Right[i + 1]))
{
for (int j = 0; j < LRobjNum.Count; j++)
{
if (isnexist(I, j))
continue;
if (LRobjNum[j].Left == LRobjNum[I[index]].Right[i + 1].ToString() && LRobjNum[j].Right[0] == '.')n I.Add(j);
}
}
}
return I;
}
// ... (其他函数和逻辑)
}
// 构造SLR(1)分析表
public class SLRParser : LRParser
{
public Table[][] SLRAna;//SLR(1)分析表
Dictionary<char, HashSet<char>> follow = new Dictionary<char, HashSet<char>>();//非终结符的Follow集
// 构造SLR(1)分析表
public new Table[][] GET_ANA()
{
SLRAnaly();
// ... (类似于LR(0)分析表的输出逻辑)
return SLRAna;
}
public void SLRAnaly()
{
// 1. 计算每个非终结符的FOLLOW集
foreach (char nonTerminal in Nchar)
{
follow[nonTerminal] = Follow(nonTerminal);
}
// 2. 构造LR(0)项目集规范族
LRAnaly();
// 3. 构造SLR(1)分析表
SLRAna = new Table[proitemset.Count][];
// ... (实现SLR(1)分析表的构造逻辑,根据FOLLOW集判断归约)
}
}
原文地址: https://www.cveoy.top/t/topic/f0NT 著作权归作者所有。请勿转载和采集!