Form1.cs
上传用户:ab16833
上传日期:2013-05-29
资源大小:10k
文件大小:9k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.IO;
- namespace Construe
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.TextBox txtSource;
- private System.Windows.Forms.TextBox txtTarget;
- char[] a = new char[1000]; //将字符串转换成字符数组
- string[] kw = new string[6]; //保留字字符表
- private System.Windows.Forms.Button btnSinConstrue;
- private System.Windows.Forms.Button btnConConstrue;
- private System.Windows.Forms.Button button1;
- /// <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.btnSinConstrue = new System.Windows.Forms.Button();
- this.txtSource = new System.Windows.Forms.TextBox();
- this.txtTarget = new System.Windows.Forms.TextBox();
- this.btnConConstrue = new System.Windows.Forms.Button();
- this.button1 = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // btnSinConstrue
- //
- this.btnSinConstrue.Location = new System.Drawing.Point(88, 256);
- this.btnSinConstrue.Name = "btnSinConstrue";
- this.btnSinConstrue.Size = new System.Drawing.Size(104, 32);
- this.btnSinConstrue.TabIndex = 1;
- this.btnSinConstrue.Text = "初步分析";
- this.btnSinConstrue.Click += new System.EventHandler(this.button1_Click);
- //
- // txtSource
- //
- this.txtSource.Location = new System.Drawing.Point(56, 56);
- this.txtSource.Multiline = true;
- this.txtSource.Name = "txtSource";
- this.txtSource.Size = new System.Drawing.Size(344, 184);
- this.txtSource.TabIndex = 3;
- this.txtSource.Text = "";
- //
- // txtTarget
- //
- this.txtTarget.Location = new System.Drawing.Point(64, 304);
- this.txtTarget.Multiline = true;
- this.txtTarget.Name = "txtTarget";
- this.txtTarget.Size = new System.Drawing.Size(344, 208);
- this.txtTarget.TabIndex = 4;
- this.txtTarget.Text = "";
- //
- // btnConConstrue
- //
- this.btnConConstrue.Location = new System.Drawing.Point(240, 256);
- this.btnConConstrue.Name = "btnConConstrue";
- this.btnConConstrue.Size = new System.Drawing.Size(104, 32);
- this.btnConConstrue.TabIndex = 5;
- this.btnConConstrue.Text = "高级分析";
- this.btnConConstrue.Click += new System.EventHandler(this.button2_Click);
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(192, 8);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(88, 32);
- this.button1.TabIndex = 6;
- this.button1.Text = "读取文件";
- this.button1.Click += new System.EventHandler(this.button1_Click_1);
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(496, 534);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.btnConConstrue);
- this.Controls.Add(this.txtTarget);
- this.Controls.Add(this.txtSource);
- this.Controls.Add(this.btnSinConstrue);
- this.Name = "Form1";
- this.Text = "Form1";
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void button1_Click(object sender, System.EventArgs e) //初级扫描时间
- {
- string strSource,strTarget;
- strSource = "";
- strSource = txtSource.Text;
- for (int i=0; i<strSource.Length; i++) //将字符串转换成字符数组
- a[i] = strSource[i];
- strTarget = "#";
- for (int i=0; i<a.Length; i++) //对源字符串逐个扫描
- {
- if (a[i] == '{') //去除{}的注释部分
- {
- i++;
- while (a[i] != '}')
- i++;
- }
- if (Convert.ToInt32(a[i])>='A' && Convert.ToInt32(a[i])<='Z') //读取保留字
- {
- strTarget += a[i];
- i++;
- while (Convert.ToInt32(a[i])>='A' && Convert.ToInt32(a[i])<='Z')
- {
- strTarget += a[i];
- i++;
- }
- i--;
- strTarget += '#';
- }
- if (Convert.ToInt32(a[i])>='a' && Convert.ToInt32(a[i])<='z') //读取标识符
- {
- strTarget += a[i];
- i++;
- while (Convert.ToInt32(a[i])>='a' && Convert.ToInt32(a[i])<='z')
- {
- strTarget += a[i];
- i++;
- }
- i--;
- strTarget += '#';
- }
- if (Convert.ToInt32(a[i])>='0' && Convert.ToInt32(a[i])<='9') //读取整数
- {
- strTarget += a[i];
- i++;
- while (Convert.ToInt32(a[i])>='0' && Convert.ToInt32(a[i])<='9')
- {
- strTarget += a[i];
- i++;
- }
- i--;
- strTarget += '#';
- }
- if (a[i]=='+' || a[i]=='-' || a[i]=='*' || a[i]==';' || a[i]=='.' || a[i]==','|| a[i]=='(' || a[i]==')') //读取专用符号
- {
- strTarget += a[i];
- strTarget += '#';
- }
- if (a[i]==':')
- {
- strTarget += a[i];
- i++;
- if (a[i]=='=')
- {
- strTarget += a[i];
- }
- i--;
- strTarget += '#';
- }
- }
- txtTarget.Text = strTarget;
- }
- private void button2_Click(object sender, System.EventArgs e) //高级扫描事件
- {
- string strSou,strTar;
- kw[0] = "PROGRAM"; //保留字字符表
- kw[1] = "VAR";
- kw[2] = "BEGIN";
- kw[3] = "DIV";
- kw[4] = "END";
- kw[5] = "integer";
- string buffer; //将“单词”保存在buffer
- strSou = txtSource.Text;
- for (int i=0; i<strSou.Length; i++) //将字符串转换成字符数组
- a[i] = strSou[i];
- strTar = "";
- for (int i=0; i<a.Length; i++) //对源字符串逐个扫描
- {
- if (a[i] == '{') //去除{}的注释部分
- {
- i++;
- while (a[i] != '}')
- i++;
- }
- else if (a[i] == ' ' || a[i]=='n' || a[i]=='r') //去除空格、回车、换行符号
- {
- }
- else if (Convert.ToInt32(a[i])>='A' && Convert.ToInt32(a[i])<='Z') //读取保留字
- {
- buffer = ""; //将整个保留字读取在buffer
- buffer += a[i];
- i++;
- while (Convert.ToInt32(a[i])>='A' && Convert.ToInt32(a[i])<='Z')
- {
- buffer += a[i];
- i++;
- }
- i--;
- for (int j=0; j<5; j++) //判断保留字是否正确
- {
- if (String.Equals(kw[j],buffer))
- {
- strTar += "(1,'" + buffer + "')";
- break;
- }
- else if (j==4) //若保留字错误,输出错误信息
- {
- strTar += "rn'" + buffer + "' is error string" + "rn";
- }
- }
-
- }
- else if (Convert.ToInt32(a[i])>='a' && Convert.ToInt32(a[i])<='z') //读取标识符
- {
- buffer = "";
- buffer += a[i];
- i++;
- while (Convert.ToInt32(a[i])>='a' && Convert.ToInt32(a[i])<='z')
- {
- buffer += a[i];
- i++;
- }
- i--;
- if (buffer == kw[5]) //判断是不是保留字
- {
- strTar += "(1,'" + buffer + "')";
- }
- else
- {
- strTar += "(3,'" + buffer + "')";
-
- }
- }
- else if (Convert.ToInt32(a[i])>='0' && Convert.ToInt32(a[i])<='9') //读取整数
- {
- buffer = "";
- buffer += a[i];
- i++;
- while (Convert.ToInt32(a[i])>='0' && Convert.ToInt32(a[i])<='9')
- {
- buffer += a[i];
- i++;
- }
- i--;
- strTar += "(4,'" + buffer + "')";
- }
- else if (a[i]=='+' || a[i]=='-' || a[i]=='*' || a[i]==';' || a[i]=='=' || a[i]=='.' || a[i]==','|| a[i]=='(' || a[i]==')') //读取专用符号
- {
- strTar += "(2,'" + a[i] + "')";
- }
- else if (a[i]==':')
- {
- buffer = "";
- buffer += a[i];
- i++;
- if (a[i]=='=')
- {
- buffer += a[i];
- }
- i--;
- strTar += "(2,'" + buffer + "')";
- }
- else //输出错误字符信息
- {
- strTar += "rn" + "'" + a[i] + "' is error string" + "rn";
- }
- }
- txtTarget.Text = strTar;
- }
- private void button1_Click_1(object sender, System.EventArgs e) //从文件中读取源程序
- {
- OpenFileDialog openFileDialog1 = new OpenFileDialog();;
- openFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
- openFileDialog1.RestoreDirectory = true ;
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
- txtSource.Text = sr.ReadToEnd(); //将文件内容读取到textbox
- sr.Close();
- }
- }
- }
- }