SLR 文法分析表构造详解及代码实现 - C#
SLR 文法分析表构造详解及代码实现 - C#
本文详细介绍了 SLR 文法分析表的构造过程,并提供了 C# 代码实现。通过 SLRAnaly() 函数、follow 集构造函数和辅助函数,演示了如何根据 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 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 string RStr = '';
public string RStr_obitemset = '';//输出返回
public string RStr_DFA = '';
public string RStr_ANA = '';
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++)
{
int proNum = Find_pro(SLRobjNum[proitemset[Gy_itemset[i]].Container[0]]);
foreach (char c in Follow[Nchar[proNum]])//对于非终结符的follow集中的每个终结符
{
int j = FindID(Echar, c);
if (j != -1)//如果该终结符在Echar中存在
{
tnode = new Table('r', proNum);//归约项目 找到原产生式序号 添加状态r
SLRAna[Gy_itemset[i]][j] = tnode;
}
}
}
for (int i = 0; i < Pindex; i++)
{
if (isFinalsymbol(dfa[i].symbol))//symbol为非终结符 添加状态N
{
int CID = FindID(Nchar, dfa[i].symbol);
foreach (char c in Follow[Nchar[CID]])//对于非终结符的follow集中的每个终结符
{
int j = FindID(Echar, c);
if (j != -1)//如果该终结符在Echar中存在
{
tnode = new Table('N', dfa[i].to);
SLRAna[dfa[i].from][j + Echar.Count] = tnode;
}
}
}
else //不是归约项目 添加状态S
{
int CID = FindID(Echar, dfa[i].symbol);
tnode = new Table('S', dfa[i].to);
SLRAna[dfa[i].from][CID] = tnode;
}
}
}
//follow集构造函数
public void FollowSet(char c)
{
if (isFinalsymbol(c))//c为终结符 返回空集
return;
if (Follow.ContainsKey(c))//已经计算过了 直接返回
return;
List<char> followset = new List<char>(50);//follow集
if (c == SLRproNum[0].Left[0])//起始符号的follow集添加#
followset.Add('#');
for (int i = 0; i < SLRproNum.Count; i++)//遍历所有产生式
{
string right = SLRproNum[i].Right;
for (int j = 0; j < right.Length; j++)//遍历产生式右部
{
if (right[j] == c)//找到了c
{
if (j == right.Length - 1)//c是产生式右部的最后一个符号
{
if (SLRproNum[i].Left[0] != c)//如果该产生式左部不是c
{
FollowSet(SLRproNum[i].Left[0]);//递归计算左部的follow集
List<char> fl = Follow[SLRproNum[i].Left[0]];
followset.AddRange(fl);//将左部的follow集加入到c的follow集中
}
}
else//c不是产生式右部的最后一个符号
{
char next = right[j + 1];//next是c的后一个符号
if (isFinalsymbol(next))//next是终结符
{
followset.Add(next);//将next加入到c的follow集中
}
else//next是非终结符
{
FirstSet(next);//计算next的first集
List<char> fir = First[next];
for (int k = 0; k < fir.Count; k++)//将next的first集加入到c的follow集中
{
if (fir[k] != 'ε')
followset.Add(fir[k]);
}
if (fir.Contains('ε'))//如果next的first集中包含空串
{
if (SLRproNum[i].Left[0] != c)//如果该产生式左部不是c
{
FollowSet(SLRproNum[i].Left[0]);//递归计算左部的follow集
List<char> fl = Follow[SLRproNum[i].Left[0]];
followset.AddRange(fl);//将左部的follow集加入到c的follow集中
}
}
}
}
}
}
}
Follow[c] = followset;//将c的follow集加入到Follow字典中
}
//调用的各类函数
// FindID(char c, List<char> list):在list中查找字符c的下标
// Find_pro(SLRNode node):在SLRproNum中查找node对应的产生式的序号
// isFinalsymbol(char c):判断c是否为终结符
// FirstSet(char c):计算非终结符c的first集
// FollowSet(char c):计算非终结符c的follow集
代码说明:
-
SLRAnaly()函数用于构建 SLR 分析表。 -
FollowSet()函数用于计算非终结符的 follow 集。 -
辅助函数包括
FindID()、Find_pro()、isFinalsymbol()、FirstSet()等,用于辅助分析表构建。
代码示例:
// 初始化产生式
SLRproNum.Add(new SLRNode('E', 'E+T'));
SLRproNum.Add(new SLRNode('E', 'T'));
SLRproNum.Add(new SLRNode('T', 'T*F'));
SLRproNum.Add(new SLRNode('T', 'F'));
SLRproNum.Add(new SLRNode('F', '(E)'));
SLRproNum.Add(new SLRNode('F', 'id'));
// 初始化终结符
Echar.Add('+');
Echar.Add('*');
Echar.Add('(');
Echar.Add(')');
Echar.Add('id');
Echar.Add('#');
// 初始化非终结符
Nchar.Add('E');
Nchar.Add('T');
Nchar.Add('F');
// ...其他初始化代码...
// 计算follow集
for (int i = 0; i < Nchar.Count; i++)
{
FollowSet(Nchar[i]);
}
// 构建分析表
SLRAnaly();
总结:
本文详细介绍了 SLR 文法分析表的构造过程,并提供了 C# 代码实现。通过该代码,您可以更深入地理解 SLR 分析表的构建原理,并应用于实际的语法分析工作。
原文地址: https://www.cveoy.top/t/topic/f0uL 著作权归作者所有。请勿转载和采集!