词法分析器用C实现.doc

上传人:b****2 文档编号:394375 上传时间:2022-10-09 格式:DOC 页数:13 大小:106KB
下载 相关 举报
词法分析器用C实现.doc_第1页
第1页 / 共13页
词法分析器用C实现.doc_第2页
第2页 / 共13页
词法分析器用C实现.doc_第3页
第3页 / 共13页
词法分析器用C实现.doc_第4页
第4页 / 共13页
词法分析器用C实现.doc_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

词法分析器用C实现.doc

《词法分析器用C实现.doc》由会员分享,可在线阅读,更多相关《词法分析器用C实现.doc(13页珍藏版)》请在冰豆网上搜索。

词法分析器用C实现.doc

设计界面如上图所示:

代码如下:

namespaceWindowsApplication1

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

stringStr="",text2="";

privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse)

{

stringFname="";

openFileDialog1.Filter="文本文件(*.txt)|*.txt";

openFileDialog1.Title="打开文件";

openFileDialog1.RestoreDirectory=true;

if(openFileDialog1.ShowDialog()==DialogResult.OK)

Fname=openFileDialog1.FileName;

richTextBox1.LoadFile(Fname,RichTextBoxStreamType.PlainText);

Str=richTextBox1.Text;

}

privatevoid保存ToolStripMenuItem_Click(objectsender,EventArgse)

{

stringFname=openFileDialog1.FileName;

richTextBox1.SaveFile(Fname,RichTextBoxStreamType.PlainText);

}

privatevoid另存为ToolStripMenuItem_Click(objectsender,EventArgse)

{

stringFname;

saveFileDialog1.Filter="文本文件(*.txt)|*.txt";

saveFileDialog1.Title="保存文件";

saveFileDialog1.RestoreDirectory=true;

saveFileDialog1.ShowDialog();

Fname=saveFileDialog1.FileName;

if(Fname!

="")

richTextBox1.SaveFile(Fname,RichTextBoxStreamType.PlainText);

}

privatevoid关闭ToolStripMenuItem_Click(objectsender,EventArgse)

{

Application.Exit();

}

privatevoid字体ToolStripMenuItem_Click(objectsender,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;//初始化错误个数

Gets.text5="";

stringtext1=richTextBox1.Text+'\0';

Getsgetstring=newGets();

text2=getstring.GetString(text1);

richTextBox2.Text="*****************Token串生成表如下********************"+"\r\n"+text2;

textBox2.Text=getstring.ErrorN0();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

}

privatevoid符号表ToolStripMenuItem_Click(objectsender,EventArgse)

{

textBox2.Text="***********************符号表信息******************"+"\r\n";

textBox2.Text+=Gets.text4;

}

privatevoid错误详细信息ToolStripMenuItem_Click(objectsender,EventArgse)

{

textBox2.Text=Gets.text5;

}

}

主要实现功能的代码如下:

创建一个Gets类用来对输入的字符串进行扫描

classGets

{

inti=0,j=0;//记录字符位置,token数组的位置

publicstaticintLineNo=1;//行号

stringtoken="";//记录识别出的单词

stringtext2="";

stringtext3="";//用来记录错误信息

stringwrong="";//记录异常信息

publicstaticinterrors=0;//错误字段的个数

publicstaticstringtext5="";//记录错误的详细信息

intflag1=0,flag2=0,flag3,flag4,flag5,flag6=0;//标记成对出现的界符

publicstaticstringtext4="入口:

单词名称长度类型种属值内存地址"+"\r\n";//用来记录符号表

string[]m_KeyWords=newstring[32]{"auto","double","int","struct","break","else","long","switch","case","enum",

"register","typedef","char","extern","return","union","const","float","short","unsigned","continue","for","signed","void","default",

"goto","sizeof","volatile","do","if","while","static"};

string[]m_operator=newstring[28]{"+","-","*","/","%",">","<",">=","<=","==","-=","+=","*=","/=",//运算符

"!

=","=","%=","&","&&","|","||","!

","++","--","~","<<",">>","?

:

"};

string[]m_bound=newstring[15]{"{","}","[","]",";",",",".","(",")",":

","\"","#",">","<","\'"};//界符

publicstringGetString(stringstr)//读入字符串

{

stringspace="";//空格数

if(str.Length==0)//判断字符串是否为空

return"";

try

{

while(str[i]!

='\0') //读入字符判断,空格、字母、数字、界符

{

if(str[i]==''||str[i]=='\t'||str[i]=='\r')

{

i++;//跳过无意义的字符

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1