编译实验报告词法分析_第1页
编译实验报告词法分析_第2页
编译实验报告词法分析_第3页
编译实验报告词法分析_第4页
编译实验报告词法分析_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 编译原理实验报告实 验 编 号实验二实 验 名 称词法分析程序的设计与实现学 号:姓 名:提交日期:成 绩:一实验二:词法分析程序的设计与实现【实验目的和要求】 设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解。【实验描述】 通过对PL/0词法分析程序(GETSYM)的分析,并在此基础上按照附录A中给出的PL/0语言的语法描述,编写一个PL/0语言的词法分析程序。此程序应具有如下功能: 输入为字符串(待进行词法分析的源程序),输出为单词串,即由(单词、类别)所组成的二元组序列。有一定检查错误的能力,例如发现2A这类不能作为单词的字符串。【主要变量名说明】char *key8=

2、if,else,for,while,do,return,break,continue; 确定关键字char *border6=,;,(,);确定界符char *arithmetic4=+,-,*,/;确定算术运算符 char *relation6=,=,;确定关系运算符【程序清单】#include #include #include #include #define NULL 0#include using namespace std;FILE *fp;char cbuffer;char *key8=if,else,for,while,do,return,break,continue;char

3、 *border6=,;,(,);char *arithmetic4=+,-,*,/;char *relation6=,=,;char *consts20;char *label20;int constnum=0,labelnum=0;/int search(char searchchar,int wordtype)int i=0;switch (wordtype) case 1:for (i=0;i=7;i+)if (strcmp(keyi,searchchar)=0)return(i+1); return 0; case 2:for (i=0;i=5;i+) if (strcmp(bord

4、eri,searchchar)=0)return(i+1); return(0); case 3:for (i=0;i=3;i+)if (strcmp(arithmetici,searchchar)=0)return(i+1); return(0); case 4:for (i=0;i=5;i+) if(strcmp(relationi,searchchar)=0)return(i+1); return(0); case 5:for (i=0;i=constnum;i+) if(i!=constnum)if (strcmp(constsi,searchchar)=0)return(i+1);

5、constsi-1=(char *)malloc(sizeof(searchchar);strcpy(constsi-1,searchchar);constnum+;return(i);case 6:for (i=0;i=labelnum;i+) if(i!=labelnum)if(strcmp(labeli,searchchar)=0)return(i+1);labeli-1=(char *)malloc(sizeof(searchchar);strcpy(labeli-1,searchchar);labelnum+;return(i);default:return 0;/char alph

6、aprocess(char buffer)int atype; int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer)alphatp+i=buffer; buffer=fgetc(fp); alphatpi+1=0; if (atype=search(alphatp,1)printf(%8s (1,%d)n,alphatp,atype-1); elseatype=search(alphatp,6);printf(%8s (6,%d)n,alphatp,atype-1); return(buffer);/char dig

7、itprocess(char buffer)int i=-1; char digittp20; int dtype; while (isdigit(buffer)digittp+i=buffer; buffer=fgetc(fp); digittpi+1=0; dtype=search(digittp,5); printf(%8s (5,%d)n,digittp,dtype-1); return(buffer);/char otherprocess(char buffer)int i=-1; char othertp20; int otype,otypetp; othertp0=buffer;

8、 othertp1=0; if (otype=search(othertp,3)printf(%8s (3,%d)n,othertp,otype-1);buffer=fgetc(fp);goto out;if(otype=search(othertp,4)buffer=fgetc(fp); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4)buffer=fgetc(fp); elseothertp1=0; printf(%8s (4,%d)n,othertp,otype-1);goto out;if (buffer=:)buff

9、er=fgetc(fp);if (buffer=)printf(%8s (2,2)n,:=);buffer=fgetc(fp);goto out; elseif (otype=search(othertp,2)printf(%8s (2,%d)n,othertp,otype-1);buffer=fgetc(fp);goto out;if (buffer!=n)&(buffer!= )&(buffer!=t)printf(%8c error,not a wordn,buffer); buffer=fgetc(fp);out: return(buffer);/void main()int i;fo

10、r (i=0;i=20;i+)labeli=NULL; constsi=NULL;if (fp=fopen(example.c,r)=NULL)printf(error!n);elsecbuffer = fgetc(fp);while(cbuffer!=EOF)if (isalpha(cbuffer)cbuffer=alphaprocess(cbuffer);else if (isdigit(cbuffer)cbuffer=digitprocess(cbuffer);else cbuffer=otherprocess(cbuffer);printf(over!n);system(pause);/ 源程序over/实验中用到的测试的 example.c的文件内容/#include void mian() int a=10,b=5;if(ab)printf(a is bigger than b);elsedoa=b+a; b=a*b; break;while(a=b)return 0;/图2程序运行结果:图1图4图3【调试情况】程序中,用到了较多的数组和动态数组分配,和较多的循环判断,在动态内存分配时出现了较多的错误,而在关键字和单词的判定中,也出现了 不可预知的错误和警告,通过设置断点和插入特殊变量 标记法,发现问题并予以解决。【设计技巧】由于此实验是测试验证的实验,在生成此词法

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论