SLR 分析表构建算法实现 - C# 代码示例
{ "title": "class SLR_fourpro\n {\n public int is_SLR = -1;\n public isLR_0_ islR0;\n SLR SLR;\n\n public SLR_fourpro(string text)\n {\n this.islR0 = new isLR_0_(text);\n if (islR0.is_LR != -2)\n {\n this.is_SLR = islR0.is_LR;\n return;\n }\n SLR = new SLR(islR0);\n is_SLR = SLR.judgeSLR() ? 1 : -2;\n if (is_SLR == 1)\n {\n SLR.buildtable();\n }\n\n }\n\n public SLR getSLR()\n {\n return SLR;\n }\n }\n\n class SLR\n {\n isLR_0_ isLR_0;\n public List
for (int i = 0; i < states.Count; i++)
{
//对每个状态经过终结符的情况进行判断
List<string> strings = new List<string>();
foreach (var symbol in terminals)
{
flag = 0;
//包含移进项的
if (transitions.ContainsKey(i))
{
foreach (var item in transitions[i])
{
if (item[0].ToString().Equals(symbol))
{
strings.Add('S' + item.Substring(1));
flag = 1;
break;
}
}
foreach (var item in states[i].items)
{
if (item.dotIndex == item.RHS.Count&& !item.LHS.Equals(production.Keys.First() + '''))
{
if (isLL_1_.follow.getfollows()[item.LHS].Contains(symbol))
{
flag = 1;
int index = getproconut(item);
strings.Add("r" + index);
break;
}
}
}
if (flag == 0)
{
if (states[i].items.First().LHS.Equals(production.Keys.First() + ''' )&&i!=0)
{
if (symbol.Equals("#")) strings.Add("acc");
else strings.Add("");
}
else strings.Add("");
}
}
//只有归约项的
else
{
if (states[i].items.First().LHS.Equals(production.Keys.First() + '''))
{
if (symbol.Equals("#")) strings.Add("acc");
else strings.Add("");
}
else
{
flag = 0;
foreach (var item in states[i].items)
{
//如果该终结集在FOLLOW集中则加入分析表
if (isLL_1_.follow.getfollows()[item.LHS].Contains(symbol) || symbol.Equals('#'))
{
flag = 1;
int index = getproconut(item);
strings.Add("r" + index);
break;
}
}
if (flag==0) strings.Add("");
}
}
}
//对每个状态经过非终结符的情况进行判断
foreach (var t in nonterminal)
{
flag = 0;
if (transitions.ContainsKey(i))
{
foreach (var item in transitions[i])
{
if (item[0].ToString().Equals(t))
{
strings.Add(item.Substring(1));
flag = 1;
break;
}
}
if (flag == 0) strings.Add("");
}
else strings.Add("");
}
table.Add(i, strings);
}
}
}
请仿照上述代码补充适合下列变量定义的实现SLR分析表构建功能的代码,请给出函数SLRAnaly()及相关调用的函数代码以及给出函数中下面未给出但使用到了的变量定义代码
class 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
//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 Table[][] SLRAna;//分析表
public void SLRAnaly() {
}
}
内容:变量定义代码:
class SLR { public int is_SLR = -1; public isLR_0_ islR0; SLR SLR;
// 新增变量定义
public List<string> terminals; // 终结符集合
public List<string> nonterminal;// 非终结符集合
public Dictionary<string, List<string>> production;//继承LL1中的原始产生式
public Dictionary<string, List<List<string>>> productions; // 产生式规则(包含全部规则)
public Dictionary<int, HashSet<string>> transitions; // 状态转移函数
public Dictionary<ItemSet, int> stateNumbers; // 状态编号
public Dictionary<int, ItemSet> states; // 状态集合
public Dictionary<int, List<string>> table;
// 新增变量定义
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);
//记录项目在项目集合中的序号
}
// 新增变量定义
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;
}
}
}
SLR分析表构建函数代码:
public void SLRAnaly() { // 继承isLR_0_中的相关变量 terminals = isLR_0.getLR0().terminals; nonterminal = isLR_0.getLR0().nonterminal; productions = isLR_0.getLR0().productions; production = isLR_0.getLR0().production; transitions = isLR_0.getLR0().transitions; stateNumbers = isLR_0.getLR0().stateNumbers; states = isLR_0.getLR0().states;
// 构建SLR分析表
int flag = 0;
SLRAna = new Table[states.Count][];
for (int i = 0; i < states.Count; i++)
{
SLRAna[i] = new Table[terminals.Count + nonterminal.Count];
//对每个状态经过终结符的情况进行判断
for (int j = 0; j < terminals.Count; j++)
{
flag = 0;
//包含移进项的
if (transitions.ContainsKey(i))
{
foreach (var item in transitions[i])
{
if (item[0].ToString().Equals(terminals[j]))
{
SLRAna[i][j] = new Table('S', int.Parse(item.Substring(1)));
flag = 1;
break;
}
}
foreach (var item in states[i].items)
{
if (item.dotIndex == item.RHS.Count && !item.LHS.Equals(production.Keys.First() + '''))
{
if (isLL_1_.follow.getfollows()[item.LHS].Contains(terminals[j]))
{
flag = 1;
int index = getproconut(item);
SLRAna[i][j] = new Table('r', index);
break;
}
}
}
if (flag == 0)
{
if (states[i].items.First().LHS.Equals(production.Keys.First() + ''' )&& i != 0)
{
if (terminals[j].Equals("#")) SLRAna[i][j] = new Table('a', 0);
else SLRAna[i][j] = new Table();
}
else SLRAna[i][j] = new Table();
}
}
//只有归约项的
else
{
if (states[i].items.First().LHS.Equals(production.Keys.First() + '''))
{
if (terminals[j].Equals("#")) SLRAna[i][j] = new Table('a', 0);
else SLRAna[i][j] = new Table();
}
else
{
flag = 0;
foreach (var item in states[i].items)
{
//如果该终结集在FOLLOW集中则加入分析表
if (isLL_1_.follow.getfollows()[item.LHS].Contains(terminals[j]) || terminals[j].Equals('#'))
{
flag = 1;
int index = getproconut(item);
SLRAna[i][j] = new Table('r', index);
break;
}
}
if (flag == 0) SLRAna[i][j] = new Table();
}
}
}
//对每个状态经过非终结符的情况进行判断
for (int j = 0; j < nonterminal.Count; j++)
{
flag = 0;
if (transitions.ContainsKey(i))
{
foreach (var item in transitions[i])
{
if (item[0].ToString().Equals(nonterminal[j]))
{
SLRAna[i][terminals.Count + j] = new Table('G', int.Parse(item.Substring(1)));
flag = 1;
break;
}
}
if (flag == 0) SLRAna[i][terminals.Count + j] = new Table();
}
else SLRAna[i][terminals.Count + j] = new Table();
}
}
}
原文地址: https://www.cveoy.top/t/topic/f1Pp 著作权归作者所有。请勿转载和采集!