编译原理实验报告.docx
- 文档编号:3044008
- 上传时间:2022-11-17
- 格式:DOCX
- 页数:20
- 大小:17.81KB
编译原理实验报告.docx
《编译原理实验报告.docx》由会员分享,可在线阅读,更多相关《编译原理实验报告.docx(20页珍藏版)》请在冰豆网上搜索。
编译原理实验报告
程序清单
1.分工:
2.词法分析:
#include
#include
#include
#definekeywordSum8
char*keyword[keywordSum]={"if","else","for","while","do","int","read","write"};
charsingleword[50]="+-*(){};,:
";
chardoubleword[10]="><=!
";
externcharScanin[300],Scanout[300];
externFILE*fin,*fout;
intTESTscan()
{charch,token[40];
intes=0,j,n;
printf("请输入源程序文件名(包括路径):
");
scanf("%s",Scanin);
printf("请输入词法分析输出文件名(包括路径):
");
scanf("%s",Scanout);
if((fin=fopen(Scanin,"r"))==NULL)
{printf("\n打开词法分析输入文件出错!
\n");
return
(1);}
if((fout=fopen(Scanout,"w"))==NULL)
{printf("\n创建词法分析输出文件出错!
\n");
return
(2);}
ch=getc(fin);
while(ch!
=EOF)
{while(ch==''||ch=='\n'||ch=='\t')ch=getc(fin);
if(isalpha(ch))
{token[0]=ch;j=1;
ch=getc(fin);
while(isalnum(ch))
{token[j++]=ch;
ch=getc(fin);}
token[j]='\0';
n=0;
while((n if(n>=keywordSum) fprintf(fout,"%s\t%s\n","ID",token); else fprintf(fout,"%s\t%s\n",token,token); }elseif(isdigit(ch)) { token[0]=ch;j=1; ch=getc(fin); while(isdigit(ch)) {token[j++]=ch; ch=getc(fin);} token[j]='\0'; fprintf(fout,"%s\t%s\n","NUM",token); }elseif(strchr(singleword,ch)>0) { token[0]=ch;token[1]='\0'; ch=getc(fin); fprintf(fout,"%s\t%s\n",token,token); }elseif(strchr(doubleword,ch)>0) { token[0]=ch; ch=getc(fin); if(token[0]=='! ') {if(ch=='=') { token[1]=ch;token[2]='\0'; ch=getc(fin); fprintf(fout,"%s\t%s\n",token,token); } else { token[1]='\0'; es=1; fprintf(fout,"%s\t%s\n","ERROR",token); } } else{ if(ch=='=') { token[1]=ch;token[2]='\0'; ch=getc(fin); }else token[1]='\0'; fprintf(fout,"%s\t%s\n",token,token); } }elseif(ch=='/') { ch=getc(fin); if(ch=='*') { charch1; ch1=getc(fin); do { ch=ch1;ch1=getc(fin); }while((ch! ='*'||ch1! ='/')&&ch1! =EOF); ch=getc(fin); }elseif(ch=='/'){ do{ ch=getc(fin); }while(ch! ='\n'&&ch! =EOF); ch=getc(fin); }else { token[0]='/';token[1]='\0'; fprintf(fout,"%s\t%s\n",token,token); } }else { token[0]=ch;token[1]='\0'; ch=getc(fin); es=3; fprintf(fout,"%s\t%s\n","ERROR",token); } } fclose(fin); fclose(fout); return(es); } 3.语法分析: #include #include #include #include intTESTparse(); intprogram(); intcompound_Stat(); intstatement(); //intexpression_Stat(); intexpression_stat(); intexpression(); intbool_expr(); intadditive_expr(); intterm(); intfactor(); intif_stat(); intwhile_stat(); intfor_stat(); intwrite_stat(); intread_stat(); intdeclaration_stat(); intdeclaration_list(); intstatement_list(); intcompound_stat(); chartoken[20],token1[40];///token保存单词符号,token1保存单词值 externcharScanout[300];///保存词法分析输出文件名 FILE*fp;///用于指向输入文件的指针 #definekeywordSum8 char*keyword[keywordSum]={"if","else","for","while","do","int","read","write"}; charsingleword[50]="+-*(){};,: "; chardoubleword[10]="><=! "; externcharScanin[300],Scanout[300]; externFILE*fin,*fout; ///语法分析程序 intTESTparse() { intes=0; if((fp=fopen(Scanout,"r"))==NULL) { printf("\n打开%s错误! \n",Scanout); es=0; } if(es==0)es=program(); printf("=====语法分析结果! ======\n"); switch(es) { case0: printf("语法分析成功! \n");break; case10: printf("打开文件%s失败! \n");break; case1: printf("缺少{! \n");break; case2: printf("缺少}! \n");break; case3: printf("缺少标识符! \n");break; case4: printf("少分号! \n");break; case5: printf("缺少(! \n");break; case6: printf("缺少)! \n");break; case7: printf("缺少操作数! \n");break; } fclose(fp); return(es); } ///<程序>: : ={<声明序列><语句序列>} intprogram() { intes=0; fscanf(fp,"%s%s\n",token,token1); printf("%s%s\n",token,token1); if(strcmp(token,"{"))///判断是否为'{' { es=1; return(es); } fscanf(fp,"%s%s\n",&token,&token1); printf("%s%s\n",token,token1); es=declaration_list(); if(es>0)return(es); es=statement_list(); if(strcmp(token,"}"))///判断是否为'}' { es=2; return(es); } return(es); } ///<声明序列>: : =<声明序列><声明语句>|空 intdeclaration_list() { intes=0; while(strcmp(token,"int")==0) { es=declaration_stat(); if(es>0)return(es); } return(es); } ///<声明语句>: : =int<变量>; intdeclaration_stat() { intes=0; fscanf(fp,"%s%s\n",&token,&token1); printf("%s%s\n",token,token1); if(strcmp(token,"ID"))return(es=3);///不是标识符 fscanf(fp,"%s%s\n",&token,&token1); printf("%s%s\n",token,token1); if(strcmp(token,";"))return(es=4); fscanf(fp,"%s%s\n",&token,&token1); printf("%s%s\n",token,token1); return(es); } ///<语句序列>: : =<语句序列><语句>|空 intstatement_list() { intes=0; while(strcmp(token,"}")) { es=statement(); if(es>0)return(es); } return(es); } ///<语句>: : = ///
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验 报告