SLR1 分析算法错误分析及代码修正
在 SLR 分析表的构建过程中,归约项目的 follow 集合没有被正确处理,导致分析表中缺少了部分归约动作。具体来说,在归约项目的 follow 集合中,应该排除空串,而在代码中没有进行排除。因此,需要对 GetFollow() 函数进行修改,将 if (first.Contains('#')) 中的代码改为:
if (first.Contains('#'))
{
first.Remove('#');
foreach (char c in first)
{
if (!follow.Contains(c))
{
follow.Add(c);
flag = true;
}
}
if (node.Right.Length > 1)
{
int nextIndex = FindID(Nchar, node.Left[0]);
foreach (char c in Follow[nextIndex])
{
if (!follow.Contains(c))
{
follow.Add(c);
flag = true;
}
}
}
}
修改后,归约项目的 follow 集合中不再包含空串,可以正确构建 SLR 分析表。
原文地址: https://www.cveoy.top/t/topic/f0JZ 著作权归作者所有。请勿转载和采集!