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 button7_Click(object sender, EventArgs e)
        {
            // 判断输入是否为空
            if (string.IsNullOrWhiteSpace(textBox1.Text))
            {
                MessageBox.Show('输入为空,分析失败');
                return;
            }

            // 初始化分析器
            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]);
            string inputStr = textBox1.Text + '#';
            for (int i = inputStr.Length - 1; i >= 0; i--)
                input.Push(inputStr[i]);

            // ... (显示分析表代码)

            // 显示分析栈和输入串
            result_analys.Add(new string(analyse.ToArray()));
            result_input.Add(new string(input.ToArray()));
            result_parse.Add('');
            DisplayListView(listView4, result_analys, result_input, result_parse);

            button8.Enabled = true;
            button9.Enabled = true;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (analyse.Count == 0 || input.Count == 0)
            {
                MessageBox.Show('分析结束');
                button8.Enabled = false;
                button9.Enabled = false;
                return;
            }

            char top = analyse.Pop();
            char next = input.Pop();
            string parse = '';

            if (top == next)
            {
                parse = '匹配 ' + top;
            }
            else if (char.IsUpper(top))
            {
                string prod = table[top.ToString()][next.ToString()];
                if (prod != null)
                {
                    parse = '使用 ' + prod;
                    for (int i = prod.Length - 1; i >= 0; i--)
                        analyse.Push(prod[i]);
                }
                else
                {
                    MessageBox.Show('分析失败');
                    button8.Enabled = false;
                    button9.Enabled = false;
                    return;
                }
            }
            else
            {
                MessageBox.Show('分析失败');
                button8.Enabled = false;
                button9.Enabled = false;
                return;
            }

            result_analys.Add(new string(analyse.ToArray()));
            result_input.Add(new string(input.ToArray()));
            result_parse.Add(parse);
            DisplayListView(listView4, result_analys, result_input, result_parse);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            while (analyse.Count > 0 && input.Count > 0)
            {
                // ... (与 button8_Click 中相同的逻辑)
            }

            // ... (显示结果和提示)
        }

        // ... (其他代码)
    }
}
C#实现LL(1)文法分析器:判断句子合法性及步骤分析

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

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