编译原理实验报告(一)词法分析程序.docx

上传人:w*** 文档编号:63199345 上传时间:2022-11-23 格式:DOCX 页数:13 大小:14.56KB
返回 下载 相关 举报
编译原理实验报告(一)词法分析程序.docx_第1页
第1页 / 共13页
编译原理实验报告(一)词法分析程序.docx_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《编译原理实验报告(一)词法分析程序.docx》由会员分享,可在线阅读,更多相关《编译原理实验报告(一)词法分析程序.docx(13页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、编译原理实验报告(一)词法分析程序 编译原理试验报告(一) 词法分析程序 通过设计编制调试一个详细的词法分析程序,加深对词法分析原理的理解。 并驾驭在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析 方法。 本试验以用户指定的想编译的以C语言编写的文件作为词法分析程序的输入数据。 在进行词法分析中,先自文件头起先以行为单位扫描程序,将该行的字符读入预先设定的一个数组缓冲区中,然后对该数组的字符逐词分割,进行词法分析,将每个词分割成关键字、标识符、常量和运算符四种词种,最终产生四个相对应的表,即关键字表、标识符表、常量表和运算符表,它们以文件的形式进行存储。除此之外,还产生一个编译

2、后的文件,它指定了每个词在四个表中的位置。 下面就词法分析程序中的文件和主要变量进行说明: 文件: cpile.c 主程序文件 key.txt 关键字文件 operation.txt 运算符文件 id.txt 标识符文件 const.txt 常量文件 after_com.txt 编译后产生的文件 主要变量: FILE *sfp,*nfp,*ifp,*kfp,*cfp,*pfp; char ib5020 标识符表(动态生成) char cb5010 常量表(动态生成) char kb4410 关键字表(预先定义好的) char pb365 运算符表(预先定义好的) 主要的子函数名: int nu

3、mber(char s,int i); 数字处理函数 int letter(char s,int i); 字符处理函数 int operation(char s,int i); 运算符处理函数 void seti (char s); 标识符建立函数 void setc (char s); 常量建立函数 void cfile(char s1, char s2,int m); 将词和词所在表中位置写入编译后文件 void error1(char s); 字符处理出错报告 void error2(char s); 标识符处理出错报告 void error3(char s); 运算符处理出错报告 vo

4、id openall(); 打开全部文件 void writeall(); 将四个表写入文件 void closeall(); 关闭全部文件 下面简要分析一下词法分析程序的运行流程: 能否打开所要编译的C语言文件 否 报错 能 推断当前字符是否是文件结束符 是 结束 从源程序中读入一行到数组缓冲区 否 推断当前字符是否是n 是 否 推断当前字符是否是字母 是 转关键字和标识符处理 否 推断当前字符是否是数字 是 转数字处理 否 推断当前字符是否是运算符 是 转运算符处理 否 现有源程序a.c清单如下: #include <stdio.h> int main(int argc, ch

5、ar *argv) char ch; int i; ch=a; ch=ch+32 ; i=ch; printf(“%d id %cn“,i,ch);/*打印*/ return 0; 运行词法分析程序后,显示如下结果: after_com.txt文件: #p-2 includei-0 <p-14 stdio.hi-1 >p-16 intk-2 maini-2 (p-7 intk-2 argci-3 ,p-6 chark-0 *p-9 argvi-4 p-18 p-21 )p-8 p-23 chark-0 chi-5 ;p-13 intk-2 ii-6 ;p-13 chi-5 =p-1

6、5 p-19 ai-7 p-19 ;p-13 chi-5 =p-15 chi-5 +p-10 32c-0 ;p-13 ii-6 =p-15 chi-5 ;p-13 printfk-33 (p-7 “p-1 %p-4 di-8 idi-9 %p-4 ci-10 p-20 ni-11 “p-1 ,p-6 ii-6 ,p-6 chi-5 )p-8 ;p-13 returnk-28 0c-1 ;p-13 p-25 key.txt 关键字文件: 0 char 1 short 2 int 3 unsigned 4 long 5 float 6 double 7 struct 8 union 9 void

7、10 enum 11 signed 12 const 13 volatile 14 typedef 15 auto 16 register 17 static 18 extem 19 break 20 case 21 continue 22 default 23 do 24 else 25 for 26 goto 27 if 28 return 29 switch 30 while 31 sizeof 32 txt 33 printf 34 FILE 35 fopen 36 NULL 37 fclose 38 exit 39 r 40 read 41 close 42 w 43 fprintf

8、 id.txt 标识符文件: 0 include 1 stdio.h 2 main 3 argc 4 argv 5 ch 6 i 7 a 8 d 9 id 10 c 11 n operation.txt 运算符文件: 0 ! 1 “ 2 # 3 $ 4 % 5 6 , 7 ( 8 ) 9 * 10 + 11 - 12 : 13 ; 14 < 15 = 16 > 17 ? 18 19 20 21 22 . 23 24 | 25 26 != 27 >= 28 <= 29 = 30 + 31 - 32 33 /* 34 */ const.txt 常量文件: 0 32 1 0

9、结果完全正确。 词法分析程序如下: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define SIZE 256 #define null 0 int line=0,error=0,mark1=0,mark2=0; char *sname; FILE *sfp,*nfp,*ifp,*kfp,*cfp,*pfp; /*-*/ char ib5020; char cb5010; char kb4410=“char“,“short“,“int

10、“,“unsigned“,“long“,“float“,“double“,“struct“,“union“,“void“, “enum“,“signed“,“const“,“volatile“,“typedef“,“auto“,“register“,“static“,“extem“, “break“,“case“,“continue“,“default“,“do“,“else“,“for“,“goto“, “if“,“return“,“switch“,“while“,“sizeof“,“txt“,“printf“,“FILE“,“fopen“,“NULL“, “fclose“,“exit“,“

11、r“,“read“,“close“,“w“,“fprintf“; char pb365=“!“,“,“#“,“$“,“%“,“,“,“,“(“,“)“,“*“,“+“,“-“,“:“,“;“,“<“,“=“,“>“,“?“,“,“, “,“,“.“,“,“|“,“,“!=“,“>=“,“<=“,“=“,“+“,“-“,“,“/*“,“*/“; /*-定义了四个二元数组存放四个表-*/ int main(int argc, char *argv) char sbuffSIZE; char nbuffSIZE; int i; /*-*/ int number(char s,

12、int i); int letter(char s,int i); int operation(char s,int i); void seti (char s); void setc (char s); void cfile(char s1, char s2,int m); void error2(char s); void error1(char s); void error3(char s); void openall(); void writeall(); void closeall(); /*-*/ if(argc!=2) printf(“Please input the file

13、you want to compile:n“); exit(1); else sname=argv1; sfp=fopen(argv1,“r“); openall(); i=0; while(i<50 ) strcpy(ibi,“ “); strcpy(cbi,“ “); i+; /*-*/ while(fgets(sbuff,SIZE,sfp) i=0; line+; /*以行的方式读取源代码进行编译*/ while(sbuffi!=n) if(isalpha(sbuffi) /* 处理关键字和标识符 */ i=letter(sbuff,i); else if(isdigit(sbuf

14、fi) i=number(sbuff,i); /* 处理数字常量 */ else i=operation(sbuff,i); /* 处理运算符 */ fputc(n,nfp); /*-*/ if(error) printf(“!There are %d errors.“,error); /* 最终错误个数报告 */ else printf(“Success to compile the file.“); writeall(); closeall(); return 0; /*$*/ void openall() /* 打开全部必需的文件 */ kfp=fopen(“key.txt“,“w+“)

15、; /* 打开关键字表 */ pfp=fopen(“operation.txt“,“w+“); /* 打开运算符表 */ cfp=fopen(“const.txt“,“w+“); /* 打开常量文件 */ ifp=fopen(“id.txt“,“w+“); /* 打开标识符文件 */ nfp=fopen(“after_com.txt“,“w+“); /* 打开编译后会产生的文件 */ if(kfp=NULL | cfp=NULL | ifp=NULL | pfp=NULL | nfp=NULL) printf(“Cannot open these files“); /* 打开文件有错,退出程序

16、 */ exit(0); /*-*/ void writeall() int m=0; while (m<36pbm!=“ “) fprintf(pfp,“%d%s%sn“, m,“ “,pbm); m+; m=0; while (m<44kbm!=“ “) fprintf(kfp,“%d%s%sn“, m,“ “,kbm); m+; m=0; while (m<mark2cbm!=“ “) fprintf(cfp,“%d%s%sn“, m,“ “,cbm); m+; m=0; while (m<mark1ibm!=“ “) fprintf(ifp,“%d%s%sn“,

17、 m,“ “,ibm); m+; /*-*/ void closeall() /* 关闭全部文件 */ fclose(kfp); fclose(pfp); fclose(cfp); fclose(ifp); fclose(nfp); /*$*/ int letter(char sbuff,int i) char str10; void cfile(char s1, char s2,int m); void seti (char s); void setc (char s); int m=0,n=0; strn=sbuffi; while(isalnum(sbuffi)|sbuffi=.) st

18、rn+=sbuffi+; strn=0; while (m<44(strcmp(str,kbm)!=0) m+; if (m<44) cfile(str,“k“,m); else m=0; while (m<50(strcmp(str,ibm)!=0) m+; if (m<50) cfile (str,“i“,m); else seti(str); return i; /*-*/ int number(char sbuff,int i) char s2,str10; void cfile(char s1, char s2,int m); void seti (char

19、s); void setc (char s); void error2(char s); void error1(char s); int m=0,n=0;strn=sbuffi;i+;n+; if(sbuffi=.) strn=sbuffi; if (sbuffi=E | sbuffi=e) (sbuffi+1=+ | sbuffi+1=- |isdigit(sbuffi+1) strn=sbuffi;strn+=sbuffi+; while (isdigit(sbuffi) strn+=sbuffi+; /*对可能出现的十进制数的处理*/ strn+=0; while (m<50(s

20、trcmp(str,cbm)!=0) m+; if (isalpha(sbuffi) error2(str); else if (m<50) cfile(str,“c“,m); else setc(str); return i; /*-*/ int operation(char sbuff,int i) void error3(char s); void cfile(char s1, char s2,int m); void setc (char s); int k=0,m=0;char s4; if (sbuffi=/sbuffi+1=*) i=i+2; while (!(sbuffi

21、=*sbuffi+1=/) i+; i=i+2; /*若为注释则跳过*/ else if(sbuffi= ) while(sbuffi= ) i+; else sk=sbuffi; while (k<3) sk+=sbuffi+; while (k>=0) sk=0; m=0; while (m<36(strcmp(s,pbm)!=0) m+; if (m<36) cfile(s,“p“,m); break; else k-;i-; if (k=-1) i+; error3(s); return i; /*-*/ void cfile(char s1, char s2,

22、int m) fprintf(nfp,“%s%s-%dn“,s1,s2,m); return; void seti (char s) void cfile(char s1, char s2,int m); strcpy(ibmark1,s); cfile(s,“i“,mark1); mark1+; return ; void setc (char s) void cfile(char s1, char s2,int m); strcpy(cbmark2,s); cfile(s,“c“,mark2); mark2+; return ; /*-*/ void error1 (char s) err

23、or+; printf(“line %d:%s is an illegal letter.n“,line,s); return ; void error2(char s) error+; printf(“line %d:%s is an illegal word.n“,line,s); return ; void error3(char s) error+; printf(“line %d:%s is an illegal operation.n“,line,s); return ; /*-*/ A编写程序之前应当要有一个整体的规划,然后在考虑细微环节问题,不断地发觉问题,修改程序,使之精益求精。B. 通过本试验,我更加深了对文件操作的理解,能够较好的驾驭文件操作的基本要领。C. 应当培育良好的程序设计风格。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 工作计划

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com