词法分析报告附代码dev c++.docx

上传人:b****3 文档编号:11168678 上传时间:2023-05-29 格式:DOCX 页数:17 大小:81.11KB
下载 相关 举报
词法分析报告附代码dev c++.docx_第1页
第1页 / 共17页
词法分析报告附代码dev c++.docx_第2页
第2页 / 共17页
词法分析报告附代码dev c++.docx_第3页
第3页 / 共17页
词法分析报告附代码dev c++.docx_第4页
第4页 / 共17页
词法分析报告附代码dev c++.docx_第5页
第5页 / 共17页
词法分析报告附代码dev c++.docx_第6页
第6页 / 共17页
词法分析报告附代码dev c++.docx_第7页
第7页 / 共17页
词法分析报告附代码dev c++.docx_第8页
第8页 / 共17页
词法分析报告附代码dev c++.docx_第9页
第9页 / 共17页
词法分析报告附代码dev c++.docx_第10页
第10页 / 共17页
词法分析报告附代码dev c++.docx_第11页
第11页 / 共17页
词法分析报告附代码dev c++.docx_第12页
第12页 / 共17页
词法分析报告附代码dev c++.docx_第13页
第13页 / 共17页
词法分析报告附代码dev c++.docx_第14页
第14页 / 共17页
词法分析报告附代码dev c++.docx_第15页
第15页 / 共17页
词法分析报告附代码dev c++.docx_第16页
第16页 / 共17页
词法分析报告附代码dev c++.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

词法分析报告附代码dev c++.docx

《词法分析报告附代码dev c++.docx》由会员分享,可在线阅读,更多相关《词法分析报告附代码dev c++.docx(17页珍藏版)》请在冰点文库上搜索。

词法分析报告附代码dev c++.docx

词法分析报告附代码devc++

 

中北大学软件学院

 

实验报告

 

专业

课程名称

学号

姓名

辅导教师成绩

实验日期

实验时间

1实验名称:

词法分析器的设计与实现

2、实验目的

(1)掌握C语言单词符号的划分、正规式、状态转换图及词法分析器的实现。

(2)掌握词法分析程序的作用。

3、实验要求

(1)对任给的一个C语言源程序,能够滤掉空格、回车换行符、tab键及注释。

(2)识别各类单词符号,如关键字、标识符、运算符、常数、界符,结果以二元式形式输出,并构造符号表。

(3)输出有词法错误的单词及所在行号。

(在此阶段只能识别有限的词法错误)

4、实验原理

根据扫描到的单词符号的第一个字符的种类,分别转到相应的程序进行处理。

这些程序的功能就是识别以相应字符开头的各类单词符号。

5、实验步骤

(1)根据C语言各类单词的正规式,构造能识别各类单词的状态转换图。

(2)根据状态转换图,构造识别各类单词的词法分析器。

6、状态转换图及词法分析程序

(1)标识符(ID)和整型常数(NUM)的正规式如下:

ID=_|letter(letter|digit)*

NUM=digitdigit*

其中标识符的状态转换图为:

 

其词法分析程序为:

if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_'))

{/*以字母开头*/

while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9')))

{

array[i++]=ch;

ch=fgetc(fpin);

}

word=(char*)malloc((i+1)*sizeof(char));

memcpy(word,array,i);

word[i]='\0';

is_id_key(word);

if(ch!

=EOF)

fseek(fpin,-1L,SEEK_CUR);

}

常数的状态转换图为:

 

其词法分析程序为:

elseif(ch>='0'&&ch<='9')

{/*以数字开头*/

while(ch>='0'&&ch<='9')

{

array[i++]=ch;

ch=fgetc(fpin);

}

word=(char*)malloc((i+1)*sizeof(char));

is_number(word,array,i);

word[i]='\0';

cs_manage(word);

if(ch!

=EOF)

fseek(fpin,-1L,SEEK_CUR);

}

(2)实验流程图

7、测试及结果

8、心得

 

#include

#include

#include

#include

voidinit();

intscan();

char*limit[]={";","{","}","(",")","\"","\'","+","-","*","/","%",">","<","==",">=","<=","!

=","!

","&&","||","<<",">>","~","|","^","&","?

",":

",",",".","->","[","]","++","--","+=","-=","*=","/=","%="};

//////////////////////////////////////////////////////(判断是否关键字结束)

voidis_number(char*p)

{

FILE*fp;

fp=fopen("constant.txt","a");

fprintf(fp,"%s\n",p);

fclose(fp);

fp=fopen("output.txt","a");

fprintf(fp,"%s\n",p);

fclose(fp);

}

/////////////////////////////////////////////////////判断是否为常量

intis_limit(char*ch)

{

inti;

for(i=0;i<41;i++)

{

if(ch==limit[i])

return1;

}

return-1;

}

voidis_error(charerror,intlineno)

{

printf("\nerror:

%c,line%d",error,lineno);

}

///////////////////////////////////////////////////////报告出错符号和所在行数

char*Key[]={"auto","break","case","char","const","continue","default",

"do","double","else","enum","extern","float","for","goto","if",

"int","main","long","printf","register","return","short","signed","sizeof","static",

"struct","switch","typedef","union","unsigned","void","volatile","while"};

//////////////////////////////////////////////////////(判断是否为关键字开始)

intis_id_key(char*p)

{

inti;

for(i=0;i<34;i++)

{

if(p==Key[i])

returni;

}

return-1;

}

intmain()

{

inti;

init();

i=scan();

if(i==1)

printf("Theanswerisin'output.txt':

");

scanf("%d",&i);

getchar();

}

voidinit()

{

FILE*fp;

fp=fopen("id.txt","w");

fclose(fp);//初始化标识符表

fp=fopen("constant.txt","w");

fclose(fp);//初始化常数表

fp=fopen("output.txt","w");

fclose(fp);//初始化输出文件

}

intscan()

{

FILE*fpin,*fpout;

charfilename[20];

charch;

inti=0,line=1;

intcount,result,errorno=0;

chararray[30];

char*word;

printf("\nInputthefilename:

");//输入要编译文件的名字和路径

scanf("%s",filename);

if((fpin=fopen(filename,"r"))==NULL)

{

printf("thefileyouinputisnotexist!

");

getchar();

return0;

}

ch=fgetc(fpin);

while(ch!

=EOF)

{//按字符依次扫描源程序,直至结束

i=0;

if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_'))

{//以字母开头

while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9')))

{

array[i++]=ch;

ch=fgetc(fpin);

}

word=(char*)malloc((i+1)*sizeof(char));

memcpy(word,array,i);

word[i]='\0';

result=is_id_key(word);

if(result==-1)

{

fpout=fopen("id.txt","a");

fprintf(fpout,"%s\n",word);

fclose(fpout);

}

fpout=fopen("output.txt","a");

fprintf(fpout,"%s\n",word);

fclose(fpout);

if(ch!

=EOF)

fseek(fpin,-1L,SEEK_CUR);

}

elseif(ch>='0'&&ch<='9')

{//以数字开头

while(ch>='0'&&ch<='9')

{

array[i++]=ch;

ch=fgetc(fpin);

}

word=(char*)malloc((i+1)*sizeof(char));

memcpy(word,array,i);

word[i]='\0';

is_number(word);

if(ch!

=EOF)

fseek(fpin,-1L,SEEK_CUR);

}

elseif((ch=='')||(ch=='\t'))

;//消除空格符和水平制表符

elseif(ch=='\n')

line++;//消除回车并记录行数

elseif(ch=='/')

{//消除注释

ch=fgetc(fpin);

if(ch=='=')

{//判断是否为‘/=’符号

fpout=fopen("output.txt","a");

fprintf(fpout,"/=\t\t运算符\n");

fclose(fpout);

}

elseif(ch!

='*')

{//若为除号,写入输出文件

fpout=fopen("output.txt","a");

fprintf(fpout,"/\t\t运算符\n");

fclose(fpout);

fseek(fpin,-1L,SEEK_CUR);

}

elseif(ch=='*')

{//若为注释的开始,消除包含在里面的所有字符

count=0;

ch=fgetc(fpin);

while(count!

=2)

{//当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束

count=0;

while(ch!

='*')

ch=fgetc(fpin);

count++;

ch=fgetc(fpin);

if(ch=='/')

count++;

else

ch=fgetc(fpin);

}

}

}

elseif(ch=='"')

{//消除包含在双引号中的字符串常量

fpout=fopen("output.txt","a");

fprintf(fpout,"%c\t\t界符\n",ch);

ch=fgetc(fpin);

while(ch!

='"')

ch=fgetc(fpin);

fprintf(fpout,"%c\t\t界符\n",ch);

fclose(fpout);

}

else

{//首字符为其它字符,即运算限界符或非法字符

array[0]=ch;

ch=fgetc(fpin);array[1]=ch;//再读入下一个字符,判断是否为双字符运算、限界符

if(ch!

=EOF)

{//若该字符非文件结束符

word=(char*)malloc(3*sizeof(char));

memcpy(word,array,2);

word[2]='\0';

result=is_limit(word);

//先检索是否为双字符运算、限界符

if(result==-1)

{//若不是

word=(char*)malloc(2*sizeof(char));

memcpy(word,array,1);

word[1]='\0';

result=is_limit(word);

//result1=is_operator(word);//检索是否为单字符运算、限界符

if(result!

=-1)

{//若还不是,则为非法字符

is_error(array[0],line);

errorno++;

fseek(fpin,-1L,SEEK_CUR);

}

else

{//若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符

fpout=fopen("output.txt","a");

fprintf(fpout,"%s\n",word);

fclose(fpout);

fseek(fpin,-1L,SEEK_CUR);

}

}

else

{//若为双字符运算、限界符,写输出文件

fpout=fopen("output.txt","a");

fprintf(fpout,"%s\n",word);

fclose(fpout);

}

}

else

{//若读入的下一个字符为文件结束符

word=(char*)malloc(2*sizeof(char));

memcpy(word,array,1);

word[1]='\0';

result=is_limit(word);

//result1=is_operator(word);//只考虑是否为单字符运算、限界符

if(result!

=-1)//若不是,转出错处理

is_error(array[0],line);

else

{//若是,写输出文件

fpout=fopen("output.txt","a");

fprintf(fpout,"%s\n",word);

fclose(fpout);

}

}

}

ch=fgetc(fpin);

}

fclose(fpin);

printf("\nThereare%derror(s).\n",errorno);//报告错误字符个数

return1;

}

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

当前位置:首页 > 小学教育 > 语文

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

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