编译原理词法分析器程序

上传人:m**** 文档编号:562294138 上传时间:2022-09-10 格式:DOC 页数:6 大小:48.52KB
返回 下载 相关 举报
编译原理词法分析器程序_第1页
第1页 / 共6页
编译原理词法分析器程序_第2页
第2页 / 共6页
编译原理词法分析器程序_第3页
第3页 / 共6页
编译原理词法分析器程序_第4页
第4页 / 共6页
编译原理词法分析器程序_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《编译原理词法分析器程序》由会员分享,可在线阅读,更多相关《编译原理词法分析器程序(6页珍藏版)》请在金锄头文库上搜索。

1、一. 实验目的1 深入理解有限自动机及其应用2 掌握根据语言的词法规则构造识别其单词的有限自动机的方法3基本掌握词法分析程序的开发。二. 实验内容及要求1、 词法分析器的功能和输出格式词法分析器的功能是输入源程序,输出单词符号。词法分析器的单词符号常常表示成以下的二元式(单词种别码,单词符号的属性值)。本实验中,采用的是一类符号一种别码的方式。2、 单词的BNF表示- -|- - |- +- - - =3、“超前搜索”方法词法分析时,常常会用到超前搜索方法。如当前待分析字符串为“a+”,当前字符为,此时,分析器倒底是将其分析为大于关系运算符还是大于等于关系运算符呢?显然,只有知道下一个字符是什

2、么才能下结论。于是分析器读入下一个字符+,这时可知应将解释为大于运算符。但此时,超前读了一个字符+,所以要回退一个字符,词法分析器才能正常运行。在分析标识符,无符号整数等时也有类似情况。4、模块结构缓冲区扫描一个字符主函数main()N输入文件名,判断能否打开文件Y缓冲区中是否还有字符Y结束取单词扫描一个字符调用返回输出N5.程序思路 这里以开始定义的C语言子集的源程序作为词法分析程序的输入数据。在词法分析中,自文件头开始扫描源程序字符,一旦发现符合“单词”定义的源程序字符串时,将它翻译成固定长度的单词内部表示,并查填适当的信息表。经过词法分析后,源程序字符串(源程序的外部表示)被翻译成具有等

3、长信息的单词串(源程序的内部表示),并产生两个表格:常数表和标识符表,它们分别包含了源程序中的所有常数和所有标识符。0.定义部分:定义常量、变量、数据结构。1.初始化:从文件将源程序全部输入到字符缓冲区中。2.取单词前:去掉多余空白。3.取单词后:去掉多余空白(可选,看着办)。4.取单词:利用实验一的成果读出单词的每一个字符,组成单词,分析类型。(关键是如何判断取单词结束?取到的单词是什么类型的单词?)5.显示结果。6.实验程序的源代码如下:#include #include #include #include #include #define NULL 0FILE *fp;char cbuf

4、fer;char *key8=if,else,for,while,do,return,break,continue;char *border6=,;,(,);char *arithmetic4=+,-,*,/;char *relation6=,=,;char *consts20=0,1,2,3,4,5,6,7,8,9;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 (strc

5、mp(keyi,searchchar)=0) return(i+1); break; case 2:for (i=0;i=5;i+) if (strcmp(borderi,searchchar)=0) return(i+1); return(0); break; case 3:for (i=0;i=3;i+) if (strcmp(arithmetici,searchchar)=0) return(i+1); return(0); break; case 4:for (i=0;i=5;i+) if (strcmp(relationi,searchchar)=0) return(i+1); re

6、turn(0); break; case 5: for (i=0;i=constnum;i+) if (strcmp(constsi,searchchar)=0) return(i+1); break; case 6:for (i=0;i=labelnum;i+) if (strcmp(labeli,searchchar)=0) return(i+1); labeli-1=(char *)malloc(sizeof(searchchar); strcpy(labeli-1,searchchar); labelnum+; return(i); break; char alphaprocess(c

7、har 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(%s t(1,%d)n,alphatp,atype-1);/结束 else atype=search(alphatp,6); printf(%s t(6,%d)n,alphatp,atype-1); return(buffer);char digitp

8、rocess(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(%s t(5,%d)n,digittp,dtype-1); return(buffer);char otherprocess(char buffer) int i=-1; char othertp20; int otype,otypetp; othertp0=buffer;

9、othertp1=0; if (otype=search(othertp,3) printf(%s t(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) printf(%s t(4,%d)n,othertp,otypetp-1); goto out; else othertp1=0; printf(%s t(4,%d)n,other

10、tp,otype-1); goto out; if (buffer=:) buffer=fgetc(fp); if (buffer=) printf(:= (2,2)n); buffer=fgetc(fp); goto out; else if (otype=search(othertp,2) printf(%s t(2,%d)n,othertp,otype-1); buffer=fgetc(fp); goto out; if (buffer!=n)&(buffer!= ) printf(%c error,not a wordn,buffer); buffer=fgetc(fp);out: r

11、eturn(buffer);void main() int i; for (i=0;i=20;i+) labeli=NULL; ; if (fp=fopen(example.txt,r)=NULL) printf(error); elsecbuffer = fgetc(fp);while (cbuffer!=EOF) if (isalpha(cbuffer) cbuffer=alphaprocess(cbuffer); else if (isdigit(cbuffer) cbuffer=digitprocess(cbuffer); else cbuffer=otherprocess(cbuffer); printf(overn); getchar(); 7运行结果如下:三. 实验总结(心得)通过本实验的学习,了解了基本此法分析的过程及方法,巩固了我对书本知识了解,拓宽了知识面

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

当前位置:首页 > 建筑/环境 > 综合/其它

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