资源描述:
《编译原理-实验报告.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、程序清单1.分工:2.词法分析:#include#include#include#definekeywordSum8char*keyword[keywordSum]={"if","else","for","while","do","int","read","write"};charsingleword[50]="+-*(){};,:";chardoubleword[10]="><=!";externcharScanin[300],Scanout[300];externFILE*fin,*fo
2、ut;intTESTscan(){charch,token[40];intes=0,j,n;printf("请输入源程序文件名(包括路径):");scanf("%s",Scanin);printf("请输入词法分析输出文件名(包括路径):");scanf("%s",Scanout);if((fin=fopen(Scanin,"r"))==NULL){printf("打开词法分析输入文件出错!");return(1);}if((fout=fopen(Scanout,"w"))==NULL){printf("创建词法分析输出文件出错!
3、");return(2);}ch=getc(fin);while(ch!=EOF){while(ch==''
4、
5、ch==''
6、
7、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]=' ';n=0;while((n=keywordSum)fprintf(fo
8、ut,"%st%s","ID",token);elsefprintf(fout,"%st%s",token,token);}elseif(isdigit(ch)){token[0]=ch;j=1;ch=getc(fin);while(isdigit(ch)){token[j++]=ch;ch=getc(fin);}token[j]=' ';fprintf(fout,"%st%s","NUM",token);}elseif(strchr(singleword,ch)>0){token[0]=ch;token[1]=' ';ch
9、=getc(fin);fprintf(fout,"%st%s",token,token);}elseif(strchr(doubleword,ch)>0){token[0]=ch;ch=getc(fin);if(token[0]=='!'){if(ch=='='){token[1]=ch;token[2]=' ';ch=getc(fin);fprintf(fout,"%st%s",token,token);}else{token[1]=' ';es=1;fprintf(fout,"%st%s","ERROR",token);
10、}}else{if(ch=='='){token[1]=ch;token[2]=' ';ch=getc(fin);}elsetoken[1]=' ';fprintf(fout,"%st%s",token,token);}}elseif(ch=='/'){ch=getc(fin);if(ch=='*'){charch1;ch1=getc(fin);do{ch=ch1;ch1=getc(fin);}while((ch!='*'
11、
12、ch1!='/')&&ch1!=EOF);ch=getc(fin);}elseif(ch=='/'){do{ch
13、=getc(fin);}while(ch!=''&&ch!=EOF);ch=getc(fin);}else{token[0]='/';token[1]=' ';fprintf(fout,"%st%s",token,token);}}else{token[0]=ch;token[1]=' ';ch=getc(fin);es=3;fprintf(fout,"%st%s","ERROR",token);}}fclose(fin);fclose(fout);return(es);}1.语法分析:#include#in
14、clude#include#includeintTESTparse();intp