分享
 
 
 

一个用C#写的词法分析程序

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

源文件内容:

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;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有