请依据SLR1分析表构建的步骤但下面代码在在分析表中没有正确地添加归约状态对应状态对每个终结符都添加同样的动作请给出错误分析结论、指出错误部分和修改后的代码。示例:输入文法假设为LR0文法:E-E+TTT-TFFF-Ed生成的部分SLR分析表:状态+d#E4 S4 S5 85r6r6 r6 r6 6 S4 S5 7 S4 S5 8S6 S11 9r1S7 r1 r110r3r3 r
错误分析结论:在SLR1分析表中没有正确地添加归约状态,对应状态对每个终结符都添加同样的动作。
错误部分:在SLRAnaly()函数中,对于归约状态的处理,只考虑了每个归约状态的Follow集,而没有考虑到每个归约状态对应的所有产生式的First集。因此,在分析表中对于每个终结符,归约状态都添加了相同的动作。
修改后的代码:
- 在SLRAnaly()函数中,修改对于归约状态的处理,考虑到每个归约状态对应的所有产生式的First集:
for (int i = 0; i < Gy_itemset.Count; i++)
{
List<int> container = proitemset[Gy_itemset[i]].Container;
foreach (int index in container)
{
SLRNode item = SLRobjNum[index];
List<char> first = GetFirst(item.Right, item.Pos + 1);
foreach (char c in first)
{
int CID = FindID(Echar, c);
SLRAna[Gy_itemset[i]][CID] = new Table('r', index);
}
}
}
- 在GetFirst()函数中,添加参数pos,表示从产生式的第pos个字符开始求First集:
public List<char> GetFirst(string str, int pos)
{
List<char> first = new List<char>();
if (pos >= str.Length)
{
first.Add('#');
return first;
}
char c = str[pos];
if (isFinalsymbol(c))
first.Add(c);
else
{
foreach (SLRNode node in SLRproNum)
{
if (node.Left[0] == c)
{
if (node.Right[0] == c)
continue;
else if (isFinalsymbol(node.Right[0]))
first.Add(node.Right[0]);
else
{
List<char> subFirst = GetFirst(node.Right, 0);
if (subFirst.Contains('#'))
{
subFirst.Remove('#');
first.AddRange(subFirst);
first.AddRange(GetFirst(node.Right, 1));
}
else
{
first.AddRange(subFirst);
}
}
}
}
}
first = first.Distinct().ToList();
return first;
}
``
原文地址: https://www.cveoy.top/t/topic/hjPC 著作权归作者所有。请勿转载和采集!