简单词法分析程序设计Word文档格式.docx

上传人:b****6 文档编号:19468104 上传时间:2023-01-06 格式:DOCX 页数:9 大小:16.91KB
下载 相关 举报
简单词法分析程序设计Word文档格式.docx_第1页
第1页 / 共9页
简单词法分析程序设计Word文档格式.docx_第2页
第2页 / 共9页
简单词法分析程序设计Word文档格式.docx_第3页
第3页 / 共9页
简单词法分析程序设计Word文档格式.docx_第4页
第4页 / 共9页
简单词法分析程序设计Word文档格式.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

简单词法分析程序设计Word文档格式.docx

《简单词法分析程序设计Word文档格式.docx》由会员分享,可在线阅读,更多相关《简单词法分析程序设计Word文档格式.docx(9页珍藏版)》请在冰豆网上搜索。

简单词法分析程序设计Word文档格式.docx

7、根据常量说明串置于高级语言源程序中时可能出现的错误情况,模仿高级语言编译器对不同错误情况做出相应处理。

四、运行结果

1、输入如下正确的常量说明串:

constcount=10,sum=81.5,char1=‘f’,max=169,str1=“h*542..4S!

AAsj”,char2=‘@’,str2=“aa!

+h”;

输出:

count(integer,10)

sum(float,81.5)

char1(char,‘f’)

max(integer,169)

str1(string,“h*542..4S!

AAsj”)

char2(char,‘@’)

str2(string,“aa!

+h”)

int_num=2;

char_num=2;

string_num=2;

float_num=1.

2、输入类似如下的保留字const错误的常量说明串:

Aconsttcount=10,sum=81.5,char1=‘f’;

输出类似下面的错误提示信息:

Itisnotaconstantdeclarationstatement!

Pleaseinputastringagain!

3、输入类似如下含常量名或常量值错误的常量说明串:

constcount=10,12sum=81.5,char1=‘ff’,max=0016;

12sum(Wrong!

Itisnotaidentifier!

char1(Wrong!

Therearemorethanonecharin‘’.)

max(Wrong!

Theintegercan’tbestartedwith‘0’.)

int_num=1;

char_num=0;

string_num=0;

float_num=0.

4、其他类型的错误处理情况(略)。

五、提示

本实验重点有三个:

一是作为常量名的标识符的识别;

二是如何根据“=”后出现的内容来判断常量类型;

三是对各种错误的处理。

难点是对整型和实型常量的判断必须综合考虑多种可能情况。

建议:

1、用指针或数组与指针相结合来处理输入的常量说明串;

2、对整型和实型常量处理时,重点考虑常数中‘0’的位置。

六、分析与讨论

1、若考虑用E或e的科学计数法来表示整数和实数,应该如何实现?

2、若考虑布尔型常量,且规定其值只能为true或false,应该如何实现?

3、如何对手工构造的词法分析程序做进一步的优化,以提高代码质量和运行效率?

 

七.代码

#include"

stdio.h"

ctype.h"

#include<

stdlib.h>

string.h>

windows.h>

#defineN80

#defineM16

char*p0,*t0,*p1,*t1,*p2,*str,*p3,*t3="

const"

*digi,*flo;

charcha;

intci=0,cf=0,cc=0,cs=0;

voidmain()

{

voidmeasurefw();

voidmeasure_iden();

voidmeasure_str();

voidmeasure_digit();

voidmeasure_cha();

printf("

\nInputstring:

\n"

);

p0=(char*)malloc(N*sizeof(char));

p1=(char*)malloc(M*sizeof(char));

p2=(char*)malloc(M*sizeof(char));

p3=(char*)malloc(M*sizeof(char));

gets(p0);

\n"

measurefw();

/*开始处理输入串p0*/

while(*(p0)=='

'

||*(p0)=='

'

)/*从串p0中取标识符,并将其作为常量名存放到串p1中*/

{

p0++;

if(*(p0)=='

continue;

else

{

measure_iden();

//*p0=="

="

if(!

isdigit(*p0)&

&

*p0!

='

\"

'

\'

)//注意'

和"

做字符表示时用\'

和\"

system("

cls"

printf("

\nConstdataiswrong.Exit!

"

exit(0);

}

elseif(*p0=='

)/*处理字符串常量*/

{

p0++;

measure_str();

}

elseif(isdigit(*p0))/*处理数字*/

measure_digit();

)//处理字符常量

measure_cha();

if(*p0!

;

system("

printf("

\nThiscentenceiswrong.Exit!

exit(0);

else

int_num=%d;

char_num=%d;

string_num=%d;

float_num=%d.\n"

ci,cc,cs,cf);

}

voidmeasurefw()

while(*p0!

)/*检查输入串是否以"

开头*/

if(*p0==*t3)

t3++;

\nThisstringisn'

taconstdeclaration!

//printf("

Thisstringisaconstdeclaration!

voidmeasure_iden()

if(*p0!

_'

(!

isalpha(*p0)))

{

\nConstnameiswrong.Exit!

elseif(*p0=='

||isalpha(*p0))

t1=p1;

while(*p0!

*p1=*p0;

p1++;

*p1='

\0'

//printf("

%s\n"

p0);

%s"

t1);

voidmeasure_str()

str=p2;

while(*(p0)!

*p2=*p0;

)//丢了个分号,直接输出Stringdataiswrong.Exit

\nStringdataiswrong.Exit!

p2++;

*p2='

p0++;

cs++;

(string,\"

%s\"

)\n"

str);

voidmeasure_digit()

char*jud;

intmark=0;

jud=p0;

for(;

*(jud)!

jud++)

if(*jud=='

.'

mark=1;

break;

if(mark==0)

digi=p2;

*p2=*p0;

p2++;

*p2='

ci++;

(integer,%s)\n"

digi);

if(mark==1)

flo=p2;

cf++;

(float,%s)\n"

flo);

voidmeasure_cha()

if(*(jud+1)=='

cha=*p0;

p0=p0+2;

cc++;

\nchardataiswrong.Exit!

(char,'

%c'

cha);

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

当前位置:首页 > 医药卫生 > 中医中药

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

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