您好,欢迎访问三七文档
词法分析程序实验实验题目:手动构造词法分析程序实验目的:掌握手动构造词法分析程序的基本思想实验设备(环境):实验基本要求:根据状态转换图利用C语言手工编写一个词法分析程序,对指定的源程序,给出程序的输出结果。实验内容及步骤:一、内容及要求:1.上机调试一个词法分析程序(程序1,见最后)(1)将单词分为标识符、常数、保留字和分隔符。(2)事先建立一保留字表,以备在识别保留字时进行查询。变量名表及常数表则在词法分析过程中建立。(3)所输出的每一单词,均按形如(CLASS,VALUE)的二元式编码。2.词法分析程序的输入程序1的输入一个PASCAL源程序sourcePROGRAMsource;{thislittlesourceprogramisusedtoillustratecompilingprocedure.}VARx,y,z:integer;a:integer;BEGIN2ab%%#x:=23+5;z:=xDIV-3b;y:=z+18*3;a:=x+(y-2)DIV4;END.3.词法分析程序的输出程序1的输出形如(CLASS,VALUE)的单词串(1,'PROGRAM')(7,'source')(9,';')(2,'VAR')(7,'x')(9,',')(7,'y')(9,',')(7,'z')(9,':')(6,'integer')(9,';')(7,'a')(9,':')(6,'integer')(9,';')(3,'BEGIN')(7,'x')(9,':=')(8,'23')(9,'+')(8,'5')(9,';')(7,'z')(9,':=')(7,'x')(4,'DIV')(9,'-')(9,';')(7,'y')(9,':=')(7,'z')(9,'+')(8,'18')(9,'*')(8,'3')(9,';')(7,'a')(9,':=')(7,'x')(9,'+')(9,'(')(7,'y')(9,'-')(8,'2')(9,')')(4,'DIV')(8,'4')(9,';')(5,'END')(9,'.')4.词法分析程序(程序1)说明附在最后面的词法分析程序(程序1)以“程序1的输入一个PASCAL源程序source”为输入程序,并对该程序进行一些词法检查,最后输出形如(CLASS,VALUE)的单词串作为词法分析程序的结果。二、改写词法分析程序对词法分析程序(程序1)进行改写,使其能对如下的源程序进行词法分析:PROGRAMsource;{thislittlesourceprogramisusedtoillustratecompilingprocedure.}VARx,y,z:integer;a:integer;BEGIN2ab%%#x:=23+5;z:=xDIV-3b;IFxzTHENy:=z+18*3ELSEa:=x+(y-2)DIV4;END.附:程序1一个词法分析程序//example1.cpp:Definestheentrypointfortheconsoleapplication.//#includestdafx.h#includeiostreamusingnamespacestd;#includestdio.h#includectype.h#includestring.h#includestdlib.h#defineID7#defineINT8#defineu9#definee10#definef11char*s[6]={PROGRAM,VAR,BEGIN,DIV,END,integer};charTOKEN[20];intlookup(char*b);voidout(inta,char*b);voidreport_error(intn);voidscanner_example(FILE*fp){staticintline=1;charch;inti=0;intc;inta=0;ch=fgetc(fp);if(isalpha(ch)){TOKEN[0]=ch;ch=fgetc(fp);i=1;while(isalnum(ch)){TOKEN[i]=ch;i++;ch=fgetc(fp);}TOKEN[i]='\0';fseek(fp,-1,1);c=lookup(TOKEN);if(c==0){out(ID,TOKEN);}else{out(c,TOKEN);}}else{if(isdigit(ch)){TOKEN[0]=ch;ch=fgetc(fp);i=1;while(isdigit(ch)||isalpha(ch)){if(isalpha(ch)){a=1;}TOKEN[i]=ch;i++;ch=fgetc(fp);}TOKEN[i]='\0';fseek(fp,-1,1);if(a==1){report_error(line);cout数字开头的标识符:;coutTOKENendl;a=0;}elseout(INT,TOKEN);}else{switch(ch){case'':ch=fgetc(fp);if(ch=='=')out(u,=);elseif(ch=='')out(u,);else{fseek(fp,-1,1);out(u,);}break;case'=':out(u,=);break;case'':ch=fgetc(fp);if(ch=='=')out(u,=);else{fseek(fp,-1,1);out(u,);}break;case',':out(u,,);break;case';':out(u,;);break;case'.':out(u,.);break;case'-':out(u,-);break;case'*':out(u,*);break;case'+':out(u,+);break;case'(':out(u,();break;case')':out(u,));break;case':':ch=fgetc(fp);if(ch=='=')out(u,:=);else{fseek(fp,-1,1);out(u,:);}break;case'{':ch=fgetc(fp);while(ch!='}'){if(ch=='\n')line++;ch=fgetc(fp);}case'':case'\t':break;case'\n':line++;break;default:if(ch!=-1){report_error(line);//非法字符cout非法字符:;TOKEN[0]=ch;TOKEN[1]='\0';coutTOKENendl;}break;}}}}//////////////////////////////////////////////////intlookup(char*b){inti;//intc;for(i=0;i6;i++){if(strcmp(b,s[i])==0){returni+1;////////////////////////////////////wende}}return0;}/////////////////////////////////////////////voidout(inta,char*token){FILE*fp;fp=fopen(end.txt,a);fprintf(fp,(%d,'%s'),a,token);fclose(fp);/*if(a=6){cout(关键字,token)endl;}if(a==7){cout(标识符,token)endl;}if(a==8){cout(整数,token)endl;}if(a==9){cout(专用符号,token)endl;}if(a==10){cout(非法字符,token)endl;}if(a==11){cout(错误符号,token)endl;}*/}//////////////////////////////////////////////////////voidreport_error(intn){cout第n行错误!endl;}intmain(intargc,char*argv[]){FILE*fp;FILE*fq;charch;printf(源程序是:\n);fp=fopen(source.txt,r);fq=fopen(end.txt,w);while(!feof(fp)){ch=fgetc(fp);printf(%c,ch);}printf(\n);printf(词法分析为:\n);fclose(fp);/////////////////////////////////////////fp=fopen(source.txt,r);while(!feof(fp)){scanner_example(fp);}fclose(fp);fclose(fq);system(end.txt);return0;}
本文标题:词法分析试验
链接地址:https://www.777doc.com/doc-7174585 .html