SLR 分析表构建及 ListView 显示问题解析
分析:
-
SLR分析表的构建主要分为两部分:为归约项目添加r状态和为移进项目添加s状态。
-
在为归约项目添加r状态时,需要遍历每个非终结符的follow集合,对于每个follow符号,找到其在Echar中的位置,然后将对应的SLRAna[i][j]赋值为r状态,并将对应的产生式编号作为id存入。
-
在为移进项目添加s状态时,需要遍历DFA数组,对于每个移进项目,找到其对应的终结符在Echar中的位置,然后将对应的SLRAna[i][j]赋值为s状态,并将对应的DFA的to状态作为id存入。
-
在代码实现中,SLRAna数组的元素类型为Table,包含三个属性:error,type和id,其中error表示是否为ERROR,type表示状态类型(S或r或N或A),id表示状态编号或产生式编号。
-
在ListView中r为表格元素的对应项显示不正确的原因可能是因为在SLRAnaly函数中为归约项目添加r状态时,没有将对应的产生式编号存入Table的id属性中,而是存入了type属性中。因此需要将SLRAna[Gy_itemset[i]][CID] = new Table('r', Find_pro(item)); 改为SLRAna[Gy_itemset[i]][CID] = new Table('r', Find_pro(item)+1);。
修改后的代码如下:
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
//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 DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] SLRAna;//分析表
public bool Success = false;
public List
public List<char>[] Follow; //每个非终结符的follow集合
public string RStr = "";
public string RStr_obitemset = "";//输出返回
public string RStr_DFA = "";
public string RStr_ANA = "";
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++)//初始化 赋予ERROR属性
for (int j = 0; j < Echar.Count + Nchar.Count; j++)//为终结符加r状态
SLRAna[i][j] = tnode;
tnode = new Table('A', 0);
SLRAna[1][FindID(Echar, '#')] = tnode;//项目集1必定是接受项目 构建[1][#]:acc的情况 先直接赋值好 dfa里没有
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);
SLRAna[Gy_itemset[i]][CID] = new Table('r', Find_pro(item)+1);
}
}
for (int i = 0; i < Pindex; i++)
{
if (isFinalsymbol(dfa[i].symbol))//symbol为非终结符 添加状态N
{
int CID = FindID(Nchar, dfa[i].symbol);
SLRAna[dfa[i].from][CID + Echar.Count] = new Table('N', dfa[i].to);
}
else //不是归约项目 添加状态S
{
int CID = FindID(Echar, dfa[i].symbol);
SLRAna[dfa[i].from][CID] = new Table('S', dfa[i].to);
}
}
}
public List<char> GetFollow(char c)
{
List<char> follow = new List<char>();
if (c == 'E')
follow.Add('#');
foreach (SLRNode node in SLRproNum)
{
int index = node.Right.IndexOf(c);
if (index != -1 && index < node.Right.Length - 1)
{
char next = node.Right[index + 1];
if (isFinalsymbol(next))
follow.Add(next);
else
{
List<char> first = GetFirst(next);
if (first.Contains('#'))
{
first.Remove('#');
follow.AddRange(first);
follow.AddRange(GetFollow(node.Left[0]));
}
else
{
follow.AddRange(first);
}
}
}
else if (index != -1 && index == node.Right.Length - 1)
{
follow.AddRange(GetFollow(node.Left[0]));
}
}
follow = follow.Distinct().ToList();
return follow;
}
public List<char> GetFirst(char c)
{
List<char> first = new List<char>();
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;
}
public bool isFinalsymbol(char c)
{
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
return false;
return false;
}
public int FindID(List<char> list, char c)
{
int i;
for (i = 0; i < list.Count; i++)
{
if (list[i] == c)
break;
}
return i;
}
public int Find_pro(SLRNode node)
{
int i;
for (i = 0; i < SLRproNum.Count; i++)
{
if (SLRproNum[i].Left == node.Left && SLRproNum[i].Right == node.Right)
break;
}
return i;
原文地址: https://www.cveoy.top/t/topic/f0yc 著作权归作者所有。请勿转载和采集!