SLR 分析表构建 C# 代码实现
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 Analyze
{
public DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] SLRAna;//分析表
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[][] GetAna()
{
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; 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 == 'S')
{
RStr_ANA += SLRAna[i][j].type.ToString() + SLRAna[i][j].id.ToString() + ' ';
}
else
{
RStr_ANA += ' ' + ' ';
}
}
for (int j = 0; j < Nchar.Count; j++)
{
if (SLRAna[i][j + Echar.Count].error)
{
RStr_ANA += ' ' + ' ';
}
else if (SLRAna[i][j + Echar.Count].type == 'r')
{
RStr_ANA += SLRAna[i][j + Echar.Count].type.ToString() + SLRAna[i][j + Echar.Count].id.ToString() + ' ';
}
else if (SLRAna[i][j + Echar.Count].type == 'N')
{
RStr_ANA += SLRAna[i][j + Echar.Count].id.ToString() + ' ';
}
else
{
RStr_ANA += ' ' + ' ';
}
}
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 j = 0; j < Echar.Count + Nchar.Count; j++)
{
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));
if (c == '#')
{
foreach (char e in Echar)
{
int EID = FindID(Echar, e);
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)
}
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 int FindID(List<char> list, char c)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i] == c)
{
return i;
}
}
return -1;
}
public bool isFinalsymbol(char c)
{
return Echar.Contains(c);
}
public int Find_pro(SLRNode node)
{
for (int i = 0; i < SLRproNum.Count; i++)
{
if (node.Left == SLRproNum[i].Left && node.Right == SLRproNum[i].Right)
{
return i;
}
}
return -1;
}
}
代码分析
该代码实现了一个简单的 SLR 分析表的构建过程。主要包含以下几个关键部分:
-
数据结构
SLRNode: 用于表示产生式,包含产生式的左部和右部。SLRitemsets: 用于表示项目集,包含该项目集中所有项目的序号。DFA: 用于表示 DFA 状态转移,包含起始状态、转移符号和目标状态。Table: 用于表示 SLR 分析表中的一个条目,包含状态类型、状态号和是否为错误状态。
-
SLR 分析表构建
SLRAnaly函数用于构建 SLR 分析表。- 首先,初始化分析表,将所有条目都设置为错误状态。
- 然后,将接受状态添加到分析表中,即项目集 1 的 # 状态。
- 接着,遍历所有含有归约项目的项目集,为每个项目集的 follow 集合中的符号添加归约状态。
- 最后,遍历 DFA 状态转移,为每个状态添加移进状态或非终结符状态。
-
辅助函数
GetFollow函数用于获取指定非终结符的 follow 集合。GetFirst函数用于获取指定非终结符的 first 集合。FindID函数用于在字符列表中查找指定字符的序号。isFinalsymbol函数用于判断指定字符是否为终结符。Find_pro函数用于在产生式列表中查找指定产生式的序号。
错误修改
在 GetAna 函数中,代码原本没有正确区分移进状态和归约状态。修改后的代码如下:
public Table[][] GetAna()
{
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; 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 == 'S')
{
RStr_ANA += SLRAna[i][j].type.ToString() + SLRAna[i][j].id.ToString() + ' ';
}
else
{
RStr_ANA += ' ' + ' ';
}
}
for (int j = 0; j < Nchar.Count; j++)
{
if (SLRAna[i][j + Echar.Count].error)
{
RStr_ANA += ' ' + ' ';
}
else if (SLRAna[i][j + Echar.Count].type == 'r')
{
RStr_ANA += SLRAna[i][j + Echar.Count].type.ToString() + SLRAna[i][j + Echar.Count].id.ToString() + ' ';
}
else if (SLRAna[i][j + Echar.Count].type == 'N')
{
RStr_ANA += SLRAna[i][j + Echar.Count].id.ToString() + ' ';
}
else
{
RStr_ANA += ' ' + ' ';
}
}
RStr_ANA += '\r\n';
}
return SLRAna;
}
代码中将移进状态和归约状态分别输出到不同的列,并使用type属性来区分它们,保证了分析表输出的正确性。
总结
这篇文章展示了一个简单的 SLR 分析表构建示例。通过代码分析,我们了解了 SLR 分析表构建过程中的关键步骤和辅助函数。同时,还通过改正错误代码,说明了如何在代码编写中避免输出错误。
注意事项
- 该代码仅供参考,需要根据实际情况进行调整。
- 代码中没有包含语法分析的具体实现,仅展示了分析表构建的过程。
- 为了更好地理解 SLR 分析表构建过程,建议参考相关的编译原理书籍或资料。
原文地址: https://www.cveoy.top/t/topic/f0CD 著作权归作者所有。请勿转载和采集!