class SLR { //分析句子 public class Analyze { public List stack_state = new List(100);//记录状态栈 public List stack_symbol = new List(100);//记录符号栈 public List Input_str = new List(100);//记录输入串 public List Tran_pro = new List(100);//记录所用产生式 }

public DFA[] dfa = new DFA[100];
public int Pindex = 0; //dfa数组指针
public Table[][] LRAna;//分析表
public Analyze Jz;
public bool Success = false;
public List<LRNode> LRproNum = new List<LRNode>(50);//产生式 列表
public List<LRNode> LRobjNum = new List<LRNode>(50);//项目 列表
public List<LRitemsets> proitemset = new List<LRitemsets>(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 string RStr = "";
public string RStr_obitemset = "";//输出返回
public string RStr_DFA = "";
public string RStr_ANA = "";

public Table[][] GET_ANA()
{
    SLRAnaly();
    RStr_ANA += "\r\nSLR分析表:\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 (LRAna[i][j].error)
            {
                RStr_ANA += "  " + "    ";
            }
            else if (i == 1 && j == Echar.Count - 1)
            {
                RStr_ANA += "AC" + "    ";
            }
            else if (LRAna[i][j].type != 'N')
            {
                RStr_ANA += LRAna[i][j].type.ToString() + LRAna[i][j].id.ToString() + "    ";
            }
            else
                RStr_ANA += LRAna[i][j].id.ToString() + "    ";
        }
        RStr_ANA += "\r\n";
    }

    return LRAna;

}

//求项目集
public void Creteitemsets()
{
    List<int> lr_item = new List<int>(100);//记录项目的序号
    lr_item.Add(0);
    lr_item = Closure(lr_item);//构造初始项目集 求闭包

    LRitemsets LR_C = new LRitemsets();
    LR_C.Container = lr_item;//集合----项目集序号的集合
    proitemset.Add(LR_C);//集合的集合----存放项目集序号集合 的集合


    for (int i = 0; i < proitemset.Count; i++)//整体集合中 第i个项目集
    {
        proitemset[i].Container.Sort();//排序由小到大 后面用于判断是否存在的比较
        int[] flag = new int[proitemset[i].Container.Count];
        for (int fi = 0; fi < proitemset[i].Container.Count; fi++)//标志位,用来判断该序号是否已经构造
        {
            flag[fi] = 0;
        }

        for (int j = 0; j < proitemset[i].Container.Count; j++)//第i个项目集的第j个项目
        {
            if (flag[j] == 1)//如果已经访问过 就不再构造 找下一个项目
                continue;
            int index = proitemset[i].Container[j];
            for (int pi = 0; pi < LRobjNum[index].Right.Length - 1; pi++)//length-1是避免匹配到.在最后的规约项目
            {
                if (LRobjNum[index].Right[pi] == '.')
                {

                    List<int> lr2_club = new List<int>(100);//记录项目的序号
                    char symbol = LRobjNum[index].Right[pi + 1];//记录.a转移状态a.的符号a
                    lr2_club.Add((index + 1));//如果遇到.a形式的项目序号为index 那么项目a.的序号为index+1
                    for (int m1 = j + 1; m1 < proitemset[i].Container.Count; m1++)
                    {//在第i个项目集中找到了可以移动的.:.a  重新遍历第i个项目集j项目之后的 找到同样可以移动a的项目集
                        int index2 = proitemset[i].Container[m1];
                        for (int m2 = 0; m2 < LRobjNum[index2].Right.Length - 1; m2++)
                        {
                            if (LRobjNum[index2].Right[m2] == '.' && LRobjNum[index2].Right[m2 + 1] == symbol)
                            {
                                flag[m1] = 1;//标记位置为1 已经访问 之后不再访问
                                lr2_club.Add(index2 + 1);
                            }
                        }
                    }
                    lr2_club = Closure(lr2_club);//求闭包
                    int value = isexist(lr2_club);
                    if (value == -1)//-1表示不存在相同的
                    {
                        for (int m3 = 0; m3 < Gy_obj.Count; m3++)
                        {
                            if (isnexist(lr2_club, Gy_obj[m3]))
                            {
                                Gy_itemset.Add(proitemset.Count);
                            }
                        }
                        LRitemsets LR_C2 = new LRitemsets();
                        dfa[Pindex++] = new DFA(i, symbol, proitemset.Count);//count不用加1  本身从0开始
                        LR_C2.Container = lr2_club;
                        proitemset.Add(LR_C2);
                    }
                    else
                    {
                        dfa[Pindex++] = new DFA(i, symbol, value);
                    }
                    break;
                }
            }
        }//end-forj
    }//end-fori

}//end-Cre_club

//分析表
public void SLRAnaly()
{
    Table tnode = new Table();

    LRAna = new Table[proitemset.Count][];
    for (int i = 0; i < proitemset.Count; i++)
        LRAna[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状态 
            LRAna[i][j] = tnode;

    tnode = new Table('A', 0);
    LRAna[1][FindID(Echar, '#')] = tnode;//项目集1必定是接受项目   构建[1][#]:acc的情况 先直接赋值好 dfa里没有

    for (int i = 0; i < Gy_itemset.Count; i++)
    {
        tnode = new Table('r', Find_pro(LRobjNum[proitemset[Gy_itemset[i]].Container[0]]));//归约项目 找到原产生式序号 添加状态r
        for (int j = 0; j < Echar.Count; j++)
        {
            LRAna[Gy_itemset[i]][j] = tnode;
        }
    }
    for (int i = 0; i < Pindex; i++)
    {

        if (isFinalsymbol(dfa[i].symbol))//symbol为非终结符  添加状态N
        {
            int CID = FindID(Nchar, dfa[i].symbol);
            tnode = new Table('N', dfa[i].to);
            LRAna[dfa[i].from][CID
class LR 分析句子 public class Analyze public Liststring stack_state = new Liststring100;记录状态栈 public Liststring stack_symbol = new Liststring100;记录符号栈

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

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