资源描述:
《词法分析器用C#实现.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、设计界面如上图所示:代码如下:namespaceWindowsApplication1{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}stringStr="",text2="";privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse){stringFname="";openFileDialog1.Filter="文本文件(*.txt)
2、*.txt";openFileDialog1.Title=
3、"打开文件";openFileDialog1.RestoreDirectory=true;if(openFileDialog1.ShowDialog()==DialogResult.OK)Fname=openFileDialog1.FileName;richTextBox1.LoadFile(Fname,RichTextBoxStreamType.PlainText);Str=richTextBox1.Text;}privatevoid保存ToolStripMenuItem_Click(objectsender,EventArgse){str
4、ingFname=openFileDialog1.FileName;richTextBox1.SaveFile(Fname,RichTextBoxStreamType.PlainText);}privatevoid另存为ToolStripMenuItem_Click(objectsender,EventArgse){stringFname;saveFileDialog1.Filter="文本文件(*.txt)
5、*.txt";saveFileDialog1.Title="保存文件";saveFileDialog1.RestoreDirector
6、y=true;saveFileDialog1.ShowDialog();Fname=saveFileDialog1.FileName;if(Fname!="")richTextBox1.SaveFile(Fname,RichTextBoxStreamType.PlainText);}privatevoid关闭ToolStripMenuItem_Click(objectsender,EventArgse){Application.Exit();}privatevoid字体ToolStripMenuItem_Click(objectsender,
7、EventArgse){fontDialog1.ShowEffects=true;fontDialog1.Font=richTextBox1.SelectionFont;if(fontDialog1.ShowDialog()==DialogResult.OK)richTextBox1.SelectionFont=fontDialog1.Font;}privatevoid词法分析ToolStripMenuItem_Click(objectsender,EventArgse){Gets.LineNo=1;//初始化行号Gets.errors=0;
8、//初始化错误个数Gets.text5="";stringtext1=richTextBox1.Text+' ';Getsgetstring=newGets();text2=getstring.GetString(text1);richTextBox2.Text="*****************Token串生成表如下********************"+"r"+text2;textBox2.Text=getstring.ErrorN0();}privatevoidForm1_Load(objectsender,EventAr
9、gse){}privatevoid符号表ToolStripMenuItem_Click(objectsender,EventArgse){textBox2.Text="***********************符号表信息******************"+"r";textBox2.Text+=Gets.text4;}privatevoid错误详细信息ToolStripMenuItem_Click(objectsender,EventArgse){textBox2.Text=Gets.text5;}}主要实现功能的代码如下:创建一
10、个Gets类用来对输入的字符串进行扫描classGets{inti=0,j=0;//记录字符位置,token数组的位置publicstaticintLineNo=1