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)
        {
            // 判断是否存在LL(1)文法
            if (!JudgeLL1(production, firsts, follows))
            {
                MessageBox.Show('该文法不是LL(1)文法,无法进行分析!');
                return;
            }

            // 获取分析串和产生式
            analyse_sentence analyse = new analyse_sentence(textBox1.Text, new Select(production, firsts, follows));
            List<string> result_analys = analyse.result_analys;
            List<string> result_input = analyse.result_input;
            List<string> result_parse = analyse.result_parse;

            // 将结果添加到listView4中
            listView4.Items.Clear();
            for (int i = 0; i < result_analys.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = (i+1).ToString();
                lvi.SubItems.Add(result_analys[i]);
                lvi.SubItems.Add(result_input[i]);
                lvi.SubItems.Add(result_parse[i]);
                listView4.Items.Add(lvi);
            }
        }

        // ... 其他代码 ...
    }
}

代码说明:

  1. button7_Click 事件处理函数:
    • 首先判断当前文法是否为 LL(1) 文法,如果不是,则提示用户并终止分析。
    • 创建 analyse_sentence 对象,传入输入句子和 Select 对象(包含文法规则、First 集、Follow 集等信息)。
    • analyse_sentence 对象中获取分析结果,包括分析串、剩余输入串和推导所用产生式。
    • 清空 listView4 控件,并将分析结果逐行添加到 listView4 中展示给用户。

使用方法:

  1. 将上述代码添加到 Form4 的代码文件中。
  2. 确保 JudgeLL1analyse_sentenceSelect 等类和方法已正确实现。
  3. 运行程序,在 textBox1 中输入待分析的句子,点击 button7 按钮即可在 listView4 中查看分析结果。

注意:

  • analyse_sentence 类需要根据实际的 LL(1) 文法分析算法进行实现。
  • Select 类需要根据 LL(1) 文法分析表的构建方法进行实现。
  • listView4 控件需要设置好相应的列,用于显示步骤、分析串、剩余输入串和推导所用产生式。

通过以上步骤,就可以实现一个简单的 C# LL(1) 文法分析器,并能够对输入句子进行分析,并将分析结果展示给用户。

C#实现LL(1)文法分析器:句子分析功能详解

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

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