private void button6_Click(object sender, EventArgs e)
{
    // 获取所有终结符和非终结符
    terminals = new List<string>();
    nonterminals = new List<string>();
    foreach (var item in production)
    {
        if (!nonterminals.Contains(item.Key))
            nonterminals.Add(item.Key);
        foreach (var prod in item.Value)
        {
            foreach (var s in prod)
            {
                if (IsTerminal(s) && !terminals.Contains(s.ToString()))
                    terminals.Add(s.ToString());
                else if (!IsTerminal(s) && !nonterminals.Contains(s.ToString()))
                    nonterminals.Add(s.ToString());
            }
        }
    }
    terminals.Add("#");

    // 初始化预测分析表
    table = new Dictionary<string, Dictionary<string, string>>();
    foreach (var nonterm in nonterminals)
    {
        table.Add(nonterm, new Dictionary<string, string>());
        foreach (var term in terminals)
        {
            table[nonterm].Add(term, "");
        }
    }

    // 填充预测分析表
    GetSelect(production, firsts, follows);


    // 输出预测分析表到 listView3 中
    listView3.Columns.Clear();
    listView3.Items.Clear();
    listView3.View = View.Details;

    // 添加第一列
    listView3.Columns.Add("", 40);

    // 添加终结符列
    foreach (var item in terminals)
    {
        listView3.Columns.Add(item, 40);
    }


    // 填充表格
    foreach (var nonterm in nonterminals)
    {
        ListViewItem item = new ListViewItem(nonterm);
        foreach (var term in terminals)
        {
            string value = table[nonterm][term];
            if (!string.IsNullOrEmpty(value))
            {
                value = "->" + value.Replace("(", "").Replace(")", "").Replace("*", "").Replace("+", "").Replace("-", "").Replace("/", "");
            }
            item.SubItems.Add(value);
        }
        listView3.Items.Add(item);
    }
}

本代码修改了 button6_Click 函数,在填充预测分析表时,为非空元素添加了 "->" 前缀。这使得表格内容更加清晰易懂。代码使用 string.Replace 方法去除了一些特殊字符,以避免 "->" 与其他符号混淆。

C# 预测分析表填充优化:添加 " 前缀" title="C# 预测分析表填充优化:添加 "->" 前缀" loading="lazy">

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

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