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 class Analyze
    {
        public List<string> stack_state = new List<string>(100);//记录状态栈
        public List<string> stack_symbol = new List<string>(100);//记录符号栈
        public List<string> Input_str = new List<string>(100);//记录输入串
        public List<string> Tran_pro = new List<string>(100);//记录所用产生式
    }

    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);//终结符集合

    Dictionary<char, HashSet<char>> follow = new Dictionary<char, HashSet<char>>();//非终结符的Follow集

    public string RStr = "";
    public string RStr_obitemset = "";//输出返回
    public string RStr_DFA = "";
    public string RStr_ANA = "";


    public void Buildprod(string str)//创建项目集
    {
        string[] prod = str.Split(new char[] { '@' });
        foreach (string s in prod)
        {
            string[] prod_part = s.Split(new char[] { '-' });
            if (prod_part.Length == 2 && prod_part[0] != "" && prod_part[1] != "")
            {
                SLRNode node = new SLRNode(prod_part[0], prod_part[1]);
                SLRproNum.Add(node);
            }
        }
    }

    //求项目集
    public void Creteitemsets()
    {
        //添加S'->S
        SLRNode node = new SLRNode("S'", SLRproNum[0].Left);
        SLRproNum.Insert(0, node);

        //添加·
        for (int i = 0; i < SLRproNum.Count; i++)
        {
            for (int j = 0; j <= SLRproNum[i].Right.Length; j++)
            {
                string temp_str = SLRproNum[i].Right.Insert(j, "·");
                SLRNode node_temp = new SLRNode(SLRproNum[i].Left, temp_str);
                SLRobjNum.Add(node_temp);
            }
        }

        //添加第一个项目集
        SLRitemsets temp_itemset = new SLRitemsets();
        temp_itemset.Container.Add(0);
        proitemset.Add(temp_itemset);

        //求项目集族
        for (int i = 0; i < proitemset.Count; i++)
        {
            //求项目集i的闭包
            Dictionary<char, List<int>> temp_dic = new Dictionary<char, List<int>>();
            foreach (int index in proitemset[i].Container)
            {
                if (SLRobjNum[index].Right.IndexOf("·") < SLRobjNum[index].Right.Length - 1)//·不在产生式末尾
                {
                    char t_char = SLRobjNum[index].Right[SLRobjNum[index].Right.IndexOf("·") + 1];
                    if (!temp_dic.ContainsKey(t_char))
                    {
                        temp_dic[t_char] = new List<int>();
                    }
                    temp_dic[t_char].Add(index);
                }
            }
            while (temp_dic.Count > 0)
            {
                Dictionary<char, List<int>> new_dic = new Dictionary<char, List<int>>();
                foreach (char c in temp_dic.Keys)
                {
                    List<int> t_list = new List<int>();
                    foreach (int index in temp_dic[c])
                    {
                        if (SLRobjNum[index].Right.IndexOf("·") < SLRobjNum[index].Right.Length - 1)//·不在产生式末尾
                        {
                            char t_char = SLRobjNum[index].Right[SLRobjNum[index].Right.IndexOf("·") + 1];
                            if (!new_dic.ContainsKey(t_char))
                            {
                                new_dic[t_char] = new List<int>();
                            }
                            if (!t_list.Contains(index))
                            {
                                t_list.Add(index);
                            }
                        }
                    }
                    foreach (int index in t_list)
                    {
                        new_dic[c].Add(index);
                    }
                }
                foreach (char c in new_dic.Keys)
                {
                    bool flag = false;
                    SLRitemsets temp_itemset2 = new SLRitemsets();
                    foreach (int index in new_dic[c])
                    {
                        temp_itemset2.Container.Add(index);
                        if (SLRobjNum[index].Right.IndexOf("·") < SLRobjNum[index].Right.Length - 1)//·不在产生式末尾
                        {
                            flag = true;
                        }
                    }
                    if (flag)
                    {
                        proitemset.Add(temp_itemset2);
                    }
                }
                temp_dic = new_dic;
            }
        }

        //求每个项目集状态
        for (int i = 0; i < proitemset.Count; i++)
        {
            for (int j = 0; j < Echar.Count; j++)
            {
                SLRitemsets temp_itemset = new SLRitemsets();
                for (int k = 0; k < proitemset[i].Container.Count; k++)
                {
                    int index = proitemset[i].Container[k];
                    if (SLRobjNum[index].Right.IndexOf("·") < SLRobjNum[index].Right.Length - 1)//·不在产生式末尾
                    {
                        if (SLRobjNum[index].Right[SLRobjNum[index].Right.IndexOf("·") + 1] == Echar[j])
                        {
                            SLRNode temp_node = new SLRNode(SLRobjNum[index].Left, SLRobjNum[index].Right);
                            temp_node.Right = temp_node.Right.Remove(temp_node.Right.IndexOf("·"), 1);
                            temp_node.Right = temp_node.Right.Insert(temp_node.Right.IndexOf("·")
class SLR_fourpro public int is_SLR = -1; public isLR_0_ islR0; SLR SLR; public SLR_fourprostring text thisislR0 = new isLR_0_text; if isl

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

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