SLR分析表构建代码详解及错误修正
SLR分析表构建代码详解及错误修正
代码分析
该代码实现了SLR分析表的构建,包含了以下几个主要部分:
- 数据结构定义: 定义了SLRNode、SLRitemsets、DFA、Table等数据结构,分别用于存储产生式、项目集合、DFA状态、分析表项等信息。
- SLR分析表构建: SLRAnaly()函数用于构建SLR分析表,主要步骤包括:
- 初始化分析表,将所有项都设置为ERROR。
- 为接受项目添加状态。
- 为归约项目集合添加状态,根据每个归约项目的follow集合,为其对应的终结符添加状态。
- 为DFA中的每个状态添加状态,根据其对应的symbol,为其对应的终结符或非终结符添加状态。
- 辅助函数: 定义了isFinalsymbol()、GetFollow()、GetFirst()等辅助函数,用于判断符号类型、计算follow集合、计算first集合。
错误分析
根据错误信息可以看出,SLR分析表的构建出现了问题,具体表现为:
- 状态5的分析表中,对于括号、数字和#的处理出现了问题,应该为空,但是却都被赋值为r6;
- 状态9、10、11的分析表中,对于括号、数字和#的处理也出现了类似的问题,应该为空,但是被赋值为了相同的值。
错误修正方案
根据上述分析,可以对代码进行如下修改:
- 在SLR分析表构建的过程中,对于状态中没有对应操作的终结符,应该将其对应的分析表项赋值为空,而不是赋值为r6。可以在SLRAnaly()函数中添加如下代码:
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 += " " + " "; // 修改部分
}
- 在SLR分析表构建的过程中,对于非终结符,应该避免多次赋值的情况。可以在SLRAnaly()函数中对赋值过程进行优化,避免重复赋值。具体来说,可以在为归约项目集合添加状态时,仅为其follow集合中的终结符赋值,而不是为所有终结符都赋值。修改部分代码如下:
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));
}
}
}
}
- 在SLR分析表构建的过程中,需要注意对于终结符和非终结符的区分。具体来说,可以在isFinalsymbol()函数中添加对于非终结符的判断,避免将其误认为是终结符。修改部分代码如下:
public bool isFinalsymbol(char c)
{
if (c >= 'A' && c <= 'Z') // 修改部分
return false;
else if (c == '+' || c == '*' || c == '(' || c == ')' || c == 'd' || c == '#')
return true;
else
return false;
}
修改后的代码
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 DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] SLRAna;//分析表
public Analyze Jz;
public bool Success = false;
public List<SLRNode> SLRproNum = new List<SLRNode>(50);//产生式 列表
public List<SLRNode> SLRobjNum = new List<SLRNode>(50);//项目 列表
public List<SLRitemsets> proitemset = new List<SLRitemsets>(100);//项目集合
public List<int> Gy_obj = new List<int>(50);//归约项目序号集合
public List<int> Gy_itemset = new List<int>(50);//含有归约项目的集合的序号 的集合
public List<char> Nchar = new List<char>(50);//非终结符集合
public List<char> Echar = new List<char>(50);//终结符集合
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);
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))//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 false;
else if (c == '+' || c == '*' || c == '(' || c == ')' || c == 'd' || c == '#')
return true;
else
return false;
}
总结
通过对代码进行分析和修改,成功解决了SLR分析表构建过程中出现的错误。通过代码的修正,可以更加准确地构建SLR分析表,进而实现语法分析的功能。
原文地址: https://www.cveoy.top/t/topic/f0Da 著作权归作者所有。请勿转载和采集!