public class SLRNode { public string Left; public string Right; public HashSet First; public HashSet Follow; public SLRNode(string Left, string Right) { this.Left = Left; this.Right = Right; this.First = new HashSet(); this.Follow = new HashSet(); } } //项目集类 public class SLRitemsets { public List Container = new List(100); //记录项目在项目集合中的序号 public Dictionary<char, int> Goto = new Dictionary<char, int>(); //记录转移符号及转移后的项目集合序号 }

    //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 Table[][] GET_ANA()
    {
        SLRAnaly();
        RStr_ANA += "\r\nSLR1分析表:\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 (SLRAna[i][j].error)
                {
                    RStr_ANA += "  " + "    ";
                }
                else if (i == 1 && j == Echar.Count - 1)
                {
                    RStr_ANA += "AC" + "    ";
                }
                else if (SLRAna[i][j].type != 'N')
                {
                    RStr_ANA += SLRAna[i][j].type.ToString() + SLRAna[i][j].id.ToString() + "    ";
                }
                else
                    RStr_ANA += SLRAna[i][j].id.ToString() + "    ";
            }
            RStr_ANA += "\r\n";
        }

        return SLRAna;

    }

    public void SLRAnaly()
    {
        //初始化
        proitemset.Clear();
        Gy_obj.Clear();
        Gy_itemset.Clear();
        Nchar.Clear();
        Echar.Clear();
        follow.Clear();
        SLRAna = new Table[100][];
        for (int i = 0; i < 100; i++)
        {
            SLRAna[i] = new Table[50];
            for (int j = 0; j < 50; j++)
            {
                SLRAna[i][j] = new Table();
            }
        }
        Pindex = 0;
        Success = false;

        //构造LR(0)项目集规范族
        LR0itemset();

        //构造SLR(1)项目集规范族
        SLRitemset();

        //构造DFA
        SLRDFA();

        //构造分析表
        SLRTable();
    }

    public void SLRitemset()
    {
        //初始化
        SLRobjNum.Clear();
        for (int i = 0; i < proitemset.Count; i++)
        {
            for (int j = 0; j < proitemset[i].Container.Count; j++)
            {
                SLRobjNum.Add(new SLRNode(LRobjNum[proitemset[i].Container[j]].Left, LRobjNum[proitemset[i].Container[j]].Right));
            }
        }

        //构造每个项目的First集
        First();

        //构造每个非终结符的Follow集
        Follow();

        //构造每个项目的Follow集
        for (int i = 0; i < SLRobjNum.Count; i++)
        {
            FollowOfNode(i);
        }

        //构造每个项目集的Goto表
        for (int i = 0; i < proitemset.Count; i++)
        {
            for (int j = 0; j < SLRobjNum.Count; j++)
            {
                if (proitemset[i].Container.Contains(j))
                {
                    if (SLRobjNum[j].Right.IndexOf('.') == SLRobjNum[j].Right.Length - 1)
                    {
                        //如果该项目是归约项目,则不需要进行转移
                        continue;
                    }
                    char symbol = SLRobjNum[j].Right[SLRobjNum[j].Right.IndexOf('.') + 1];
                    if (!Echar.Contains(symbol))
                    {
                        //如果该项目的下一个符号是非终结符,则不需要进行转移
                        continue;
                    }
                    int nextitemset = Goto(i, symbol);
                    if (nextitemset != -1)
                    {
                        proitemset[i].Goto[symbol] = nextitemset;
                    }
                }
            }
        }
    }

    public void Follow()
    {
        //初始化
        foreach (char c in Nchar)
        {
            follow[c] = new HashSet<char>();
        }
        follow[SLRproNum[0].Left].Add('#');

        bool changed = true;
        while (changed)
        {
            changed = false;
            for (int i = 0; i < SLRproNum.Count; i++)
            {
                for (int j = 0; j < SLRproNum[i].Right.Length; j++)
                {
                    if (isNonterminal(SLRproNum[i].Right[j]))
                    {
                        //如果该符号是非终结符
                        HashSet<char> tmp = new HashSet<char>();
                        int k = j + 1;
                        while (k < SLRproNum[i].Right.Length && SLRproNum[i].Right[k] == '\'')
                        {
                            k += 2;
                        }
                        if (k ==

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

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