词法分析器用C实现Word文档下载推荐.doc

上传人:wj 文档编号:1451976 上传时间:2023-04-30 格式:DOC 页数:13 大小:106KB
下载 相关 举报
词法分析器用C实现Word文档下载推荐.doc_第1页
第1页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第2页
第2页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第3页
第3页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第4页
第4页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第5页
第5页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第6页
第6页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第7页
第7页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第8页
第8页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第9页
第9页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第10页
第10页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第11页
第11页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第12页
第12页 / 共13页
词法分析器用C实现Word文档下载推荐.doc_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

词法分析器用C实现Word文档下载推荐.doc

《词法分析器用C实现Word文档下载推荐.doc》由会员分享,可在线阅读,更多相关《词法分析器用C实现Word文档下载推荐.doc(13页珍藏版)》请在冰点文库上搜索。

词法分析器用C实现Word文档下载推荐.doc

saveFileDialog1.Filter="

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="

***********************符号表信息******************"

+"

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="

入口:

单词名称长度类型种属值内存地址"

//用来记录符号表

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]!

='

) //读入字符判断,空格、字母、数字、界符

{

if(str[i]=='

'

||str[i]=='

\t'

\r'

{

i++;

//跳过无意义的字符

}

elseif(str[i]=='

\n'

) //如果是换行符,则行号加1

LineNo++;

elseif(isalpha(str[i])) //如果是字母

i=recog_id(str,i);

for(j=0;

j<

m_KeyWords.Length;

j++)

{

if(token.CompareTo(m_KeyWords[j])==0)

break;

}

if(j>

=m_KeyWords.Length) //不是保留字

for(intm=0;

m<

12-token.Length;

m++)

space=space+"

"

text2=text2+LineNo.ToString()+"

"

+token+space+"

标识符Token码75"

;

text4=text4+LineNo.ToString()+"

+token+"

"

+token.Length+space+"

标识符"

简单变量"

未知"

未知"

token="

space="

if(j<

m_KeyWords.Length) //是保留字

12-token.Length;

+m_KeyWords[j]+space+"

保留字Token码"

+Convert.ToString(gettoken(token,1))+"

space="

elseif(isdigit(str[i])) //如果是数字

i=recog_dig(str,i);

for(intm=0;

space=space+"

text2=text2+LineNo.ToString()+"

常量Token码76"

text4=text4+LineNo.ToString()+"

整数"

"

token="

space="

elseif(isbound(str[i]))//识别界符

i=recog_bound(str,i);

space=space+"

界符Token码"

+Convert.ToString(gettoken(token,3))+"

elseif(isoperator(str[i]))

i=recog_Operator(str,i);

m<

运算符Token码"

+Convert.ToString(gettoken(token,2))+"

else{error(0);

i++;

}

}

catch(DivideByZeroExceptione1)

wrong=e1.Message;

catch(IndexOutOfRangeExceptione2)

wrong=e2.Message;

catch(Exceptione)

wrong=e.Message;

returntext2;

publicboolisalpha(charch)//判断是否为字母

if((ch>

a'

&

ch<

z'

)||(ch>

A'

Z'

))

returntrue;

elsereturnfalse;

publicboolisdigit(charch)//判断是否为数字

if(ch>

0'

9'

publicboolissign(charch)//识别下划线

if(ch=='

_'

publicboolisbound(charch)//判断是否为界符

for(intj=0;

j<

m_bound.Length;

j++)

if(ch.CompareTo(m_bound[j][0])==0)

returntrue;

returnfalse;

publicboolisoperator(charch)

for(inti=0;

i<

m_operator.Length;

i++)

if(ch==m_operator[i][0])

returnfalse;

privateintrecog_Operator(stringstr,inti)

charstate='

stringsstr="

while(state!

2'

switch(state)

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

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2