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; First = new HashSet(); Follow = new HashSet(); } } //项目集类 public class SLRitemsets { public List Container = new List(100); //记录项目在项目集合中的序号 public HashSet LookAhead = new HashSet(); //记录展望符 }

    //DFA结点
    public struct DFA
    {
        public int from;
        public char symbol;
        public int to;
        public HashSet<char> LookAhead;
        public DFA(int from, char symbol, int to, HashSet<char> LookAhead)
        {
            this.from = from;
            this.symbol = symbol;
            this.to = to;
            this.LookAhead = LookAhead;
        }
    }

    //分析表 结点
    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 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>> first = new Dictionary<char, HashSet<char>>();//非终结符的First集
    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 CreateDFA() { RStr_DFA += '\r\nSLR1分析器DFA:\r\n'; RStr_DFA += '状态|'; foreach (char c in Echar) { RStr_DFA += c + '|'; } foreach (char c in Nchar) { RStr_DFA += c + '|'; } RStr_DFA += '\r\n'; for (int i = 0; i < Pindex; i++) { RStr_DFA += i + ' |'; for (int j = 0; j < Echar.Count + Nchar.Count; j++) { if (dfa[i].to == -1) { RStr_DFA += ' |'; } else { if (dfa[i].symbol == (j < Echar.Count ? Echar[j] : Nchar[j - Echar.Count])) { RStr_DFA += dfa[i].to + ',' + string.Join('', dfa[i].LookAhead) + '|'; } else { RStr_DFA += ' |'; } } } RStr_DFA += '\r\n'; } } public void CreateItemsets() { //初始化第一个项目集 SLRitemsets itemset0 = new SLRitemsets(); itemset0.Container.Add(0); itemset0.LookAhead.Add('#'); itemset0 = Closure(itemset0); proitemset.Add(itemset0); //构造所有项目集 bool flag = true; while (flag) { flag = false; for (int i = 0; i < proitemset.Count; i++) { for (int j = 0; j < Echar.Count + Nchar.Count; j++) { char symbol = j < Echar.Count ? Echar[j] : Nchar[j - Echar.Count]; HashSet lookAhead = new HashSet(); foreach (int index in proitemset[i].Container) { if (SLRobjNum[index].Right.Contains('.') && SLRobjNum[index].Right.IndexOf('.') < SLRobjNum[index].Right.Length - 1 && SLRobjNum[index].Right[SLRobjNum[index].Right.IndexOf('.') + 1] == symbol) { if (SLRobjNum[index].Right.Length - 1 == SLRobjNum[index].Right.IndexOf('.') + 1) { lookAhead.UnionWith(proitemset[i].LookAhead); } else { bool canEpsilon = true; for (int k = SLRobjNum[index].Right.IndexOf('.') + 2; k < SLRobjNum[index].Right.Length; k++) { if (Echar.Contains(SLRobjNum[index].Right[k])) { lookAhead.UnionWith(first[SLRobjNum[index].Right[k]]); if (!first[SLRobjNum[index].Right[k]].Contains('ε')) { canEpsilon = false; break; } } else { canEpsilon = false; lookAhead.UnionWith(first[SLRobjNum[index].Right[k]]); break; } } if (canEpsilon) { lookAhead.UnionWith(proitemset[i].LookAhead); } } } } if (lookAhead.Count > 0) { SLRitemsets newItemset = new SLRitemsets(); foreach (int index in proitemset[i].Container) { if (SLRobjNum[index].Right.Contains('.') && SLRobjNum[index].Right.IndexOf('.') < SLRobjNum[index].Right.Length - 1 && SLRobjNum[index].Right[SLRobjNum[index].Right.IndexOf('.') + 1] == symbol) { SLRNode newNode = new SLRNode(SLRobjNum[index].Left, SLRobjNum[index].Right.Insert(SLRobjNum[index].Right.IndexOf('.') + 2, '$')); if (!SLRobjNum.Contains(newNode)) { SLRobjNum.Add(newNode); newItemset.Container.Add(SLRobjNum.Count - 1); newItemset.LookAhead.Add(symbol); } else { int index2 = SLRobjNum.IndexOf(newNode); newItemset.Container.Add(index2); newItemset.LookAhead.Add(symbol); } } } newItemset = Closure(newItemset); if (!proitemset.Contains(newItemset)) { proitemset.Add(newItemset); flag = true; } dfa[Pindex++] = new DFA(i, symbol, proitemset.IndexOf(newItemset), lookAhead); } } } } }

    public SLRitemsets Closure(SLRitemsets itemset)
    {
        for (int index = 0; index < itemset.Container.Count; index++)//遍历该集合中的所有序号 初始序号就是拓广文法的0 下面的ADD步骤中会扩大他的内容
            for (int i = 0; i < SLRobjNum[itemset.Container[index]].Right.Length - 1; i++)//遍历第index序号项目右侧字符串 找到.A结构
            {
                if (SLRobjNum[itemset.Container[index]].Right[i] == '.' && isFinalsymbol(SLRobjNum[itemset.Container[index]].Right[i + 1]))
                {
                    for (int j = 0; j < SLRobjNum.Count; j++)//遍历所有项目,找到以A开头的.a
                    {
                        if (isnexist(itemset.Container, j))
                            continue;
                        if (SLRobjNum[j].Left == SLRobjNum[itemset.Container[index]].Right[i + 1].ToString() && SLRobjNum[j].Right[0] == '.')
                        {
                            itemset.Container.Add(j);//ADD:在项目集(序号集合)中加入新成员
                            foreach (char c in follow[SLRobjNum[itemset.Container[index]].Right[i + 1]])
                            {
                                if (!itemset.LookAhead.Contains(c))
                                {
                                    itemset.LookAhead.Add(c);
                                }
                            }
                        }
                    }
                }
            }
        return itemset;
    }
    public void CreateSLRTable()
    {
        CreateItemsets();
        CreateDFA();
        SLRAna = new Table[proitemset.Count][];
        for (int i = 0; i < proitemset.Count; i++)
        {
            SLRAna[i] = new Table[Echar.Count + Nchar.Count];
            for (int j = 0; j < Echar.Count + Nchar.Count; j++)
            {
                char symbol = j < Echar.Count ? Echar[j] : Nchar[j - Echar.Count];
                int nextState = -1;
                HashSet<char> lookAhead = new HashSet<char>();
                foreach (DFA d in dfa)
                {
                    if (d.from == i && d.symbol == symbol)
                    {
                        nextState = d.to;
                        lookAhead.UnionWith(d.LookAhead);
                        break;
                    }
                }
                if (nextState == -1)
                {
                    SLRAna[i][j] = new Table();
                }
                else if (Gy_obj.Contains(nextState))
                {
                    SLRAna[i][j] = new Table('R', Gy_obj.IndexOf(nextState) + 1);
                }
                else if (proitemset[nextState].LookAhead.Contains('#'))
                {
                    SLRAna[i][j] = new Table('A', 0);
                }
                else
                {
                    SLRAna[i][j] = new Table('S', nextState);
                }
            }
            foreach (int index in Gy_itemset)
            {
                if (proitemset[index].Container.Contains(i))
                {
                    foreach (char c in proitemset[index].LookAhead)
                    {
                        if (Echar.IndexOf(c) >= 0)
                        {
                            SLRAna[i][Echar.IndexOf(c)] = new Table('R', Gy_obj.IndexOf(index) + 1);
                        }
                        else if (c == '#')
                        {
                            SLRAna[i][Nchar.Count - 1] = new Table('R', Gy_obj.IndexOf(index) + 1);
                        }
                    }
                }
            }
        }
        Success = true;
    }

    public void CreateFirst()
    {
        foreach (char c in Nchar)
        {
            first[c] = new HashSet<char>();
        }
        bool flag = true;
        while (flag)
        {
            flag = false;
            foreach (SLRNode node in SLRproNum)
            {
                if (node.Right.Length > 0)
                {
                    if (Echar.Contains(node.Right[0]))
                    {
                        if (first[node.Left].Add(node.Right[0]))
                        {
                            flag = true;
                        }
                    }
                    else
                    {
                        bool canEpsilon = true;
                        foreach (char c in node.Right)
                        {
                            if (Echar.Contains(c))
                            {
                                if (first[node.Left].Add(c))
                                {
                                    flag = true;
                                }
                                if (!first[c].Contains('ε'))
                                {
                                    canEpsilon = false;
                                    break;
                                }
                            }
                            else
                            {
                                if (first[node.Left].UnionWith(first[c]))
                                {
                                    flag = true;
                                }
                                if (!first[c].Contains('ε'))
                                {
                                    canEpsilon = false;
                                    break;
                                }
                            }
                        }
                        if (canEpsilon && first[node.Left].Add('ε'))
                        {
                            flag = true;
                        }
                    }
                }
            }
        }
    }
    public void CreateFollow()
    {
        foreach (char c in Nchar)
        {
            follow[c] = new HashSet<char>();
        }
        follow[SLRproNum[0].Left].Add('#');
        bool flag = true;
        while (flag)
        {
            flag = false;
            foreach (SLRNode node in SLRproNum)
            {
                for (int i = 0; i < node.Right.Length; i++)
                {
                    if (Nchar.Contains(node.Right[i]))
                    {
                        if (i == node.Right.Length - 1)
                        {
                            if (follow[node.Right[i]].UnionWith(follow[node.Left]))
                            {
                                flag = true;
                            }
                        }
                        else
                        {
                            bool canEpsilon = true;
                            for (int j = i + 1; j < node.Right.Length; j++)
                            {
                                if (Echar.Contains(node.Right[j]))
                                {
                                    if (follow[node.Right[i]].Add(node.Right[j]))
                                    {
                                        flag = true;
                                    }
                                    canEpsilon = false;
                                    break;
                                }
                                else
                                {
                                    if (follow[node.Right[i]].UnionWith(first[node.Right[j]]))
                                    {
                                        flag = true;
                                    }
                                    if (!first[node.Right[j]].Contains('ε'))
                                    {
                                        canEpsilon = false;
                                        break;
                                    }
                                }
                            }
                            if (canEpsilon)
                            {
                                if (follow[node.Right[i]].UnionWith(follow[node.Left]))
                                {
                                    flag = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    //判断是否是终结符
    public bool isFinalsymbol(char symbol)
    {
        return Echar.Contains(symbol);
    }
    //判断项目是否存在于项目集合中
    public bool isnexist(List<int> I, int i)
    {
        foreach (int index in I)
        {
            if (index == i)
            {
                return false;
            }
        }
        return true;
    }
SLR1 分析器构造:基于 LR0 分析器的改进

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

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