编译原理课程设计-词法分析器(附含源代码)精选._第1页
编译原理课程设计-词法分析器(附含源代码)精选._第2页
编译原理课程设计-词法分析器(附含源代码)精选._第3页
编译原理课程设计-词法分析器(附含源代码)精选._第4页
编译原理课程设计-词法分析器(附含源代码)精选._第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、编译原理 -词法分析器的设计一 设计说明及设计要求 一般来说,编译程序的整个过程可以划分为五个阶段: 词法分析、语法分析、 中间代码生成、 优化和目标代码生成。 本课程设计即为词法分析阶段。 词法分析 阶段是编译过程的第一个阶段。 这个阶段的任务是从左到右一个字符一个字符地 读入源程序,对构成源程序的字符流进行扫描和分解, 从而识别出一个个单词 (也 称单词符号或符号) 。如保留字(关键字或基本字) 、标志符、常数、算符和界符 等等。二 设计中相关关键字说明1. 基本字:也称关键字,女口 C语言中的if , else , while , do ,for,case,break,return 等。

2、2. 标志符:用来表示各种名字,如常量名、变量名和过程名等。3. 常数:各种类型的常数,如 12, 6.88,和“ ABC ”等。4. 运算符:如 + , - , * , / ,%, < , > ,<= , >= 等。5. 界符,如逗点,冒号,分号,括号, # , , 等。三 、程序分析词法分析是编译的第一个阶段, 它的主要任务是从左到右逐个字符地对源 程序进行扫描,产生一个个单词序列, 用以语法分析。 词法分析工作可以是独立的 一遍,把字符流的源程序变为单词序列, 输出在一个中间文件上, 这个文 件做为语法分析程序的输入而继续编译过程。 然而,更一般的情况, 常将 词

3、法分析程序设计成一个子程序, 每当语法分析程序需要一个单词时, 则 调用该子程序。 词法分析程序每得到一次调用, 便从源程序文件中读入一 些字符,直到识别出一个单词,或说直到下一个单词的第一个字符为止。四、模块设计 下面是程序的流程图匚主函数)91谬取文件扫摘一令字待H-取单词逅回UI用输出五、程序介绍在程序当前目录里建立一个文本文档,取名为infile.txt,所有需要分析的程序都写 在此文本文档里,程序的结尾必须以“ ”标志符结束。程序结果输出在同一个 目录下,文件名为outfile.txt,此文件为自动生成。本程序所输出的单词符号采 用以下二元式表示:(单词种别,单词自身的值)如程序输出

4、结果 (57,"#")(33,"i nclude")(52,"v")(33,"iostream")等。程序的功能:(1) 能识别C语言中所有关键字(共32个)(单词种别分别为1 32,详情见程序代码相关部分,下同)(2)能识别C语言中自定义的标示符(单词种别为33)(3) 能识别C语言中的常数(单词种别为0)(4) 能识别C语言中几乎所有运算符(单词种别分别为41 54)(5)能识别C语言中绝大多数界符(单词种别分别为55 66)六、运行结果输入文件infile.txt运行结果(输出文件 outfile.txt)七

5、、设计体会八、附录部分(程序代码)单词符号类别编码单词符号类别编码单词符号类别编码单词符号类别编码if3float21+31#62the n4short22-32.63else5un sig ned23*33J64while6con ti nue24/34:65do7for25<35>=39begi n8sig ned26>36<=38end9void27=37=41long10default2851!=42switch11goto29(52%40case12sizeof30)53标识符1enum13volatile43J54常数2register14auto4455t

6、ypedef15double4556char16int4657exter n17struct4758return18break48<<59union19static49>>60con st20a61提示:文件的打开和读写函数:FILE *fp,*out;/定义文件指针fp=fope n("i nfile.txt","r")如果打开文件"infile.txt"失败,则函数返回NULL,即fp=NULL,第二个参数“ r”表示以只读方式打开,如果为” W;则以可写方式打开调用 fgetc(fp) 这个函数一次从 f

7、p 所指向的文件读取一个字符 char ch=fgetc(fp);想文件写字符的函数为 fprintf(FILE * fp, 写进的内容 ) 比如下面的调用 fprintf(outfile,"abcdn") 是把字符串 “abcd ”写到文件 outfile 的 末尾,并且在后面加上了一个换行标志 文件读写完成后要用函数 fclose(fp) 关闭。源代码#include "stdio.h"#include "string.h"#include "ctype.h" void analzid(FILE *output

8、,char *p)int i=0;int count=0;if (isalpha(p0)if (strcmp(p,"if")=0) fprintf(output,"(3,if)n"); else if(strcmp(p,"then")=0) fprintf(output,"(4,then)n"); else if(strcmp(p,"else")=0) fprintf(output,"(5,else)n"); else if(strcmp(p,"while"

9、;)=0) fprintf(output,"(6,while)n"); else if(strcmp(p,"do")=0) fprintf(output,"(7,do)n"); else if(strcmp(p,"begin")=0) fprintf(output,"(8,begin)n"); else if(strcmp(p,"end")=0) fprintf(output,"(9,end)n"); else if(strcmp(p,"long

10、")=0) fprintf(output,"(10,long)n"); else if(strcmp(p,"switch")=0) fprintf(output,"(11,switch)n"); else if(strcmp(p,"case")=0) fprintf(output,"(12,case)n"); else if(strcmp(p,"enum")=0) fprintf(output,"(13,enum)n"); else if(str

11、cmp(p,"register")=0) fprintf(output,"(14,register)n"); else if(strcmp(p,"typedef")=0) fprintf(output,"(15,typedef)n"); else if(strcmp(p,"char")=0) fprintf(output,"(16,char)n"); else if(strcmp(p,"extern")=0) fprintf(output,"(1

12、7,extern)n"); else if(strcmp(p,"return")=0) fprintf(output,"(18,return)n"); else if(strcmp(p,"union")=0) fprintf(output,"(19,union)n"); else if(strcmp(p,"const")=0) fprintf(output,"(20,const)n"); else if(strcmp(p,"float")=0)

13、fprintf(output,"(21,float)n"); else if(strcmp(p,"short")=0) fprintf(output,"(22,short)n"); else if(strcmp(p,"unsigned")=0) fprintf(output,"(23,unsigned)n"); else if(strcmp(p,"continue")=0) fprintf(output,"(24,continue)n"); else if

14、(strcmp(p,"for")=0) fprintf(output,"(25,for)n"); else if(strcmp(p,"signed")=0) fprintf(output,"(26,signed)n"); else if(strcmp(p,"void")=0) fprintf(output,"(27,void)n"); else if(strcmp(p,"default")=0) fprintf(output,"(28,defau

15、lt)n"); else if(strcmp(p,"goto")=0) fprintf(output,"(29,goto)n"); else if(strcmp(p,"sizeof")=0) fprintf(output,"(30,sizeof)n"); else if(strcmp(p,"volatile")=0) fprintf(output,"(43,volatile)n"); else if(strcmp(p,"auto")=0) fpr

16、intf(output,"(44,auto)n"); else if(strcmp(p,"double")=0) fprintf(output,"(45,double)n"); else if(strcmp(p,"int")=0) fprintf(output,"(46,int)n"); else if(strcmp(p,"struct")=0) fprintf(output,"(47,struct)n"); else if(strcmp(p,"

17、break")=0) fprintf(output,"(48,break)n"); else if(strcmp(p,"static")=0) fprintf(output,"(49,static)n"); else fprintf(output,"(1,%s)n",p);elsefor(;i<(int)strlen(p);i+) if(isdigit(pi) count+;if (count=(int)strlen(p) fprintf(output,"(2,%s)n",p);

18、 elseif (p0='_'&&(isalpha(p1) fprintf(output,"(1,%s)n",p); else fprintf(output,"%s 未定义 n",p);void analzsy(FILE *outfile,char *p)if (strcmp(p,"=")=0) fprintf(outfile,"(37,=)n"); else if(strcmp(p,"+")=0) fprintf(outfile,"(31,+)n&qu

19、ot;); else if(strcmp(p,"-")=0) fprintf(outfile,"(32,-)n"); else if(strcmp(p,"*")=0) fprintf(outfile,"(33,*)n"); else if(strcmp(p,"/")=0) fprintf(outfile,"(34,/)n"); else if(strcmp(p,"(")=0) fprintf(outfile,"(52,()n"); el

20、se if(strcmp(p,")")=0) fprintf(outfile,"(53,)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(55,)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(56,)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(57,)n"); else if(strcmp(p,

21、"")=0) fprintf(outfile,"(58,)n"); else if(strcmp(p,"<<")=0) fprintf(outfile,"(59,<<)n"); else if(strcmp(p,">>")=0) fprintf(outfile,"(60,>>)n"); else if(strcmp(p,"'")=0) fprintf(outfile,"(61,')n

22、"); else if(strcmp(p,"#")=0) fprintf(outfile,"(62,#)n"); else if(strcmp(p,".")=0) fprintf(outfile,"(64,.)n"); else if(strcmp(p,"*")=0) fprintf(outfile,"(33,*)n"); else if(strcmp(p,"/")=0) fprintf(outfile,"(34,/)n");

23、 else if(strcmp(p,"%")=0) fprintf(outfile,"(40,%)n"); else if(strcmp(p,",")=0) fprintf(outfile,"(64,)n"); else if(strcmp(p,":")=0) fprintf(outfile,"(65,:)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(54,;)n"); else if(s

24、trcmp(p,">")=0) fprintf(outfile,"(36,>)n"); else if(strcmp(p,"<")=0) fprintf(outfile,"(35,<)n"); else if(strcmp(p,">=")=0) fprintf(outfile,"(39,>=)n"); else if(strcmp(p,"<=")=0) fprintf(outfile,"(38,<=

25、)n"); else if(strcmp(p,"=")=0) fprintf(outfile,"(41,=)n"); else if(strcmp(p,"!=")=0) fprintf(outfile,"(42,!=)n"); else if(strcmp(p," ")=0) ;else if(strcmp(p,"n")=0) ;else fprintf(outfile,"%s 未定义 n",p);void main()FILE *fp,*out

26、;int i=0,x=0,y=0;int EA=0;char ch,str10000,idstr10,systr2; if(fp=fopen("infile.txt","r")=NULL)printf("Can not open infile!n"); exit(0);if(out=fopen("outfile.txt","w")=NULL)printf("Can not open outfile!n"); exit(0); ch=fgetc(fp);while(ch!=EO

27、F) stri=ch;stri+1='0'i+;ch=fgetc(fp); i=0;while(1) if(stri='') break;else if(stri>='a'&&stri<='z')|(stri>='A'&&stri<='Z')| (stri>='0'&&stri<='9')|(stri='_') idstrx=stri;idstrx+1='0'x+;i+;EA=1; else x=0;if(strlen(idstr)!=0)&&(EA) analzid

温馨提示

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

评论

0/150

提交评论