编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx

上传人:b****5 文档编号:20353011 上传时间:2023-01-22 格式:DOCX 页数:17 大小:87.79KB
下载 相关 举报
编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx_第1页
第1页 / 共17页
编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx_第2页
第2页 / 共17页
编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx_第3页
第3页 / 共17页
编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx_第4页
第4页 / 共17页
编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx

《编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx(17页珍藏版)》请在冰豆网上搜索。

编译原理词法分析实验一词法分析扫描器的设计实现本科论文Word格式文档下载.docx

iostream>

#include<

string.h>

stdlib.h>

usingnamespacestd;

/***********定义结构体***********/

typedefstructToken

{

charname[20];

intstate;

//状态,为0是标识符,为1是字符,为2是字符串,为3是数字,为4是关键字,为5是界符

}Token;

typedefstructSTR//

intcode;

}STR;

STRiT[10];

STRcT[20];

STRsT[20];

STRCT[20];

STRKT[10]={{"

int"

4},{"

main"

5},{"

void"

6},{"

if"

7},{"

else"

8},{"

char"

9}};

STRPT[30]={{"

>

="

10},{"

<

11},{"

=="

12},{"

13},{"

"

14},{"

15},{"

+"

16},{"

-"

17},{"

*"

18},{"

/"

19},

{"

{"

20},{"

}"

21},{"

"

22},{"

;

23},{"

("

24},{"

)"

25},

["

26},{"

]"

27}};

/***********全局变量声明*********/

TokenCurrentToken;

//正在读的token

Tokentoken[200];

FILE*fp;

//源文件指针

charch;

//读取字符

intstate=0;

//0表示标识符,1表示字符,2表示字符串

intiT_i=0;

//iT结构体数组下标

intcT_i=0;

intsT_i=0;

intCT_i=0;

intindex=0;

//token的下标

/***函数声明***/

//读的是字母,关键字和标示符

//,;

+-

/******************主函数**********************/

intmain()

inti=0;

if((fp=fopen("

source.txt"

r"

))==NULL)

{

cout<

cannotopenthesourcefile!

\n"

endl;

exit

(1);

}

ch=fgetc(fp);

//文件中读一个字符

while(ch!

='

#'

for(i=0;

i<

20;

i++)

CurrentToken.name[i]='

\0'

//将单词缓冲区初始化

if((ch>

0'

)&

&

(ch<

9'

))//数字

IsNumber();

elseif(((ch>

a'

z'

))||((ch>

A'

Z'

))||(ch=='

_'

))//关键字和标识符

IsAlpha();

elseif(ch=='

'

||ch=='

\n'

else

IsOther();

i<

=index;

i++)

cout<

token[i].name<

"

;

finish"

return0;

}

/************数字处理****************/

voidIsNumber()

intk=0;

while(((ch>

)))

CurrentToken.name[k++]=ch;

//将数字放入单词缓冲区

boolflag=1;

if(ch=='

.'

if(((ch<

'

)||(ch>

flag=0;

if(flag==1)

strcpy(CT[CT_i++].name,CurrentToken.name);

strcpy(token[index].name,CurrentToken.name);

token[index++].state=3;

//cout<

CurrentToken.name<

<

03>

error>

/************是否为关键字****************/

voidIsAlpha()

inti=0;

if(state==0)

boolflag=0;

))||ch=='

)//将完整的单词放入单词缓冲区

CurrentToken.name[i++]=ch;

//判断是否是关键字

7;

if(strcmp(CurrentToken.name,KT[i].name)==0)

flag=1;

//printf("

%s<

%02d>

CurrentToken.name,KT[i].code);

strcpy(token[index].name,CurrentToken.name);

token[index++].state=4;

printf("

KT[i].code);

break;

if(flag!

=1)

||((ch>

00>

CurrentToken.name);

);

strcpy(token[index].name,CurrentToken.name);

token[index++].state=0;

inttag=0;

for(intj=0;

j<

iT_i;

j++)

{

if(strcmp(CurrentToken.name,iT[j].name)==0)

{

tag=1;

break;

}

}

if(tag==0)

strcpy(iT[iT_i].name,CurrentToken.name);

iT[iT_i++].code=0;

 

/***************其它************/

intIsOther()

inti;

//将缓冲区初始化

switch(ch)

case'

:

CurrentToken.name[0]='

if(ch=='

CurrentToken.name[1]='

+'

-'

*'

/'

{'

}'

'

('

)'

['

]'

case'

\'

ch<

))

CurrentToken.name[1]=ch;

CurrentToken.name[2]='

01>

strcpy(token[index].name,CurrentToken.name);

token[index++].state=1;

inttag=0;

for(intj=0;

=cT_i;

{

if(strcmp(cT[cT_i].name,CurrentToken.name)==0)

tag=1;

}

if(tag==0)

strcpy(cT[cT_i++].name,CurrentToken.name);

error1"

error2"

\"

intj=1;

while((ch>

CurrentToken.name[j++]=ch;

CurrentToken.name[j++]='

02>

=sT_i;

if(strcmp(sT[sT_i].name,CurrentToken.name)==0)

tag=1;

}if(tag==0)

strcpy(sT[sT_i++].name,CurrentToken.name);

error3"

if(CurrentToken.name[0]!

if(strcmp(CurrentToken.name,PT[i].name)==0)

%d>

CurrentToken.name,PT[i].code);

PT[i].code);

strcpy(token[index].name,CurrentToken.name);

token[index++].state=PT[i].code;

//while(ch=='

)ch=fgetc(fp);

程序运行结果:

(截屏)

输入:

Source.txt文本

intmain(void)

inta=1,d=2,c;

if(a<

=d)

{

c=a;

a=d;

d=c;

charch[10]="

ok"

charx,y='

c=a+d;

#

输出:

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

当前位置:首页 > PPT模板 > 动态背景

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

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