SLR1 分析表错误分析及修正

错误分析:

在 SLRAnaly() 函数中,对于归约状态的处理不正确。在添加归约状态时,应该遍历该项目集中的所有归约项目,并对它们的 Follow 集中的终结符都添加相应的归约状态。但是当前代码只遍历了一个归约项目,只为它的 Follow 集中的一个终结符添加了归约状态,导致分析表中缺少部分归约状态。

改正方法:

在 SLRAnaly() 函数中,对于归约状态的处理,应该遍历该项目集中的所有归约项目,并对它们的 Follow 集中的终结符都添加相应的归约状态。修改后的代码如下:

for (int i = 0; i < Gy_itemset.Count; i++)
{
    foreach (int index in proitemset[Gy_itemset[i]].Container)
    {
        SLRNode item = SLRobjNum[index];
        char left = item.Left[0];
        List<char> follow = GetFollow(left);
        foreach (char c in follow)
        {
            int CID = FindID(Echar, c);
            SLRAna[Gy_itemset[i]][CID] = new Table('r', Find_pro(item));
            if (c == '#')
            {
                int EID = FindID(Echar, '#');
                SLRAna[Gy_itemset[i]][EID] = new Table('r', Find_pro(item));
            }
        }
    }
}

需要调整的调用代码:

在 GET_ANA() 函数中,调用 SLRAnaly() 函数时不需要传入参数,因为 SLRAnaly() 函数中已经对分析表进行了赋值。修改后的代码如下:

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;

}

通过以上修正,SLR1 分析表构建的代码将能够正确地生成 SLR1 分析表,确保语法分析的正确性。

SLR1 分析表错误分析及修正

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

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