编译原理-实验报告.docx

编译原理-实验报告.docx

ID:59466583

大小:15.74 KB

页数:28页

时间:2020-11-02

编译原理-实验报告.docx_第1页
编译原理-实验报告.docx_第2页
编译原理-实验报告.docx_第3页
编译原理-实验报告.docx_第4页
编译原理-实验报告.docx_第5页
资源描述:

《编译原理-实验报告.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

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。