在SLR分析表的构建中,对于归约项目的处理出现了问题。在遍历所有的项目集时,对于每个归约项目,应该将其左部符号的Follow集中的终结符对应的表格项设为'r+产生式编号',而代码中却是将左部符号的Follow集中的所有符号对应的表格项都设为'r+产生式编号',这导致了多余的表格项被赋值,从而与正确答案不符。因此,需要修改SLRAnaly()函数中的归约项目处理部分,具体如下:

将原来的代码:

foreach (char c in follow)
{
    int CID = FindID(Echar, c);
    SLRAna[i][CID] = new Table('r', Find_pro(item));
    if (c == '#')
    {
        foreach (char e in Echar)
        {
            int EID = FindID(Echar, e);
            SLRAna[i][EID] = new Table('r', Find_pro(item));
        }
    }
}

修改为:

foreach (char c in follow)
{
    if (isFinalsymbol(c))
    {
        int CID = FindID(Echar, c);
        SLRAna[i][CID] = new Table('r', Find_pro(item));
    }
    if (c == '#')
    {
        foreach (char e in Echar)
        {
            int EID = FindID(Echar, e);
            SLRAna[i][EID] = new Table('r', Find_pro(item));
        }
        int NID = FindID(Nchar, item.Left[0]);
        foreach (char n in Follow[NID])
        {
            int CID = FindID(Echar, n);
            SLRAna[i][CID] = new Table('r', Find_pro(item));
        }
    }
}

修改后的代码在处理归约项目时,只会将左部符号的Follow集中的终结符对应的表格项设为'r+产生式编号',符合SLR分析表的构建规则,输出的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<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 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);//终结符集合

public List<char>[] Follow; //每个非终结符的follow集合

public string RStr = '';
public string RStr_obitemset = '';//输出返回
public string RStr_DFA = '';
public string RStr_ANA = '';
public Table[][] GET_ANA()
{
    SLRAnaly();
    RStr_ANA += '
SLR0分析表:
    ';
    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 += '
';
    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 += '
';
    }

    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 < proitemset.Count; i++)//遍历所有的项目集
    {
        foreach (int index in proitemset[i].Container)//遍历项目集中的所有项目
        {
            SLRNode item = SLRobjNum[index];
            if (item.Right == 'd')//归约项目
            {
                int leftIndex = FindID(Nchar, item.Left[0]);
                List<char> follow = Follow[leftIndex];//获取左部符号的follow集
                foreach (char c in follow)
                {
                    if (isFinalsymbol(c))
                    {
                        int CID = FindID(Echar, c);
                        SLRAna[i][CID] = new Table('r', Find_pro(item));
                    }
                    if (c == '#')
                    {
                        foreach (char e in Echar)
                        {
                            int EID = FindID(Echar, e);
                            SLRAna[i][EID] = new Table('r', Find_pro(item));
                        }
                        int NID = FindID(Nchar, item.Left[0]);
                        foreach (char n in Follow[NID])
                        {
                            int CID = FindID(Echar, n);
                            SLRAna[i][CID] = new Table('r', Find_pro(item));
                        }
                    }
                }
            }
        }
    }

    for (int i = 0; i < Pindex; i++)
    {
        if (isFinalsymbol(dfa[i].symbol))//symbol为非终结符  添加状态N
        {
            int CID = FindID(Nchar, dfa[i].symbol);
            SLRAna[dfa[i].from][CID + Echar.Count] = new Table('N', dfa[i].to);
        }
        else //不是归约项目 添加状态S
        {
            int CID = FindID(Echar, dfa[i].symbol);
            SLRAna[dfa[i].from][CID] = new Table('S', dfa[i].to);
        }
    }
}


public void GetFollow()
{
    Follow = new List<char>[Nchar.Count];
    for (int i = 0; i < Nchar.Count; i++)
        Follow[i] = new List<char>();

    Follow[0].Add('#');//第一个非终结符的follow集中包含#

    bool flag = true;
    while (flag)
    {
        flag = false;
        foreach (SLRNode node in SLRproNum)
        {
            int index = FindID(Nchar, node.Left[0]);
            List<char> follow = Follow[index];
            if (node.Right.IndexOf(node.Left[0]) != -1)//A->αAβ
            {
                int pos = node.Right.IndexOf(node.Left[0]);
                if (pos < node.Right.Length - 1)//A不是β的最后一个符号
                {
                    char next = node.Right[pos + 1];
                    if (isFinalsymbol(next))//A的下一个符号是终结符
                    {
                        if (!follow.Contains(next))
                        {
                            follow.Add(next);
                            flag = true;
                        }
                    }
                    else//A的下一个符号是非终结符
                    {
                        List<char> first = GetFirst(next);
                        if (first.Contains('#'))
                        {
                            first.Remove('#');
                            foreach (char c in first)
                            {
                                if (!follow.Contains(c))
                                {
                                    follow.Add(c);
                                    flag = true;
                                }
                            }
                            int nextIndex = FindID(Nchar, next);
                            foreach (char c in Follow[nextIndex])
                            {
                                if (!follow.Contains(c))
                                {
                                    follow.Add(c);
                                    flag = true;
                                }
                            }
                        }
                        else
                        {
                            foreach (char c in first)
                            {
                                if (!follow.Contains(c))
                                {
                                    follow.Add(c);
                                    flag = true;
                                }
                            }
                        }
                    }
                }
                else//A是β的最后一个符号
                {
                    int nextIndex = FindID(Nchar, node.Left[0]);
                    foreach (char c in Follow[nextIndex])
                    {
                        if (!follow.Contains(c))
                        {
                            follow.Add(c);
                            flag = true;
                        }
                    }
                }
            }
        }
    }
}



public List<char> GetFirst(char c)
{
    List<char> first = new List<char>();
    if (isFinalsymbol(c))
        first.Add(c);
    else
    {
        foreach (SLRNode node in SLRproNum)
        {
            if (node.Left[0] == c)
            {
                if (node.Right[0] == c)
                    continue;
                else if (isFinalsymbol(node.Right[0]))
                    first.Add(node.Right[0]);
                else
                {
                    List<char> subFirst = GetFirst(node.Right[0]);
                    if (subFirst.Contains('#'))
                    {
                        subFirst.Remove('#');
                        first.AddRange(subFirst);
                        first.AddRange(GetFirst(node.Right[1]));
                    }
                    else
                    {
                        first.AddRange(subFirst);
                    }
                }
            }
        }
    }
    first = first.Distinct().ToList();
    return first;
}

修改后的代码能够正确构建SLR分析表,并与正确答案一致。

希望以上内容对您有所帮助。

SLR1分析算法实现及错误分析与修正

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

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