当前位置:编程学习 > asp >>

一个用C#写的词法分析程序

源文件内容:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace wzy2
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;

  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;

  private System.Windows.Forms.RichTextBox richTextBox1;
  private System.Windows.Forms.RichTextBox richTextBox2;

  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.Windows.Forms.SaveFileDialog saveFileDialog1;
 
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.button2 = new System.Windows.Forms.Button();
   this.richTextBox2 = new System.Windows.Forms.RichTextBox();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
   this.button3 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(72, 24);
   this.label1.TabIndex = 0;
   this.label1.Text = "词法分析";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(240, 8);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(64, 23);
   this.button1.TabIndex = 1;
   this.button1.Text = "分析";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // richTextBox1
   //
   this.richTextBox1.Location = new System.Drawing.Point(24, 48);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(240, 352);
   this.richTextBox1.TabIndex = 2;
   this.richTextBox1.Text = "";
  // this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(96, 8);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(64, 23);
   this.button2.TabIndex = 3;
   this.button2.Text = "读入";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // richTextBox2
   //
   this.richTextBox2.Location = new System.Drawing.Point(280, 48);
   this.richTextBox2.Name = "richTextBox2";
   this.richTextBox2.Size = new System.Drawing.Size(280, 352);
   this.richTextBox2.TabIndex = 4;
   this.richTextBox2.Text = "";
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(384, 8);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(56, 23);
   this.button3.TabIndex = 5;
   this.button3.Text = "保存";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(584, 430);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.richTextBox2);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.richTextBox1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.label1);
   this.MaximizeBox = false;
   this.Name = "Form1";
   this.Text = "Form1";
  // this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
/// <summary>
/// 词法分析函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
  private void button1_Click(object sender, System.EventArgs e)
  {
   //得到想要的字符数组。
   char[] getch = textToCharArray();

   //将字符数组,转换为词法分析后的 单词数组。
   string[] stringArray = charArrayToStringArray(getch);

   //将单词数组分类,用数字标出各个单词所在的类别。
   string[,] twoStringArray = stringArrayToTwoStringArray(stringArray);

   //用于输出二维数组。
   printString(twoStringArray);

  }
/// <summary>
/// 输出结果 即用于输出二维数组。
/// </summary>
/// <param name="twoStringArray"></param>
  private void printString(string[,] twoStringArray)
  {
   //提示说明
   this.richTextBox2.Text ="1 -> 保留字"  + " " +
         "2 -> 运算符"  + " " +
         "3 -> 分隔符"  + " " +
         "4 -> 数字 "   + " " +
         "5 -> 其它"    + " ";
   //输出二维数组中的数据
   for(int x=0;x<twoStringArray.Length/2;x++)
   {
    for(int y=0;y<2;y++)
    {
     this.richTextBox2.Text = this.richTextBox2.Text + twoStringArray[y,x] + " ";
     if( y == 1)
     {
      this.richTextBox2.Text = this.ric

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,