📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
namespace 语法分析器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
///
public class Form1 : System.Windows.Forms.Form
{
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.textBox1 = new System.Windows.Forms.TextBox();
this.List = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label11 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label14 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.ForeColor = System.Drawing.Color.Maroon;
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(432, 72);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// List
//
this.List.ForeColor = System.Drawing.Color.Purple;
this.List.HorizontalScrollbar = true;
this.List.ItemHeight = 12;
this.List.Location = new System.Drawing.Point(448, 32);
this.List.Name = "List";
this.List.Size = new System.Drawing.Size(168, 220);
this.List.TabIndex = 1;
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(320, 32);
this.label1.TabIndex = 2;
this.label1.Text = "1、begin 2、const 3、call 4、do 5、end 6、if 7、var 8、procedure 9、read 10、then 11、while " +
"12、write";
//
// button1
//
this.button1.Location = new System.Drawing.Point(456, 264);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 3;
this.button1.Text = "语法分析";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
this.label2.Location = new System.Drawing.Point(8, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(152, 16);
this.label2.TabIndex = 4;
this.label2.Text = "请输入表达式:";
//
// label3
//
this.label3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
this.label3.Location = new System.Drawing.Point(448, 8);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(160, 16);
this.label3.TabIndex = 5;
this.label3.Text = "词法分析结果:";
//
// button2
//
this.button2.Location = new System.Drawing.Point(544, 264);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 6;
this.button2.Text = "清除输入";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label5
//
this.label5.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label5.ForeColor = System.Drawing.Color.Red;
this.label5.Location = new System.Drawing.Point(8, 24);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(264, 16);
this.label5.TabIndex = 8;
this.label5.Text = "本词法分析器可分析基本字为:";
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.ForeColor = System.Drawing.Color.Red;
this.label4.Location = new System.Drawing.Point(8, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(264, 16);
this.label4.TabIndex = 9;
this.label4.Text = "本词法分析器可分析运算符为:";
//
// label6
//
this.label6.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label6.ForeColor = System.Drawing.Color.Red;
this.label6.Location = new System.Drawing.Point(24, 88);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(288, 16);
this.label6.TabIndex = 10;
this.label6.Text = "1 + 2 - 3 * 4 / 5 = 6 # 7 < 8 >";
//
// label7
//
this.label7.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label7.ForeColor = System.Drawing.Color.Red;
this.label7.Location = new System.Drawing.Point(8, 104);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(264, 16);
this.label7.TabIndex = 11;
this.label7.Text = "本词法分析器可分析分界副为:";
//
// label8
//
this.label8.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label8.ForeColor = System.Drawing.Color.Red;
this.label8.Location = new System.Drawing.Point(24, 120);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(288, 16);
this.label8.TabIndex = 12;
this.label8.Text = "1 ( 2 ) 3 , 4 ; 5 . 6 [ 7 ] 8 :";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label11);
this.groupBox1.Controls.Add(this.label10);
this.groupBox1.Controls.Add(this.label9);
this.groupBox1.Controls.Add(this.label5);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.label7);
this.groupBox1.Controls.Add(this.label8);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
this.groupBox1.Location = new System.Drawing.Point(8, 224);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(432, 144);
this.groupBox1.TabIndex = 13;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "使用说明";
//
// label11
//
this.label11.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label11.ForeColor = System.Drawing.Color.Blue;
this.label11.Location = new System.Drawing.Point(328, 120);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(96, 16);
this.label11.TabIndex = 15;
this.label11.Text = "(种类号,序号)";
//
// label10
//
this.label10.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label10.ForeColor = System.Drawing.Color.Green;
this.label10.Location = new System.Drawing.Point(352, 16);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(72, 16);
this.label10.TabIndex = 14;
this.label10.Text = "单词种类:";
//
// label9
//
this.label9.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label9.ForeColor = System.Drawing.Color.Green;
this.label9.Location = new System.Drawing.Point(352, 32);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(72, 80);
this.label9.TabIndex = 13;
this.label9.Text = "1是基本字 2是运算符 3是标识符 4是常数 5是界符 6是无用字符";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.textBox3);
this.groupBox2.Controls.Add(this.textBox2);
this.groupBox2.Controls.Add(this.label14);
this.groupBox2.Controls.Add(this.label13);
this.groupBox2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
this.groupBox2.Location = new System.Drawing.Point(8, 112);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(432, 104);
this.groupBox2.TabIndex = 15;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "用户自定义数据";
//
// textBox3
//
this.textBox3.BackColor = System.Drawing.SystemColors.HighlightText;
this.textBox3.ForeColor = System.Drawing.Color.Purple;
this.textBox3.Location = new System.Drawing.Point(56, 56);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.ReadOnly = true;
this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox3.Size = new System.Drawing.Size(368, 40);
this.textBox3.TabIndex = 3;
this.textBox3.Text = "";
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.Color.White;
this.textBox2.ForeColor = System.Drawing.Color.Purple;
this.textBox2.Location = new System.Drawing.Point(56, 16);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(368, 40);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// label14
//
this.label14.Location = new System.Drawing.Point(8, 56);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(48, 16);
this.label14.TabIndex = 1;
this.label14.Text = "常 数:";
//
// label13
//
this.label13.Location = new System.Drawing.Point(8, 24);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(56, 16);
this.label13.TabIndex = 0;
this.label13.Text = "标示符:";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(456, 304);
this.textBox4.Multiline = true;
this.textBox4.Name = "textBox4";
this.textBox4.ReadOnly = true;
this.textBox4.Size = new System.Drawing.Size(152, 56);
this.textBox4.TabIndex = 16;
this.textBox4.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(624, 374);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.List);
this.Controls.Add(this.textBox1);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(720, 592);
this.MinimumSize = new System.Drawing.Size(200, 200);
this.Name = "Form1";
this.Text = "PL/0表达式分析 C#实现(02122984 郑刚)";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
public class KeyAnalyze
{
private string [] keytxt =new string[]{ "begin","const","call","do","end","if","var",
"procedure","read","then","while","write"};
//基本字
private string [] optxt=new string []{"+","-","*","/","=","#","<",">"};
//运算符
private string [] eoptxt=new string []{"(",")",",",";",".","[","]",":"};
//界符
private string keybuffer;
//字符缓冲区
public ArrayList ResultList=new ArrayList(); //结果存放区
public ArrayList NumList=new ArrayList(); //常数存放区
public string ResultNum;//所有常数输出存放区
public ArrayList IdList=new ArrayList(); //标识符存放区
public string ResultId;//所有标示符输出存放区
public bool IsKey(string a)
{
foreach(string b in keytxt)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -