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<char> GetFollow(SLRNode node)
{
    List<char> follow = new List<char>();
    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<char> 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<char> GetFirst(char c)
{
    List<char> first = new List<char>();
    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);
                    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 分析表。


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

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