欢迎光临
我们一直在努力

一个用C#写的词法分析程序-.NET教程,C#语言

建站超值云服务器,限时71元/月

源文件内容:

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 -> 保留字" + "\r\n" +

"2 -> 运算符" + "\r\n" +

"3 -> 分隔符" + "\r\n" +

"4 -> 数字 " + "\r\n" +

"5 -> 其它" + "\r\n";

//输出二维数组中的数据

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 + "\r\n";

}

}

}

}

/// <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;

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个用C#写的词法分析程序-.NET教程,C#语言
分享到: 更多 (0)