LR0文法分析表构建算法实现
public void SLRAnaly()
{
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;
}
}
//SLR1分析表构建
Follow(); //计算Follow集
for (int i = 0; i < proitemset.Count; i++)
{
for (int j = 0; j < proitemset[i].Container.Count; j++)
{
int itemIndex = proitemset[i].Container[j];
if (LRobjNum[itemIndex].Right.Length == 1 && LRobjNum[itemIndex].Right[0] == '.')
{
//找到归约项目
int proIndex = Find_pro(LRobjNum[itemIndex]); //找到对应的产生式序号
HashSet<char> followSet = follow[LRobjNum[itemIndex].Left[0]]; //获取产生式左部的Follow集
foreach (char followSymbol in followSet)
{
int followSymbolID = FindID(Echar, followSymbol); //找到Follow集中的符号在终结符表中的序号
LRAna[i][followSymbolID] = new Table('r', proIndex);
}
}
}
}
}
//计算Follow集
public void Follow()
{
Dictionary<char, HashSet<char>> follow = new Dictionary<char, HashSet<char>>();//非终结符的Follow集
foreach (char c in Nchar)
{
follow[c] = new HashSet<char>();
}
follow[Nchar[0]].Add('#');//将开始符号的Follow集加入#
bool flag = true;
while (flag)
{
flag = false;
foreach (LRNode lr in LRproNum)//遍历每个产生式
{
string right = lr.Right;
int len = right.Length;
for (int i = 0; i < len; i++)
{
if (isFinalsymbol(right[i])) continue;//终结符不考虑
HashSet<char> fset = follow[right[i]];
if (i == len - 1)//如果是产生式右边的最后一个符号
{
HashSet<char> fset2 = follow[lr.Left[0]];
if (fset2.IsSupersetOf(fset)) continue;//如果已经包含了就不用再添加了
fset2.UnionWith(fset);
follow[lr.Left[0]] = fset2;
flag = true;
}
else
{
HashSet<char> fset2 = first(right.Substring(i + 1));
if (fset2.Contains('#'))//如果后继符号可以为空
{
fset2.Remove('#');
fset2.UnionWith(follow[lr.Left[0]]);
}
if (fset2.IsSubsetOf(fset)) continue;//如果已经包含了就不用再添加了
fset.UnionWith(fset2);
follow[right[i]] = fset;
flag = true;
}
}
}
}
//输出Follow集
RStr += "\r\nFollow集:\r\n";
foreach (char c in Nchar)
{
RStr += c + ": { ";
foreach (char f in follow[c])
{
RStr += f + " ";
}
RStr += "}";
}
}
//计算一个字符串的First集
public HashSet<char> first(string str)
{
HashSet<char> fset = new HashSet<char>();
if (str.Length == 0) return fset;
if (isFinalsymbol(str[0]))
{
fset.Add(str[0]);
}
else
{
HashSet<char> fset2 = first(str.Substring(1));
fset.UnionWith(fset2);
if (fset2.Contains('#'))
{
fset.Remove('#');
fset.UnionWith(first(str.Substring(1)));
}
}
return fset;
}
原文地址: https://www.cveoy.top/t/topic/f0Mf 著作权归作者所有。请勿转载和采集!