源文件内容:
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.richTextBox2.Text + "
";
源代码网推荐 }
源代码网推荐 }
源代码网推荐 }
源代码网推荐 }
/// <summary>
源代码网推荐/// 打开方件
源代码网推荐/// </summary>
源代码网推荐/// <param name="sender"></param>
源代码网推荐/// <param name="e"></param>
源代码网推荐 private void button2_Click(object sender, System.EventArgs e)// 文件打开的方法。
源代码网推荐 {
源代码网推荐 openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
源代码网推荐 openFileDialog1.Title = "打开要分析的源文件。";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
源代码网推荐 {
源代码网推荐 System.IO.StreamReader sr = new
源代码网推荐 System.IO.StreamReader(openFileDialog1.FileName);
源代码网推荐 this.richTextBox1.Text = sr.ReadToEnd();
源代码网推荐 sr.Close();
源代码网推荐 }
源代码网推荐 }
源代码网推荐/// <summary>
源代码网推荐/// 保存文件
源代码网推荐/// </summary>
源代码网推荐/// <param name="sender"></param>
源代码网推荐/// <param name="e"></param>
源代码网推荐 private void button3_Click(object sender, System.EventArgs e)
源代码网推荐 {
源代码网推荐 saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
源代码网推荐 saveFileDialog1.Title = "保存分析结果.";
源代码网推荐 if(saveFileDialog1.ShowDialog() == DialogResult.OK)
源代码网推荐 {
源代码网推荐 System.IO.StreamWriter wr = new
源代码网推荐 System.IO.StreamWriter(saveFileDialog1.FileName);
源代码网推荐 wr.Write(this.richTextBox2.Text);
源代码网推荐 wr.Close();
源代码网推荐 }
源代码网推荐 }
/// <summary>
源代码网推荐/// 引用二维数组和单词的标志j
源代码网推荐/// </summary>
源代码网推荐/// <param name="twoArray"></param>
源代码网推荐/// <param name="j"></param>
源代码网推荐 private void oneArrayToTwo(ref string[,] twoArray,ref int j)
源代码网推荐 {
源代码网推荐
源代码网推荐 string[,] tempArray = twoArray;
源代码网推荐 twoArray = new string[2,j+2];
源代码网推荐 for(int x=0;x<2;x++)
源代码网推荐 {
源代码网推荐 for(int y=0;y<j+1;y++)
源代码网推荐 {
源代码网推荐 twoArray[x,y] = tempArray[x,y];
源代码网推荐 }
源代码网推荐 }
源代码网推荐 j=j+1;
源代码网推荐 }
源代码网推荐/// <summary>
源代码网推荐/// 引用单词数组,和要加入单词数组的单词
源代码网推荐/// </summary>
源代码网推荐/// <param name="stringArrange"></param>
源代码网推荐/// <param name="st"></param>
源代码网推荐 private void stringToArrayString(ref string[] stringArrange, string st)
源代码网推荐 {
if(stringArrange[0] =="")
源代码网推荐 {
源代码网推荐 stringArrange[0] = st;
源代码网推荐 }
源代码网推荐 else
源代码网推荐 {
源代码网推荐 string[] oldA = stringArrange;//刚得到的字符串
源代码网推荐 int i=oldA.Length +1;
源代码网推荐 stringArrange = new string[i];//申请一个长一个的字符数组。
源代码网推荐 oldA.CopyTo(stringArrange,0);//将先前的字符数组考到现在这个数组中。
源代码网推荐 stringArrange[stringArrange.Length -1] = st;
源代码网推荐 }
源代码网推荐 }
源代码网推荐/// <summary>
源代码网推荐/// 将Text中的字符串,存入一个字符数组中。
源代码网推荐/// </summary>
源代码网推荐/// <returns></returns>
源代码网推荐 private char[] textToCharArray()
源代码网推荐 {
源代码网推荐 string stringTemp;
源代码网推荐 stringTemp = this.richTextBox1.Text;
源代码网推荐 char[] getch = stringTemp.ToCharArray();//要处理的字符都在getch这个数组中。
源代码网推荐 return getch;
源代码网推荐 }
源代码网推荐/// <summary>
源代码网推荐/// 字符数组 到 单词数组
源代码网推荐/// </summary>
源代码网推荐/// <param name="getch"></param>
源代码网推荐/// <returns></returns>
源代码网推荐 private string[] charArrayToStringArray(char[] getch)//将字符数组转换为字符串数组。即词法分析后的单词数组。
源代码网推荐 {
源代码网推荐 string[] stringArrange={""} ;//用这个字符串数组存放词法分析后得到的单词。
源代码网推荐 char charTemp;
源代码网推荐 string stringSave = "";// 存放一个分析得到的单词
//一次循环因得到一个单词。
源代码网推荐 for(int i = 0;i < getch.Length;i++)
源代码网推荐 {
源代码网推荐 charTemp = getch[i];
源代码网推荐
源代码网推荐 //由字母开头 数字和字母组成的单词。
源代码网推荐 if( charTemp >= "a"&&
源代码网推荐 charTemp <= "z"
源代码网推荐 ||
源代码网推荐 charTemp >= "A" &&
源代码网推荐 charTemp <= "Z")
源代码网推荐 {
源代码网推荐 stringSave = charTemp.ToString();
源代码网推荐 i = i + 1;
源代码网推荐 int test = 0;//判断循环是否结束,1 为结束。
源代码网推荐 while(test == 0)
源代码网推荐 {
源代码网推荐 charTemp = getch[i];
源代码网推荐 if( charTemp >= "a"&&
源代码网推荐 charTemp <= "z"
源代码网推荐 ||
源代码网推荐 charTemp >= "A" &&
源代码网推荐 charTemp <= "Z"
源代码网推荐 ||
源代码网推荐 charTemp >= "0" &&
源代码网推荐 charTemp <= "9")
源代码网推荐 {
源代码网推荐 stringSave = stringSave + charTemp.ToString();
源代码网推荐 i = i + 1;
源代码网推荐 }
源代码网推荐 else
源代码网推荐 test = 1;
源代码网推荐 }
源代码网推荐 stringToArrayString(ref stringArrange,stringSave);
源代码网推荐 }
源代码网推荐 stringSave = "";
源代码网推荐 //由数字组成的单词。
源代码网推荐 if( charTemp >= "0" &&
源代码网推荐 charTemp <= "9")
源代码网推荐 {
源代码网推荐 stringSave = stringSave + charTemp.ToString();
源代码网推荐 i = i + 1;
源代码网推荐 int test1 = 0;
源代码网推荐 while(test1 == 0)
源代码网推荐 {
源代码网推荐 charTemp = getch[i];
源代码网推荐 if( charTemp >= "0" &&
源代码网推荐 charTemp <= "9")
源代码网推荐 {
源代码网推荐 stringSave = stringSave + charTemp.ToString();
源代码网推荐 i = i + 1;
源代码网推荐 }
源代码网推荐 else
源代码网推荐 test1 = 1;
源代码网推荐 }
源代码网推荐 stringToArrayString(ref stringArrange,stringSave);
源代码网推荐 }
源代码网推荐 stringSave = "";
源代码网推荐 //由运算符组成的单词。
源代码网推荐 if( charTemp == "+"
源代码网推荐 || charTemp == "-"
源代码网推荐 || charTemp == "*"
源代码网推荐 || charTemp == "/"
源代码网推荐 || charTemp == "="
源代码网推荐 || charTemp == "<"
源代码网推荐 || charTemp == ">"
源代码网推荐 || charTemp == "!")
源代码网推荐 {
源代码网推荐 stringSave = stringSave + charTemp.ToString();
源代码网推荐 i = i + 1;
源代码网推荐 int test2 = 0;
源代码网推荐 while(test2 == 0)
源代码网推荐 {
源代码网推荐 charTemp = getch[i];
源代码网推荐 if( charTemp == "+"
源代码网推荐 || charTemp == "-"
源代码网推荐 || charTemp == "*"
源代码网推荐 || charTemp == "/"
源代码网推荐 || charTemp == "="
源代码网推荐 || charTemp == "<"
源代码网推荐 || charTemp == ">"
源代码网推荐 || charTemp == "!")
源代码网推荐 {
源代码网推荐 stringSave = stringSave + charTemp.ToString();
源代码网推荐 i = i + 1;
源代码网推荐 }
源代码网推荐 else
源代码网推荐 test2 = 1;
源代码网推荐 }
源代码网推荐 stringToArrayString(ref stringArrange,stringSave);
源代码网推荐 }
源代码网推荐 stringSave = "";
源代码网推荐 //由介符组成的单词。
源代码网推荐 if( charTemp == "("
源代码网推荐 ||charTemp == ")"
源代码网推荐 ||charTemp == "{"
源代码网推荐 ||charTemp == "}"
源代码网推荐 ||charTemp == "["
源代码网推荐 ||charTemp == "]"
源代码网推荐 ||charTemp == ","
源代码网推荐 ||charTemp == ":"
源代码网推荐 ||charTemp == ";"
源代码网推荐 ||charTemp == """
源代码网推荐 ||charTemp == """
源代码网推荐 ||charTemp == "\")
源代码网推荐 {
源代码网推荐 stringSave = stringSave + charTemp.ToString();
源代码网推荐 stringToArrayString(ref stringArrange,stringSave);
源代码网推荐 }
源代码网推荐 }
源代码网推荐 return stringArrange;
源代码网推荐 }
源代码网推荐/// <summary>
源代码网推荐/// 单词数组 到 二维单词数组。
源代码网推荐/// </summary>
源代码网推荐/// <param name="stringArray"></param>
源代码网推荐/// <returns></returns>
源代码网推荐 private string[,] stringArrayToTwoStringArray(string[] stringArray)
源代码网推荐 {
源代码网推荐 //存放单词标识后的结果。
源代码网推荐 string [,] twoArray = new string[2,1];
源代码网推荐 //单词的标志
源代码网推荐 int j=0;
源代码网推荐 //每循环一次,把一个单词归于一类,即前面加上一个数字。
源代码网推荐 for(int i=0;i<stringArray.Length;i++)
源代码网推荐 {
源代码网推荐 //保留字 1
源代码网推荐 if( stringArray[i] == "main"
源代码网推荐 || stringArray[i] == "int"
源代码网推荐 || stringArray[i] == "float"
源代码网推荐 || stringArray[i] == "printf"
源代码网推荐 || stringArray[i] == "if"
源代码网推荐 || stringArray[i] == "for"
源代码网推荐 || stringArray[i] == "while"
源代码网推荐 || stringArray[i] == "do"
源代码网推荐 || stringArray[i] == "return"
源代码网推荐 || stringArray[i] == "break"
源代码网推荐 || stringArray[i] == "continue")
源代码网推荐 {
源代码网推荐 twoArray[0,j] = "1";
源代码网推荐 twoArray[1,j] = stringArray[i];
源代码网推荐 this.oneArrayToTwo(ref twoArray,ref j);
源代码网推荐 }
源代码网推荐 //运算符 2
源代码网推荐 else
源代码网推荐 if( stringArray[i] == "+"
源代码网推荐 || stringArray[i] == "-"
源代码网推荐 || stringArray[i] == "*"
源代码网推荐 || stringArray[i] == "/"
源代码网推荐 || stringArray[i] == ">"
源代码网推荐 || stringArray[i] == "<"
源代码网推荐 || stringArray[i] == ">="
源代码网推荐 || stringArray[i] == "<="
源代码网推荐 || stringArray[i] == "!="
源代码网推荐 || stringArray[i] == "=="
源代码网推荐 || stringArray[i] == "++"
源代码网推荐 || stringArray[i] == "--"
源代码网推荐 || stringArray[i] == "=")
源代码网推荐 {
源代码网推荐 twoArray[0,j] = "2";
源代码网推荐 twoArray[1,j] = stringArray[i];
源代码网推荐 this.oneArrayToTwo(ref twoArray,ref j);
源代码网推荐 }
源代码网推荐 //分隔符 3
源代码网推荐 else
源代码网推荐 if( stringArray[i] == "("
源代码网推荐 || stringArray[i] == ")"
源代码网推荐 || stringArray[i] == "{"
源代码网推荐 || stringArray[i] == "}"
源代码网推荐 || stringArray[i] == "["
源代码网推荐 || stringArray[i] == "]"
源代码网推荐 || stringArray[i] == ","
源代码网推荐 || stringArray[i] == ";"
源代码网推荐 || stringArray[i] == ":"
源代码网推荐 || stringArray[i] == """
源代码网推荐 || stringArray[i] == "/*"
源代码网推荐 || stringArray[i] == "*/")
源代码网推荐 {
源代码网推荐 twoArray[0,j] = "3";
源代码网推荐 twoArray[1,j] = stringArray[i];
源代码网推荐 this.oneArrayToTwo(ref twoArray,ref j);
源代码网推荐 }
源代码网推荐 //数字 4
源代码网推荐 else
源代码网推荐 if( stringArray[i].ToCharArray()[0] >="0" &&
源代码网推荐 stringArray[i].ToCharArray()[0] <= "9")
源代码网推荐 {
源代码网推荐 twoArray[0,j] = "4";//数字
源代码网推荐 twoArray[1,j] = stringArray[i];
源代码网推荐 this.oneArrayToTwo(ref twoArray,ref j);
源代码网推荐 }
源代码网推荐 //其它 5(变量等)
源代码网推荐 else
源代码网推荐 {
源代码网推荐 twoArray[0,j] = "5";
源代码网推荐 twoArray[1,j] = stringArray[i];
源代码网推荐 this.oneArrayToTwo(ref twoArray,ref j);
源代码网推荐 }
源代码网推荐 }
源代码网推荐 return twoArray;
源代码网推荐 }
}
源代码网推荐}