C#实现LL(1)预测分析表 - 编译原理
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace byyljxfzxt
{
public partial class Form4 : Form
{
// ... (其他代码)
private void button6_Click(object sender, EventArgs e)
{
// 构造预测分析表
Dictionary<string, Dictionary<string, string>> 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 item in production)
{
string A = item.Key;
foreach (var prod in item.Value)
{
// 计算 Select 集
var select = new Select(new LL1Item(item.Key, prod), new First(production), new Follow(production, firsts));
// 根据 Select 集填充预测分析表
foreach (var t in select.getselects()[A][prod])
{
if (table[A][t] != '')
{
MessageBox.Show('该文法不是LL(1)文法,存在冲突!\n');
return;
}
table[A][t] = prod;
}
}
}
// 输出预测分析表到 ListView
listView3.Clear();
foreach (var terminal in terminals)
{
listView3.Columns.Add(terminal, 100, HorizontalAlignment.Center);
}
foreach (var nonterminal in nonterminals)
{
ListViewItem lvi = new ListViewItem(nonterminal);
foreach (var terminal in terminals)
{
lvi.SubItems.Add(table[nonterminal][terminal]);
}
listView3.Items.Add(lvi);
}
}
// ... (其他代码)
}
}
原文地址: https://www.cveoy.top/t/topic/fXFM 著作权归作者所有。请勿转载和采集!