您好,欢迎访问三七文档
词法分析实验报告班级:姓名:学号:1.实验要求a)可以识别每个单词符号,并以记号形式输出;b)识别注释c)统计字符,单词,行,列数等d)检查错误并报告位置e)发现错误修正并恢复2.程序自动机设计如手写图片。3.程序源代码//词法分析器by程闻博//以下保留字以及运算符取自《C语言核心技术》;//部分位运算符(==)未列入;//内存定位运算符*算作算数运算符;//char*keyword[34]={auto,break,case,char,const,continue,default,do,double,else,enum,extern,float,for,goto,if,int,long,register,return,short,signed,sizeof,static,struct,switch,typedef,printf,union,unsigned,void,volatile,while,main};//保留字//char*count_op[5]={+,-,*,/,%};//算数运算符//char*assignment_op[11]={=,+=,-=,*=,/=,%=,&=,^=,|=};//赋值运算符//char*self_op[2]={++,--};//char*comparison_op[6]={,=,,=,==,!=};//比较运算符//char*logic_op[3]={&&,||,!};//逻辑运算符//char*bool_op[4]={&,|,^,~};//布尔位运算符//char*getmem_op[5]={&,.,-,[,]};//内存定位运算符//char*else_op[5]={(,),?,:,,};//其他运算符//char*note[3]={//,/*,*/};//注释//char*sentence[3]={;,{,}}//语句分割#includestdio.h#includestdlib.h#includestring.h#includectype.h#includevectorusingnamespacestd;FILE*fp,*fpo;//文件指针charcurr;//读入的字符intline,row;chartemp[1000];inti=0;inttotal_words,total_word;structerr{intline;introw;intmode;};typedefstructerrerr;vectorerrerr_list;structid_save{inttimes;charcontain[1000];structid_save*next;};typedefstructid_save*id_savePtr;id_savePtrid_list;voidid_count(){intk;for(id_savePtrcurr=id_list;curr-next!=NULL;curr=curr-next){intflag=1;for(k=0;temp[k]!='\0';k++){if(curr-contain[k]!=temp[k]){flag=0;break;}}if(flag==1&&curr-contain[k]=='\0'){curr-times++;return;}}id_savePtrcurr2=new(id_save);curr2-times=1;for(k=0;temp[k]!='\0';k++){curr2-contain[k]=temp[k];}curr2-contain[k]='\0';curr2-next=id_list-next;id_list-next=curr2;}voidoutput_id_list(){intj=1;printf(id统计\n);if(id_list-next!=NULL)for(id_savePtrcurr=id_list-next;curr-next!=NULL;curr=curr-next){printf(NO.%d:%-30s%-30d\n,j,curr-contain,curr-times);j++;}}voidbugreport(intreason){errtemp;temp.line=line;temp.row=row;temp.mode=reason;err_list.push_back(temp);}voidoutput_bugreport(){printf(\n共计%d个错误。\n,err_list.size());intj=1;while(!err_list.empty()){errtemp;temp=err_list.back();err_list.pop_back();switch(temp.mode){case1:printf(%d:非法输入。已忽略非法字符之后部分至空格。位置%d行%d列。\n,j,temp.line,temp.row);break;case2:printf(%d:非法字符串。已全部忽略至空格。位置%d行%d列。\n,j,temp.line,temp.row);break;case3:printf(%d:缺失后引号。位置%d行%d列。\n,j,temp.line,temp.row);break;}j++;}}voidoutput(char*s,intmode){switch(mode){case1:printf(%-20s保留字\n,s);fprintf(fpo,%-20s保留字\n,s);break;case2:id_count();printf(%-20sID\n,s);fprintf(fpo,%-20sID\n,s);break;case3:printf(%-20s数字\n,s);fprintf(fpo,%-20s数字\n,s);break;case4:printf(\n%s\n\t\t整行注释内容\n,s);fprintf(fpo,\n%s\n\t\t整行注释内容\n,s);break;case5:printf(\n%s\n\t\t整段注释内容\n,s);fprintf(fpo,\n%s\n\t\t整段注释内容\n,s);break;case6:printf(%-20s符号\n,s);fprintf(fpo,%-20s符号\n,s);break;case7:printf(%-20s单引号字符\n,s);fprintf(fpo,%-20s单引号字符\n,s);break;case8:printf(%-20s双引号内字符串\n,s);fprintf(fpo,%-20s双引号内字符串\n,s);break;}if(mode!=4&&mode!=5)total_words++;}voidjumptoblank(){while(curr!='\n'&&!feof(fp)&&int(curr)!=32){curr=fgetc(fp);row++;}}charalpha_first(){while(isalnum(curr)!=0||curr=='_'){temp[i]=curr;i++;curr=fgetc(fp);row++;}temp[i]='\0';charkeyword[34][10]={auto,break,case,char,const,continue,default,do,double,else,enum,extern,float,for,goto,if,int,long,register,return,short,signed,sizeof,static,struct,switch,typedef,printf,union,unsigned,void,volatile,while,main};//保留字for(intj=0;j34;j++){if(strcmp(keyword[j],temp)==0){//这是一个保留字output(temp,1);returncurr;}}output(temp,2);returncurr;}chardigit_first(){temp[i]=curr;i++;//数字部分if(curr=='0'){curr=fgetc(fp);row++;switch(curr){//十六进制case'x':case'X':{temp[i]=curr;i++;curr=fgetc(fp);row++;if((isdigit(curr)!=0||curr=='a'||curr=='A'||curr=='b'||curr=='B'||curr=='c'||curr=='C'||curr=='d'||curr=='D'||curr=='e'||curr=='E'||curr=='f'||curr=='F'||curr=='.')==0){bugreport(2);jumptoblank();returncurr;}while(isdigit(curr)!=0||curr=='a'||curr=='A'||curr=='b'||curr=='B'||curr=='c'||curr=='C'||curr=='d'||curr=='D'||curr=='e'||curr=='E'||curr=='f'||curr=='F'){temp[i]=curr;i++;curr=fgetc(fp);row++;}if(curr=='.'){temp[i]=curr;i++;curr=fgetc(fp);row++;if((isdigit(curr)!=0||curr=='a'||curr=='A'||curr=='b'||curr=='B'||curr=='c'||curr=='C'||curr=='d'||curr=='D'||curr=='e'||curr=='E'||curr=='f'||curr=='F')==0){bugreport(2);jumptoblank();returncurr;}while(isdigit(curr)!=0||curr=='a'||curr=='A'||curr=='b'||curr=='B'||curr=='c'||curr=='C'||curr=='d'||curr=='D'||curr=='e'||curr=='E'||curr=='f'||curr=='F'){temp[i]=curr;i++;curr=fgetc(fp);row++;}}if(curr=='p'||curr=='P'){temp[i]=curr;i++;curr=fgetc(fp);row++;if(curr=='-'){temp[i]=curr;i++;curr=fgetc(fp);row++;}if(isdigit(curr)==0){bugreport(2);jumptoblank();returncurr;}while(isdigit(curr)!=0){temp[i]=curr;i++;curr=fgetc(fp);row++;}}}break;//八进制case'0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':{temp[i]=curr;i++;curr=fgetc(fp);row++;while(isdigit(curr)!=0&&curr!='8'&&curr!='9'){temp[i]=curr;i++;curr=fgetc(fp);row++;}if(curr=='.'){temp[i]=curr;i++;curr=fgetc(fp);row++;if(isdigit(curr)==0||curr!='8'||curr!='9'){bugreport(2);jumptoblank();returncurr;}while(isdigit(curr)!=0&&curr!
本文标题:词法分析器实验报告
链接地址:https://www.777doc.com/doc-2026526 .html