{ "title": "SLR文法分析:实现SLR文法判断与分析表构建", "description": "本文提供C#代码实现SLR文法判断与分析表构建。代码包含SLRNode、SLRitemsets、DFA、Table等类定义,以及SLRAnaly()函数的详细实现。该代码能够根据输入的文法规则,判断是否为SLR文法,并构建相应的分析表。", "keywords": "SLR文法, LR分析, 编译原理, C#代码, 分析表构建", "content": "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()
    {
       
    }
}
   
    //产生式结点类
    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);
        //记录项目在项目集合中的序号
    }

    //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()
    {
       //构造项目集规范族
        List<SLRitemsets> itemsets = new List<SLRitemsets>();
        //构造DFA
        List<DFA> dfas = new List<DFA>();
        //构造分析表
        SLRAna = new Table[itemsets.Count][];
        for (int i = 0; i < itemsets.Count; i++)
        {
            SLRAna[i] = new Table[terminals.Count + nonterminal.Count];
            //填充分析表
            for (int j = 0; j < terminals.Count; j++)
            {
                //查找移进项
                int to = -1;
                foreach (var d in dfas)
                {
                    if (d.from == i && d.symbol == terminals[j][0])
                    {
                        to = d.to;
                        break;
                    }
                }
                if (to != -1)
                {
                    SLRAna[i][j] = new Table('s', to);
                }
                else
                {
                    SLRAna[i][j] = new Table();
                }
            }
            for (int j = 0; j < nonterminal.Count; j++)
            {
                int to = -1;
                foreach (var d in dfas)
                {
                    if (d.from == i && d.symbol == nonterminal[j][0])
                    {
                        to = d.to;
                        break;
                    }
                }
                if (to != -1)
                {
                    SLRAna[i][j + terminals.Count] = new Table('g', to);
                }
                else
                {
                    SLRAna[i][j + terminals.Count] = new Table();
                }
            }
            //查找归约项
            foreach (var item in itemsets[i].Container)
            {
                if (productions[item].Right[0] == '#')
                {
                    SLRAna[i][terminals.Count - 1] = new Table('r', item);
                }
                else if (productions[item].Right[0] == nonterminal[0][0])
                {
                    SLRAna[i][terminals.Count - 1] = new Table('r', item);
                }
            }
        }
    }
}
SLR文法分析:实现SLR文法判断与分析表构建

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

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