SLR(1) 分析表构建:高效实现方法及代码解析
public void SLRAnaly()
{
// 构造终结符集合、非终结符集合
List
// 构造FIRST、FOLLOW集合
FIRST first = new FIRST(SLRproNum, Nchar, Echar);
first.buildFIRST();
FOLLOW follow = new FOLLOW(SLRproNum, first.getfirsts(), Nchar, Echar);
follow.buildFOLLOW();
// 构造LR(0)自动机
LR_0 lr_0 = new LR_0(SLRobjNum, proitemset, Nchar, Echar);
lr_0.buildLR_0();
// 构造SLR(1)自动机
SLR_1 slr_1 = new SLR_1(lr_0, Nchar, Echar);
slr_1.buildSLR_1();
// 构造分析表
Dictionary<int, List<string>> table = new Dictionary<int, List<string>>();
SLRTable slrtable = new SLRTable(table, SLRproNum, slr_1, terminals, nonterminal, follow);
slrtable.buildtable();
SLRAna = new Table[table.Count][];
for (int i = 0; i < table.Count; i++)
{
SLRAna[i] = new Table[terminals.Count + nonterminal.Count];
for (int j = 0; j < terminals.Count + nonterminal.Count; j++)
{
if (j < terminals.Count)
{
string str = table[i][j];
if (str == "")
{
SLRAna[i][j] = new Table();
}
else if (str[0] == 'S')
{
SLRAna[i][j] = new Table('S', int.Parse(str.Substring(1)));
}
else if (str[0] == 'r')
{
SLRAna[i][j] = new Table('r', int.Parse(str.Substring(1)));
}
else if (str == "acc")
{
SLRAna[i][j] = new Table('a', 0);
}
}
else
{
string str = table[i][j - terminals.Count];
if (str == "")
{
SLRAna[i][j] = new Table();
}
else
{
SLRAna[i][j] = new Table('G', int.Parse(str.Substring(1)));
}
}
}
}
}
public void buildtable()
{
for (int i = 0; i < states.Count; i++)
{
// 对每个状态经过终结符的情况进行判断
List
原文地址: https://www.cveoy.top/t/topic/f1Rm 著作权归作者所有。请勿转载和采集!