SLR(1) 分析表构建 - LR0 文法代码示例
{
//产生式结点类
public class LRNode
{
public string Left;
public string Right;
public LRNode(string Left, string Right)
{
this.Left = Left;
this.Right = Right;
}
}
//项目集类
public class LRitemsets
{
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 DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] LRAna;//分析表
public Analyze Jz;
public bool Success = false;
public List<LRNode> LRproNum = new List<LRNode>(50);//产生式 列表
public List<LRNode> LRobjNum = new List<LRNode>(50);//项目 列表
public List<LRitemsets> proitemset = new List<LRitemsets>(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 Table[][] GET_ANA()
{
LRAnaly();
RStr_ANA += '\r\nLR0分析表:\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 (LRAna[i][j].error)
{
RStr_ANA += ' ' + ' ';
}
else if (i == 1 && j == Echar.Count - 1)
{
RStr_ANA += 'AC' + ' ';
}
else if (LRAna[i][j].type != 'N')
{
RStr_ANA += LRAna[i][j].type.ToString() + LRAna[i][j].id.ToString() + ' ';
}
else
RStr_ANA += LRAna[i][j].id.ToString() + ' ';
}
RStr_ANA += '\r\n';
}
return LRAna;
}
//分析表
public void LRAnaly()
{
Table tnode = new Table();
LRAna = new Table[proitemset.Count][];
for (int i = 0; i < proitemset.Count; i++)
LRAna[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状态
LRAna[i][j] = tnode;
tnode = new Table('A', 0);
LRAna[1][FindID(Echar, '#')] = tnode;//项目集1必定是接受项目 构建[1][#]:acc的情况 先直接赋值好 dfa里没有
for (int i = 0; i < Gy_itemset.Count; i++)
{
tnode = new Table('r', Find_pro(LRobjNum[proitemset[Gy_itemset[i]].Container[0]]));//归约项目 找到原产生式序号 添加状态r
for (int j = 0; j < Echar.Count; j++)
{
LRAna[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);
tnode = new Table('N', dfa[i].to);
LRAna[dfa[i].from][CID + Echar.Count] = tnode;
}
else //不是归约项目 添加状态S
{
int CID = FindID(Echar, dfa[i].symbol);
tnode = new Table('S', dfa[i].to);
LRAna[dfa[i].from][CID] = tnode;
}
}
}
// 以下是需要补充的函数
// 1. 计算 First 集
public List<char> First(string str)
{
// ...
}
// 2. 计算 Follow 集
public List<char> Follow(char A)
{
// ...
}
// 3. 判断符号是否为终结符
public bool isFinalsymbol(char symbol)
{
// ...
}
// 4. 找到产生式在列表中的序号
public int Find_pro(LRNode pro)
{
// ...
}
// 5. 找到符号在列表中的序号
public int FindID(List<char> list, char symbol)
{
// ...
}
// 6. 判断项目是否为归约项目
public bool isGyItem(int itemIndex)
{
// ...
}
// 7. 构建 SLR(1) 分析表
public void SLR1Analy()
{
// ...
}
} '
代码解释:
-
结构定义: 代码中定义了 LR0 文法相关的结构,包括: -
LRNode: 产生式结点类 -LRitemsets: 项目集类 -DFA: DFA 结点类 -Table: 分析表结点类 -
LR0 分析表构建: -
LRAnaly函数实现了 LR0 分析表构建的逻辑,使用dfa数组存储 DFA 信息,并将分析表信息存储在LRAna数组中。 -
SLR(1) 分析表构建: 代码中缺少 SLR(1) 分析表构建的函数,需要补充以下几个核心函数: -
First(string str): 计算符号串的 First 集 -Follow(char A): 计算非终结符的 Follow 集 -isFinalsymbol(char symbol): 判断符号是否为终结符 -Find_pro(LRNode pro): 找到产生式在列表中的序号 -FindID(List<char> list, char symbol): 找到符号在列表中的序号 -isGyItem(int itemIndex): 判断项目是否为归约项目 -SLR1Analy:构建 SLR(1) 分析表
补充说明:
- 代码中没有提供 LR0 文法规则,需要根据具体文法规则来实现
First,Follow等函数。*SLR1Analy函数的实现逻辑需要基于 LR0 分析表,并根据 Follow 集和归约项目来调整分析表。
示例:
假设有一个 LR0 文法:
S -> ABA -> aB -> b
可以根据这个文法来补充代码中的函数实现,例如:csharppublic List
// ... 其它函数实现 .
原文地址: https://www.cveoy.top/t/topic/f0LY 著作权归作者所有。请勿转载和采集!