LL(1)文法判定工具 - 在线检测你的文法是否符合LL(1)条件
public partial class Form4 : Form { LL1Item LL1Item; First first; Follow follow;
public Form4()
{
InitializeComponent();
}
//--------------------预处理
private void button3_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
DialogResult dr = openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;//获取或设置一个包含在文件对话框中选定的文件名字符串
if (dr == DialogResult.OK && !string.IsNullOrEmpty(filename))
{
StreamReader sr = new StreamReader(filename);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}
}
private void Form4_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
string text = richTextBox1.Text;
LL1Item = new LL1Item(text);
if (LL1Item.creatproduction() == 1)
{
first = new First(LL1Item);
follow = new Follow(LL1Item, first);
if (LL1Item.getisreach()["S"])
{
MessageBox.Show("该文法不是LL(1)文法,存在左递归的情况!\n");
}
else
{
bool flag = true;
var selects = new Select(LL1Item, first, follow).getselects();
foreach (var item in selects)
{
var value = item.Value;
for (int i = 0; i < value.Count - 1; i++)
for (int j = i + 1; j < value.Count; j++)
if (value[i] == value[j])
{
flag = false;
break;
}
if (!flag) break;
}
if (flag)
{
MessageBox.Show("该文法是LL(1)文法\n");
foreach (var item in first.getfirsts())
{
richTextBox2.AppendText(item.Key + ": { ");
foreach (var s in item.Value)
richTextBox2.AppendText(s + " ");
richTextBox2.AppendText("}\n");
}
richTextBox2.AppendText("FOLLOW集:\n");
foreach (var item in follow.getfollows())
{
richTextBox2.AppendText(item.Key + ": { ");
foreach (var s in item.Value)
richTextBox2.AppendText(s + " ");
richTextBox2.AppendText("}\n");
}
}
else
{
MessageBox.Show("该文法不是LL(1)文法,存在FIRST集合有交集的情况!\n");
}
}
}
else
{
MessageBox.Show("该文法不是LL(1)文法,产生式有误!\n");
}
}
private void button5_Click(object sender, EventArgs e)
{
SaveFileDialog
saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog.FileName = "Grammer.txt"; // 设置默认文件名
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filename = saveFileDialog.FileName;
File.WriteAllText(filename, richTextBox1.Text);
MessageBox.Show("文件已成功保存至:" + filename);
}
}
}
原文地址: https://www.cveoy.top/t/topic/oxGv 著作权归作者所有。请勿转载和采集!