class Select
    {
        LL1Item LL1Item;
        First first;
        Follow follow;
        Dictionary<string, Dictionary<string, List<string>>> selects;

        public Select(LL1Item LL1Item, First first, Follow follow)
        {
            this.LL1Item = LL1Item;
            this.first = first;
            this.follow = follow;
            selects = new Dictionary<string, Dictionary<string, List<string>>>();
            GetSelect();
        }

        public void GetSelect()
        {
            var production=LL1Item.getproduction();
            foreach (var nofinal in LL1Item.nofinal)
            {
                selects.Add(nofinal,new Dictionary<string, List<string>>());
                //对非终结符的每个产生式获取select值
                foreach (var f in production[nofinal])
                {
                    selects[nofinal].Add(f,new List<string>());
                    for(int i=0; i < f.Length; i++)
                    {
                        //如果该产生式第一个字符为终结符
                        if (IsTerminal(f[i]))
                        {
                            if (f[i].Equals('#'))
                            {
                                foreach (var fol in follow.getfollows()[nofinal])
                                    selects[nofinal][f].Add(fol);
                            }
                            else if (!selects[nofinal][f].Contains(f[i].ToString()))
                                selects[nofinal][f].Add(f[i].ToString());
                            break;
                        }
                        //如果该产生式第一个字符为非终结符
                        else
                        {
                            int flag = 0;
                            foreach(var fir in first.getfirsts()[f[i].ToString()])
                            {
                                if (fir.Equals('#'))
                                    flag = 1;
                                else
                                {
                                    if (!selects[nofinal][f].Contains(fir))
                                        selects[nofinal][f].Add(fir);
                                }
                            }
                            if (flag == 0) break;
                        }
                        if (i == f.Length - 1)
                        {
                            foreach (var fol in follow.getfollows()[nofinal])
                                if (!selects[nofinal][f].Contains(fol))
                                    selects[nofinal][f].Add(fol);
                        }
                    }
                }
            }
        }
        static bool IsTerminal(char symbol)
        {
            return !char.IsUpper(symbol);
        }

        public Dictionary<string, Dictionary<string, List<string>>> getselects()
        {
            return selects;
        }
    }

private void button6_Click(object sender, EventArgs e)
{
    //构造预测分析表
    var selects = new Select(LL1Item, first, follow).getselects();
    var terminals = LL1Item.terminals;
    var nonterminals = LL1Item.nofinal;
    var productions = LL1Item.getproduction();
    var table = new Dictionary<string, Dictionary<string, string>>();
    foreach (var nonterminal in nonterminals)
    {
        table.Add(nonterminal, new Dictionary<string, string>());
        foreach (var terminal in terminals)
        {
            table[nonterminal].Add(terminal, '');
        }
    }
    foreach (var nonterminal in nonterminals)
    {
        foreach (var production in productions[nonterminal])
        {
            foreach (var terminal in selects[nonterminal][production])
            {
                table[nonterminal][terminal] = production;
            }
        }
    }
    //输出预测分析表
    listView3.Columns.Clear();
    listView3.Items.Clear();
    listView3.Columns.Add('', 50);
    foreach (var terminal in terminals)
    {
        listView3.Columns.Add(terminal, 50);
    }
    foreach (var nonterminal in nonterminals)
    {
        ListViewItem item = new ListViewItem(nonterminal);
        foreach (var terminal in terminals)
        {
            item.SubItems.Add(table[nonterminal][terminal]);
        }
        listView3.Items.Add(item);
    }
}
C#实现LL(1)文法预测分析表

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

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