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

        public string RStr = '';
        public string RStr_obitemset = '';//输出返回
        public string RStr_DFA = '';
        public string RStr_ANA = '';
            
            listView2.Clear();
            LR.Table[][] table;
            table = lr.GET_ANA();
            int xlen = table.GetLength(0);
            int ylen = table[1].Length;

            listView2.Columns.Clear();
            listView2.Items.Clear();
            listView2.View = View.Details;

            listView2.Columns.Add(' ');

            for (int i = 0; i < lr.Echar.Count; i++)//添加表头
            {
                string text = lr.Echar[i].ToString();
                listView2.Columns.Add(text,58);

            }
            for (int i = 0; i < lr.Nchar.Count; i++)//添加表头
            {
                string text = lr.Nchar[i].ToString();
                listView2.Columns.Add(text,58);
            }

            for (int i = 0; i < xlen; i++)
            {
                ListViewItem li = new ListViewItem(i.ToString());
                
                for (int j = 0; j < ylen; j++)
                {
                    string st = '';
                    if (table[i][j].error)
                        st = '-';
                    else if (table[i][j].type == 'A')
                        st = 'AC';
                    else
                        st = table[i][j].type.ToString() + table[i][j].id.ToString();
                    li.SubItems.Add(st);

                }
                listView2.Items.Add(li);
            }

            listView2.GridLines = true;
}
public Table[][] GET_ANA()
        {
            SLRAnaly();
            RStr_ANA += '\r\nSLR0分析表:\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()
        {
            Table tnode = new Table();

            SLRAna = new Table[proitemset.Count][];
            for (int i = 0; i < proitemset.Count; i++)
                SLRAna[i] = new Table[Echar.Count + Nchar.Count];

            for (int i = 0; i < proitemset.Count; i++)//初始化 赋予ERROR属性
                for (int j = 0; j < Echar.Count + Nchar.Count; j++)//为终结符加r状态 
                    SLRAna[i][j] = tnode;

            tnode = new Table('A', 0);
            SLRAna[1][FindID(Echar, '#')] = tnode;//项目集1必定是接受项目   构建[1][#]:acc的情况 先直接赋值好 dfa里没有

            for (int i = 0; i < Gy_itemset.Count; i++)
            {
                int proNum = Find_pro(SLRobjNum[proitemset[Gy_itemset[i]].Container[0]]);
                foreach (char c in Follow[Nchar[proNum]])//对于非终结符的follow集中的每个终结符
                {
                    int j = FindID(Echar, c);
                    if (j != -1)//如果该终结符在Echar中存在
                    {
                        tnode = new Table('r', proNum);//归约项目 找到原产生式序号 添加状态r
                        SLRAna[Gy_itemset[i]][j] = tnode;
                    }
                }
            }

            for (int i = 0; i < Pindex; i++)
            {
                if (isFinalsymbol(dfa[i].symbol))//symbol为非终结符  添加状态N
                {
                    int CID = FindID(Nchar, dfa[i].symbol);
                    foreach (char c in Follow[Nchar[CID]])//对于非终结符的follow集中的每个终结符
                    {
                        int j = FindID(Echar, c);
                        if (j != -1)//如果该终结符在Echar中存在
                        {
                            tnode = new Table('N', dfa[i].to);
                            SLRAna[dfa[i].from][j + Echar.Count] = tnode;
                        }
                    }
                }
                else //不是归约项目 添加状态S
                {
                    int CID = FindID(Echar, dfa[i].symbol);
                    tnode = new Table('S', dfa[i].to);
                    SLRAna[dfa[i].from][CID] = tnode;
                }
            }
        }
上述是SLR文法的分析表的构造,请给出相应follow集构造及相关调用函数代码。内容:为了构造SLR文法的分析表,需要先构造非终结符的Follow集。Follow集的构造规则如下:

1. 对于文法的开始符号S,将$加入到Follow(S)中。

2. 对于每个产生式A → αBβ,将Follow(A)中的所有符号加入到Follow(B)中。

3. 对于每个产生式A → αB,将Follow(A)中的所有符号加入到Follow(B)中。

4. 对于每个产生式A → αBβ,如果β可以推导出ε,则将Follow(A)中的所有符号加入到Follow(B)中。

5. 重复执行步骤2-4,直到Follow集不再改变。

下面是相应的代码实现:

//构造Follow集
public void FollowSet()
{
    Follow = new Dictionary<char, HashSet<char>>();
    foreach (char c in Nchar)
    {
        Follow[c] = new HashSet<char>();
    }
    Follow[start] = new HashSet<char>();
    Follow[start].Add('#');

    bool flag = true;
    while (flag)
    {
        flag = false;
        for (int i = 0; i < SLRproNum.Count; i++)
        {
            string right = SLRproNum[i].Right;
            int len = right.Length;
            for (int j = 0; j < len; j++)
            {
                char c = right[j];
                if (isNonterminal(c))
                {
                    HashSet<char> set = Follow[c];
                    HashSet<char> nextSet = new HashSet<char>();
                    if (j < len - 1)//B后面还有字符
                    {
                        for (int k = j + 1; k < len; k++)
                        {
                            if (isTerminal(right[k]))
                            {
                                nextSet.Add(right[k]);
                                break;
                            }
                            HashSet<char> tmpSet = First[right[k]];
                            nextSet.UnionWith(tmpSet);
                            if (!tmpSet.Contains('ε'))
                                break;
                        }
                    }
                    if (nextSet.Count > 0)
                    {
                        if (set.IsSupersetOf(nextSet))
                            continue;
                        set.UnionWith(nextSet);
                        flag = true;
                    }
                    if (j == len - 1 || First[right[j + 1]].Contains('ε'))
                    {
                        HashSet<char> tmpSet = Follow[SLRproNum[i].Left];
                        if (set.IsSubsetOf(tmpSet))
                            continue;
                        tmpSet.UnionWith(set);
                        flag = true;
                    }
                }
            }
        }
    }
}

其中,isNonterminal(c)函数用于判断字符c是否为非终结符;isTerminal(c)函数用于判断字符c是否为终结符。First集的构造在LR(1)文法的分析中已经给出。
SLR 文法分析表构造:SLRNode 类及分析表生成

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

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