代码实现了SLR分析表的构建,其中SLRAna是一个二维数组,用于存储分析表。SLRAnaly()函数中,首先初始化SLRAna,将所有元素赋值为error属性,然后将接受状态赋值为'A',将归约状态赋值为'r',将移进状态赋值为'S',最后根据DFA中的状态转移信息,将对应状态的SLRAna元素赋值为对应的移进或归约状态。

在SLRAnaly()函数中,存在一个问题:对于归约状态的处理,使用了SLRobjNum[proitemset[Gy_itemset[i]].Container[0]]来获取对应的归约项目,但是这里的Container[0]可能会导致获取到错误的项目。正确的做法应该是遍历proitemset[Gy_itemset[i]].Container,找到左部符号与项目点相同的产生式,然后将其作为归约项目。

另外,需要注意的是,在SLRAna数组中,r状态对应的元素应该是归约项目的序号,而不是对应的产生式序号。因此需要将Find_pro()函数修改为返回归约项目的序号。

以下是修改后的代码:

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 SLRproNum = new List(50);//产生式 列表 public List SLRobjNum = new List(50);//项目 列表 public List proitemset = new List(100);//项目集合 public List Gy_obj = new List(50);//归约项目序号集合 public List Gy_itemset = new List(50);//含有归约项目的集合的序号 的集合 public List Nchar = new List(50);//非终结符集合 public List Echar = new List(50);//终结符集合

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

public string RStr = ""; public string RStr_obitemset = "";//输出返回 public string RStr_DFA = ""; public string RStr_ANA = "";

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++)
{
    SLRNode item = null;
    foreach (int index in proitemset[Gy_itemset[i]].Container)
    {
        if (SLRobjNum[index].Dot == SLRobjNum[index].Right.Count)
        {
            item = SLRobjNum[index];
            break;
        }
    }
    if (item != null)
    {
        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', Gy_obj.IndexOf(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);
    }
}

}

private int Find_pro(SLRNode node) { for (int i = 0; i < SLRproNum.Count; i++) { if (SLRproNum[i].Left == node.Left && SLRproNum[i].Right.SequenceEqual(node.Right)) return i; } return -1;

public class Table public bool error;是否为ERROR public char type;结点类型 public int id;数值 public Table thiserror = true;

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

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