编译原理-实验报告1-词法分析

上传人:lil****ar 文档编号:281913537 上传时间:2022-04-25 格式:DOC 页数:7 大小:73KB
返回 下载 相关 举报
编译原理-实验报告1-词法分析_第1页
第1页 / 共7页
编译原理-实验报告1-词法分析_第2页
第2页 / 共7页
编译原理-实验报告1-词法分析_第3页
第3页 / 共7页
编译原理-实验报告1-词法分析_第4页
第4页 / 共7页
编译原理-实验报告1-词法分析_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《编译原理-实验报告1-词法分析》由会员分享,可在线阅读,更多相关《编译原理-实验报告1-词法分析(7页珍藏版)》请在金锄头文库上搜索。

1、计算机硬件实验室实验报告姓名学号班 级成 绩 设备名称及软件环境词法分析一、 实验目的:通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。(遇到错误时可显示“Error”,然后跳过错误部分继续显示) 二、实验要求:识别保留字:if、int、for、while、do、return、break、continue,等C语言的保留字;单词种别码为1。

2、其他的都识别为标识符;单词种别码为2。常数为无符号整形数;单词种别码为3。运算符包括:+、-、*、/、=、=、=、!= ;单词种别码为4。分隔符包括:,、;、(、)、; 单词种别码为5。三、 实验过程:根据题目要求,初步完成程序过程的设计,模块的划分、变量类型分配。#inlcludeint main()int d,a,c;a=10;c=a+b;printf(%d%d,a,b);return 0;用以上定义的C语言子集的源程序作为词法分析程序的输入数据。在词法分析中,自文件头开始扫描源程序字符,一旦发现符合“单词”定义的源程序字符串时,将它翻译成固定长度的单词内部表示,并查填适当的信息表。经过词

3、法分析后,源程序字符串(源程序的外部表示)被翻译成具有等长信息的单词串(源程序的内部表示),并产生两个表格:常数表和标识符表,它们分别包含了源程序中的所有常数和所有标识符。0.定义部分:定义常量、变量、数据结构。1.初始化:从文件将源程序全部输入到字符缓冲区中。2.取单词前:去掉多余空白。3.取单词:利用实验一的成果读出单词的每一个字符,组成单词,分析类型。(关键是如何判断取单词结束?取到的单词是什么类型的单词?)4.显示结果。四、 实验结果:(1) 写出示例程序的程序流程图(2) 给出程序的运行结果。(附运行窗体)示例程序:(注意:示例程序需要修改的部分已用蓝色标记,修改之后再调试运行)#i

4、nclude #include #include #include #include #define NULL 0FILE *fp;char cbuffer;char *key32=if,else,for,break,continue,int,float,double,auto,case,char,const,default,do,enum,long,extern,goto,register,return,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile,while;char *borde

5、r8=,;,(,),;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=31;i+) if (strcmp(keyi,searchchar)=0) return(i+1); return 0;case 2:for (i=0;i=7;i+) if (strcmp(borderi,sea

6、rchchar)=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(constsi & (strcmp(constsi,searchchar)=0)return(i+1);constsi-1=(char *)

7、malloc(sizeof(searchchar);strcpy(constsi-1,searchchar);constnum+;return(i);case 6:for(i=0;i=labelnum;i+)if(labeli & (strcmp(labeli,searchchar)=0) return(i+1);labeli-1=(char *)malloc(sizeof(searchchar);strcpy(labeli-1,searchchar);labelnum+;return(i);default: return 0;char alphaprocess(char buffer)/in

8、t 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(%s (1,%d)n,alphatp,atype-1);printf(1, %s)n,alphatp);elsesearch(alphatp,6);/printf(%s (6,%d)n,alphatp,atype-1);printf(2, %s)n,alphatp);return(buf

9、fer);char digitprocess(char buffer)int i=-1;char digittp20;/int dtype;while (isdigit(buffer)digittp+i=buffer;buffer=fgetc(fp);digittpi+1=0;search(digittp,5);/printf(%s (5,%d)n,digittp,dtype-1);printf(3, %s)n,digittp);return(buffer);char otherprocess(char buffer)int i=-1;char othertp20;/int otype,oty

10、petp;othertp0=buffer;othertp1=0;if (/*otype=*/search(othertp,3)/printf(%s (3,%d)n,othertp,otype-1);printf(4, %s)n,othertp);buffer=fgetc(fp);goto out;if (/*otype=*/search(othertp,4)buffer=fgetc(fp);othertp1=buffer;othertp2=0;if (/*otypetp=*/search(othertp,4)/printf(%s (4,%d)n,othertp,otypetp-1);print

11、f(4, %s)n,othertp);goto out;elseothertp1=0;/printf(%s (4,%d)n,othertp,otype-1);printf(4, %s)n,othertp);goto out;if (buffer=:)buffer=fgetc(fp);if (buffer=)printf(:= (2,2)n);buffer=fgetc(fp);goto out;elseif (/*otype=*/search(othertp,2)/ printf(%s (2,%d)n,othertp,otype-1); printf(5, %s)n,othertp); buff

12、er=fgetc(fp); goto out; if (buffer!=n)&(buffer!= )printf(%c error,not a wordn,buffer);buffer=fgetc(fp);out: return(buffer);void main()int i;for (i=0;i=20;i+)labeli=NULL;constsi=NULL;if (fp=fopen(example.c,r)=NULL)printf(error);elsecbuffer = fgetc(fp);while (cbuffer!=EOF)if (isalpha(cbuffer)cbuffer=alphaprocess(cbuffer);else if(isdigit(cbuffer)cbuffe

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

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号