源文件内容:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace wzy2
{
///
1<summary>
2/// Form1 的摘要说明。
3/// </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;
///
1<summary>
2/// 必需的设计器变量。
3/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
1<summary>
2/// 清理所有正在使用的资源。
3/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </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
///
1<summary>
2/// 应用程序的主入口点。
3/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
1<summary>
2/// 词法分析函数
3/// </summary>
///
1<param name="sender"/>
///
1<param name="e"/>
private void button1_Click(object sender, System.EventArgs e)
{
//得到想要的字符数组。
char[] getch = textToCharArray();
//将字符数组,转换为词法分析后的 单词数组。
string[] stringArray = charArrayToStringArray(getch);
//将单词数组分类,用数字标出各个单词所在的类别。
string[,] twoStringArray = stringArrayToTwoStringArray(stringArray);
//用于输出二维数组。
printString(twoStringArray);
}
///
1<summary>
2/// 输出结果 即用于输出二维数组。
3/// </summary>
///
1<param name="twoStringArray"/>
private void printString(string[,] twoStringArray)
{
//提示说明
this.richTextBox2.Text ="1 -> 保留字" + "\r\n" +
"2 -> 运算符" + "\r\n" +
"3 -> 分隔符" + "\r\n" +
"4 -> 数字 " + "\r\n" +
"5 -> 其它" + "\r\n";
//输出二维数组中的数据
for(int x=0;x
1<twostringarray.length "="" ";="" "\r\n";="" +="" 2;x++)="" <summary="" for(int="" if(="" this.richtextbox2.text="this.richTextBox2.Text" twostringarray[y,x]="" y="1)" {="" }="">
2/// 打开方件
3///
4/// <param name="sender"/>
5/// <param name="e"/>
6private void button2_Click(object sender, System.EventArgs e)// 文件打开的方法。
7{
8openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
9openFileDialog1.Title = "打开要分析的源文件。";
10
11if(openFileDialog1.ShowDialog() == DialogResult.OK)
12{
13System.IO.StreamReader sr = new
14System.IO.StreamReader(openFileDialog1.FileName);
15this.richTextBox1.Text = sr.ReadToEnd();
16sr.Close();
17}
18}
19/// <summary>
20/// 保存文件
21/// </summary>
22/// <param name="sender"/>
23/// <param name="e"/>
24private void button3_Click(object sender, System.EventArgs e)
25{
26saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
27saveFileDialog1.Title = "保存分析结果.";
28if(saveFileDialog1.ShowDialog() == DialogResult.OK)
29{
30System.IO.StreamWriter wr = new
31System.IO.StreamWriter(saveFileDialog1.FileName);
32wr.Write(this.richTextBox2.Text);
33wr.Close();
34}
35}
36
37/// <summary>
38/// 引用二维数组和单词的标志j
39/// </summary>
40/// <param name="twoArray"/>
41/// <param name="j"/>
42private void oneArrayToTwo(ref string[,] twoArray,ref int j)
43{
44
45string[,] tempArray = twoArray;
46twoArray = new string[2,j+2];
47for(int x=0;x<2;x++)
48{
49for(int y=0;y<j+1;y++) <summary="" j="j+1;" twoarray[x,y]="tempArray[x,y];" {="" }="">
50/// 引用单词数组,和要加入单词数组的单词
51///
52/// <param name="stringArrange"/>
53/// <param name="st"/>
54private void stringToArrayString(ref string[] stringArrange, string st)
55{
56
57if(stringArrange[0] =="")
58{
59stringArrange[0] = st;
60}
61else
62{
63string[] oldA = stringArrange;//刚得到的字符串
64int i=oldA.Length +1;
65stringArrange = new string[i];//申请一个长一个的字符数组。
66oldA.CopyTo(stringArrange,0);//将先前的字符数组考到现在这个数组中。
67stringArrange[stringArrange.Length -1] = st;
68}
69}
70/// <summary>
71/// 将Text中的字符串,存入一个字符数组中。
72/// </summary>
73/// <returns></returns>
74private char[] textToCharArray()
75{
76string stringTemp;
77stringTemp = this.richTextBox1.Text;
78char[] getch = stringTemp.ToCharArray();//要处理的字符都在getch这个数组中。
79return getch;
80}
81/// <summary>
82/// 字符数组 到 单词数组
83/// </summary>
84/// <param name="getch"/>
85/// <returns></returns>
86private string[] charArrayToStringArray(char[] getch)//将字符数组转换为字符串数组。即词法分析后的单词数组。
87{
88string[] stringArrange={""} ;//用这个字符串数组存放词法分析后得到的单词。
89char charTemp;
90string stringSave = "";// 存放一个分析得到的单词
91
92//一次循环因得到一个单词。
93for(int i = 0;i < getch.Length;i++)
94{
95charTemp = getch[i];
96
97//由字母开头 数字和字母组成的单词。
98if( charTemp >= 'a'&&
99charTemp <= 'z'
100||
101charTemp >= 'A' &&
102charTemp <= 'Z')
103{
104stringSave = charTemp.ToString();
105i = i + 1;
106int test = 0;//判断循环是否结束,1 为结束。
107while(test == 0)
108{
109charTemp = getch[i];
110if( charTemp >= 'a'&&
111charTemp <= 'z'
112||
113charTemp >= 'A' &&
114charTemp <= 'Z'
115||
116charTemp >= '0' &&
117charTemp <= '9')
118{
119stringSave = stringSave + charTemp.ToString();
120i = i + 1;
121}
122else
123test = 1;
124}
125stringToArrayString(ref stringArrange,stringSave);
126}
127stringSave = "";
128//由数字组成的单词。
129if( charTemp >= '0' &&
130charTemp <= '9')
131{
132stringSave = stringSave + charTemp.ToString();
133i = i + 1;
134int test1 = 0;
135while(test1 == 0)
136{
137charTemp = getch[i];
138if( charTemp >= '0' &&
139charTemp <= '9')
140{
141stringSave = stringSave + charTemp.ToString();
142i = i + 1;
143}
144else
145test1 = 1;
146}
147stringToArrayString(ref stringArrange,stringSave);
148}
149stringSave = "";
150//由运算符组成的单词。
151if( charTemp == '+'
152|| charTemp == '-'
153|| charTemp == '*'
154|| charTemp == '/'
155|| charTemp == '='
156|| charTemp == '<'
157|| charTemp == '>'
158|| charTemp == '!')
159{
160stringSave = stringSave + charTemp.ToString();
161i = i + 1;
162int test2 = 0;
163while(test2 == 0)
164{
165charTemp = getch[i];
166if( charTemp == '+'
167|| charTemp == '-'
168|| charTemp == '*'
169|| charTemp == '/'
170|| charTemp == '='
171|| charTemp == '<'
172|| charTemp == '>'
173|| charTemp == '!')
174{
175stringSave = stringSave + charTemp.ToString();
176i = i + 1;
177}
178else
179test2 = 1;
180}
181stringToArrayString(ref stringArrange,stringSave);
182}
183stringSave = "";
184//由介符组成的单词。
185if( charTemp == '('
186||charTemp == ')'
187||charTemp == '{'
188||charTemp == '}'
189||charTemp == '['
190||charTemp == ']'
191||charTemp == ','
192||charTemp == ':'
193||charTemp == ';'
194||charTemp == '"'
195||charTemp == '\''
196||charTemp == '\\\')
197{
198stringSave = stringSave + charTemp.ToString();
199stringToArrayString(ref stringArrange,stringSave);
200}
201}
202return stringArrange;
203}
204/// <summary>
205/// 单词数组 到 二维单词数组。
206/// </summary>
207/// <param name="stringArray"/>
208/// <returns></returns>
209private string[,] stringArrayToTwoStringArray(string[] stringArray)
210{
211//存放单词标识后的结果。
212string [,] twoArray = new string[2,1];
213//单词的标志
214int j=0;
215
216
217//每循环一次,把一个单词归于一类,即前面加上一个数字。
218for(int i=0;i<stringarray.length;i++) "="" )="" 1="" 2="" 3="" 4="" ;="" else="" if(="" j);="" stringarray[i]="*/" stringarray[i].tochararray()[0]="" this.onearraytotwo(ref="" twoarray,ref="" twoarray[0,j]="3" twoarray[1,j]="stringArray[i];" {="" ||="" }="" 保留字="" 分隔符="" 数字="" 运算符="">='0' &&
219stringArray[i].ToCharArray()[0] <= '9')
220{
221twoArray[0,j] = "4";//数字
222twoArray[1,j] = stringArray[i];
223this.oneArrayToTwo(ref twoArray,ref j);
224}
225//其它 5(变量等)
226else
227{
228twoArray[0,j] = "5";
229twoArray[1,j] = stringArray[i];
230this.oneArrayToTwo(ref twoArray,ref j);
231}
232}
233return twoArray;
234}
235
236}
237}</stringarray.length;i++)></j+1;y++)></twostringarray.length>