SLR1分析表构建的步骤:

  1. 构建LR(0)自动机;
  2. 对于每个项目集,求出它的闭包;
  3. 对于每个项目集中的每个项目,找到它后面的那个符号;
  4. 如果后面的符号是终结符,则在自动机中添加一个移进操作;
  5. 如果后面的符号是非终结符,则在自动机中添加一个goto操作;
  6. 对于每个归约项目,找到它所在的项目集,以及它所能接受的符号的follow集合;
  7. 在自动机中添加一个归约操作。

对于上述代码,分析表构建的步骤是正确的。但是代码中存在一些错误:

  1. 在SLRAnaly()函数中,对于每个归约项目,应该找到它所在的项目集,而不是它所在的项目。
  2. 在SLRAnaly()函数中,对于每个归约项目,应该找到它所能接受的符号的follow集合,而不是它所在的非终结符的follow集合。
  3. 在GetFirst()函数中,当一个符号的产生式的第一个符号是它本身时,应该跳过这个产生式,否则会出现死循环。

修改后的代码见下:

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 = SLRobjNum[proitemset[Gy_itemset[i]].Container[0]];
    char left = item.Left[0];
    List<char> follow = GetFollow(item);
    foreach (char c in follow)
    {
        int CID = FindID(Echar, c);
        SLRAna[Gy_itemset[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 List GetFollow(SLRNode node) { List follow = new List(); if (node.Left[0] == 'E') follow.Add('#'); foreach (SLRNode n in SLRproNum) { int index = n.Right.IndexOf(node.Left[0]); if (index != -1 && index < n.Right.Length - 1) { char next = n.Right[index + 1]; if (isFinalsymbol(next)) follow.Add(next); else { List first = GetFirst(next); if (first.Contains('#')) { first.Remove('#'); follow.AddRange(first); follow.AddRange(GetFollow(n)); } else { follow.AddRange(first); } } } else if (index != -1 && index == n.Right.Length - 1 && !n.Left.Equals(node.Left)) { follow.AddRange(GetFollow(n)); } } follow = follow.Distinct().ToList(); return follow; }

public List GetFirst(char c) { List first = new List(); 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 subFirst = GetFirst(node.Right[0]); if (subFirst.Contains('#')) { subFirst.Remove('#'); first.AddRange(subFirst); if (node.Right.Length > 1) first.AddRange(GetFirst(node.Right[1])); else first.AddRange(GetFollow(node)); } else { first.AddRange(subFirst); } } } } first = first.Distinct().ToList(); return first;

请给出SLR1分析表构建的步骤依据上述步骤分析下列代码的正误并给出结论、指出错误部分和修改后的代码。求项目集 public void Creteitemsets Listint lr_item = new Listint100;记录项目的序号 lr_itemAdd0; lr_item = Closure

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

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