{ "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 terminals; // 终结符集合\n public List nonterminal;// 非终结符集合\n public Dictionary<string, List> production;//继承LL1中的原始产生式\n public Dictionary<string, List<List>> productions; // 产生式规则(包含全部规则)\n\n public Dictionary<int, HashSet> transitions; // 状态转移函数\n public Dictionary<ItemSet, int> stateNumbers; // 状态编号\n public Dictionary<int, ItemSet> states; // 状态集合\n public Dictionary<int, List> table;\n\n public SLR(isLR_0_ isLR_0_) //判断是否为LR(0)文法\n {\n isLR_0 = isLR_0_;\n terminals = isLR_0.getLR0().terminals;\n nonterminal = isLR_0.getLR0().nonterminal;\n productions = isLR_0.getLR0().productions;\n production = isLR_0.getLR0().production;\n transitions = isLR_0.getLR0().transitions;\n stateNumbers = isLR_0.getLR0().stateNumbers;\n states = isLR_0.getLR0().states;\n }\n\n public bool judgeSLR()\n {\n isLL_1_ isLL_1_ = isLR_0.isLL_1_;\n List<List> back;\n List put;\n\n foreach (var item in stateNumbers.Keys)\n {\n back = new List<List>();\n put = new List();\n foreach (var pro in item.items)\n {\n if (pro.dotIndex == pro.RHS.Count)\n {\n if(!pro.LHS.Equals(productions.Keys.First() + '''))\n back.Add(isLL_1_.follow.getfollows()[pro.LHS]);\n } \n else if (terminals.Contains(pro.RHS[pro.dotIndex])) put.Add(pro.RHS[pro.dotIndex]);\n }\n //看移进归约冲突能否解决\n if (back.Count > 0 && put.Count > 0)\n {\n //遍历每个归约项FOLLOW集\n foreach(var item1 in back)\n {\n //遍历每个移进项FIRST集\n foreach(var item2 in put)\n {\n if(item1.Contains(item2))return false;\n }\n }\n }\n //看归约归约冲突能否解决\n if (back.Count >= 2)\n {\n for (int i=0;i<back.Count-1;i++)\n {\n //对之后的FOLLOW集进行查询\n for(int j = i+1; j < back.Count; j++)\n {\n //看当前FOLLOW与之后FOLLW集是否有交集\n foreach(var item3 in back[i])\n if (back[j].Contains(item3)) return false;\n }\n }\n }\n }\n return true;\n }\n\n private int getproconut(Item item)\n {\n //获取对应的产生式编号\n int index = 1;\n if (item.dotIndex == item.RHS.Count)\n {\n foreach (var key in productions.Keys)\n {\n if (key.Equals(item.LHS))\n {\n //查找当前左部中符合item的产生式\n foreach (var item2 in productions[key])\n {\n if (item.RHS.Equals(item2)) return index;\n else index++;\n }\n }\n index += productions[key].Count;\n }\n return index;\n }\n return -1;\n }\n\n public void buildtable()//构造分析表\n {\n isLL_1_ isLL_1_ = isLR_0.isLL_1_;\n int flag = 0;\n table = new Dictionary<int, 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 Container = new List(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 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();
    }
}

}

SLR 分析表构建算法实现 - C# 代码示例

原文地址: https://www.cveoy.top/t/topic/f1Pp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录