using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LL1
{
    public partial class Form1 : Form
    {
        // ...其他代码...

        private void button6_Click(object sender, EventArgs e)
        {
            // 构造预测分析表
            First first = new First(LL1Item);
            Dictionary<string, List<string>> firsts = GetFirst(first);

            Follow follow = new Follow(LL1Item, first);
            Dictionary<string, List<string>> follows = GetFollow(follow);

            Select select = new Select(LL1Item, first, follow);
            Dictionary<string, Dictionary<string, List<string>>> selects = GetSelect(select);

            List<string> terminals = LL1Item.terminals;
            List<string> nofinals = LL1Item.nofinal;
            Dictionary<string, Dictionary<string, string>> analysisTable = new Dictionary<string, Dictionary<string, string>>();

            foreach (string nofinal in nofinals)
            {
                analysisTable.Add(nofinal, new Dictionary<string, string>());
                foreach (string terminal in terminals)
                {
                    analysisTable[nofinal].Add(terminal, '');
                }
            }

            foreach (string nofinal in nofinals)
            {
                foreach (KeyValuePair<string, List<string>> entry in selects[nofinal])
                {
                    List<string> productions = entry.Value;
                    foreach (string terminal in productions)
                    {
                        analysisTable[nofinal][terminal] = entry.Key;
                    }
                }
            }

            // 输出预测分析表
            listView3.Clear();
            listView3.View = View.Details;
            listView3.Columns.Add('');

            foreach (string terminal in terminals)
            {
                listView3.Columns.Add(terminal);
            }

            foreach (string nofinal in nofinals)
            {
                ListViewItem item = new ListViewItem(nofinal);
                for (int i = 0; i < terminals.Count; i++)
                {
                    item.SubItems.Add(analysisTable[nofinal][terminals[i]]);
                }
                listView3.Items.Add(item);
            }
        }

        // 获取First集
        private Dictionary<string, List<string>> GetFirst(First first)
        {
            return first.getfirsts();
        }

        // 获取Follow集
        private Dictionary<string, List<string>> GetFollow(Follow follow)
        {
            return follow.getfollows();
        }

        // 获取Select集
        private Dictionary<string, Dictionary<string, List<string>>> GetSelect(Select select)
        {
            return select.getselects();
        }

        // ...其他代码...
    }

    // ...其他类定义...
}

代码说明:

  1. GetFirst, GetFollow, GetSelect 函数:
    • 这些函数分别用于获取First集、Follow集和Select集。
    • 它们调用了对应的类实例 (first, follow, select) 的方法来获取相应集合。
  2. 构造预测分析表:
    • 创建了一个名为 analysisTable 的字典,用于存储预测分析表。
    • 使用嵌套循环遍历非终结符和终结符,并根据 selects 字典中的信息填充 analysisTable
  3. 输出预测分析表:
    • 清空 listView3 并设置其视图模式。
    • 添加列标题,包括一个空列和所有终结符。
    • 使用嵌套循环遍历非终结符和终结符,并将 analysisTable 中对应的内容添加到 listView3 中。

改进说明:

  • 添加了 GetFirst, GetFollow, GetSelect 函数,使代码更易读和维护。
  • 使用更具描述性的变量名,例如 analysisTable, terminals, nofinals
  • 添加了代码注释,解释代码的功能和实现方式。

这段代码演示了如何使用 C# 构建 LL(1) 文法的预测分析表。您可以根据自己的需求修改和扩展此代码。

C#实现LL(1)文法预测分析表构建

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

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