SLR 语法分析表构建算法与实现
// 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 SLRAnalyzer
{
// ... 其他成员变量和函数 ...
public Table[][] GET_ANA()
{
SLRAnaly();
RStr_ANA += '\r\nSLR0分析表:\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 (SLRAna[i][j].error)
{
RStr_ANA += ' ' + ' ';
}
else if (i == 1 && j == Echar.Count - 1)
{
RStr_ANA += 'AC' + ' ';
}
else if (SLRAna[i][j].type != 'N')
{
RStr_ANA += SLRAna[i][j].type.ToString() + SLRAna[i][j].id.ToString() + ' ';
}
else
RStr_ANA += SLRAna[i][j].id.ToString() + ' ';
}
RStr_ANA += '\r\n';
}
return SLRAna;
}
public void SLRAnaly()
{
Table tnode = new Table();
SLRAna = new Table[proitemset.Count][];
for (int i = 0; i < proitemset.Count; i++)
SLRAna[i] = new Table[Echar.Count + Nchar.Count];
for (int i = 0; i < proitemset.Count; i++)
for (int j = 0; j < Echar.Count + Nchar.Count; j++)
SLRAna[i][j] = tnode;
tnode = new Table('A', 0);
SLRAna[1][FindID(Echar, '#')] = tnode;
// ... 其他代码 ...
for (int i = 0; i < Gy_itemset.Count; i++)
{
SLRNode item = SLRobjNum[proitemset[Gy_itemset[i]].Container[0]];
char left = item.Left[0];
List<char> follow = GetFollow(left);
foreach (char c in follow)
{
int CID = FindID(Echar, c);
// 避免重复赋值
if (SLRAna[Gy_itemset[i]][CID].error)
{
SLRAna[Gy_itemset[i]][CID] = new Table('r', Find_pro(item));
}
if (c == '#')
{
foreach (char e in Echar)
{
int EID = FindID(Echar, e);
// 避免重复赋值
if (SLRAna[Gy_itemset[i]][EID].error)
{
SLRAna[Gy_itemset[i]][EID] = new Table('r', Find_pro(item));
}
}
}
}
}
// ... 其他代码 ...
for (int i = 0; i < Pindex; i++)
{
if (isFinalsymbol(dfa[i].symbol))
{
int CID = FindID(Nchar, dfa[i].symbol);
// 避免重复赋值
if (SLRAna[dfa[i].from][CID + Echar.Count].error)
{
SLRAna[dfa[i].from][CID + Echar.Count] = new Table('N', dfa[i].to);
}
}
else
{
int CID = FindID(Echar, dfa[i].symbol);
// 避免重复赋值
if (SLRAna[dfa[i].from][CID].error)
{
SLRAna[dfa[i].from][CID] = new Table('S', dfa[i].to);
}
}
}
}
// ... 其他成员变量和函数 ...
}
问题分析:
问题出现在 SLRAnaly() 函数中构建分析表的逻辑。在为分析表中的每个状态和符号赋值时,代码没有判断该位置是否已经有了有效值,导致同一个符号在同一个状态下被重复赋值多次,最终在 ListView 中出现重复显示的问题。
解决方法:
在为分析表赋值之前,先判断该位置是否已经有了有效值。如果已经有了有效值,则不再进行赋值。
修改后的代码:
在上述代码中,我们在 SLRAnaly() 函数的三个循环中,都添加了 if (SLRAna[...][...].error) 的判断条件,只有当该位置仍然是初始的 error 状态时,才进行赋值操作。
其他说明:
除了上述代码修改外,还可以通过其他方式解决这个问题,例如:
- 使用字典类型存储分析表,而不是二维数组,这样可以避免重复赋值的问题。
- 在构建 DFA 的过程中,就将分析表构建好,这样可以避免在
SLRAnaly()函数中再次遍历 DFA 节点。
希望以上分析和修改能够帮助你解决问题。
原文地址: https://www.cveoy.top/t/topic/f0Ey 著作权归作者所有。请勿转载和采集!