C#语法分析器实现:详细代码与步骤解析
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace 语法分析器
{
public partial class Form1 : Form
{
// ... 其他代码 ...
private void button7_Click(object sender, EventArgs e)
{
// 判断输入是否为空
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show('输入为空,分析失败');
return;
}
// 初始化分析器
string text = textBox1.Text;
analyse = new Stack<char>();
input = new Stack<char>();
result_analys = new List<string>();
result_input = new List<string>();
result_parse = new List<string>();
analyse.Push('#');
analyse.Push(production.Keys.First()[0]);
input.Push('#');
for (int i = text.Length - 1; i >= 0; i--)
{
input.Push(text[i]);
}
// 进行语法分析
while (true)
{
// 如果分析栈栈顶元素为终结符
if (!char.IsUpper(analyse.Peek()))
{
// 当两个栈都只剩下#时,说明匹配成功
if (analyse.Peek() == input.Peek() && analyse.Count == 1 && input.Count == 1)
{
result_parse.Add('成功');
break;
}
if (analyse.Peek() == input.Peek())
{
result_parse.Add(''' + analyse.Peek() + ''匹配');
analyse.Pop();
input.Pop();
result_analys.Add(new string(analyse.Reverse().ToArray()));
result_input.Add(new string(input.Reverse().ToArray()));
continue;
}
else
{
result_parse.Add('失败');
break;
}
}
// 如果分析栈栈顶元素为非终结符
string nonterminal = analyse.Peek().ToString();
string terminal = input.Peek().ToString();
if (!table.ContainsKey(nonterminal) || !table[nonterminal].ContainsKey(terminal))
{
result_parse.Add('失败');
break;
}
string production = table[nonterminal][terminal];
result_parse.Add(nonterminal + '->' + production);
analyse.Pop();
if (production != '#')
{
for (int i = production.Length - 1; i >= 0; i--)
{
analyse.Push(production[i]);
}
}
result_analys.Add(new string(analyse.Reverse().ToArray()));
result_input.Add(new string(input.Reverse().ToArray()));
}
listView4.Columns.Clear();
listView4.Items.Clear();
listView4.View = View.Details;
listView4.Columns.Add('步骤', 60, HorizontalAlignment.Center);
listView4.Columns.Add('分析串', 60, HorizontalAlignment.Center);
listView4.Columns.Add('剩余输入串', 80, HorizontalAlignment.Center);
listView4.Columns.Add('推导所用产生式或匹配', 150, HorizontalAlignment.Center);
step = 0;
button8.Enabled = true;
button9.Enabled = true;
}
private void button8_Click(object sender, EventArgs e)
{
// 在界面第二行之后展示步骤、分析串、剩余输入串、推导所用产生式或匹配
ListViewItem item = new ListViewItem((step + 1).ToString());
item.SubItems.Add(result_analys[step]);
item.SubItems.Add(result_input[step]);
item.SubItems.Add(result_parse[step]);
listView4.Items.Add(item);
// 将步骤加一
step++;
}
private void button9_Click(object sender, EventArgs e)
{
// 自动分析直到分析结束
while (step < result_parse.Count)
{
// 在界面第二行之后展示步骤、分析串、剩余输入串、推导所用产生式或匹配
ListViewItem item = new ListViewItem((step + 1).ToString());
item.SubItems.Add(result_analys[step]);
item.SubItems.Add(result_input[step]);
item.SubItems.Add(result_parse[step]);
listView4.Items.Add(item);
// 将步骤加一
step++;
// 检查是否到达分析结果的末尾
if (step >= result_parse.Count)
{
break;
}
}
button8.Enabled = false;
button9.Enabled = false;
}
// ... 其他代码 ...
}
}
原文地址: https://www.cveoy.top/t/topic/fXPj 著作权归作者所有。请勿转载和采集!