收藏 分销(赏)

编译原理专业课程设计词法分析器附含源代码.doc

上传人:w****g 文档编号:2682959 上传时间:2024-06-04 格式:DOC 页数:10 大小:86.04KB
下载 相关 举报
编译原理专业课程设计词法分析器附含源代码.doc_第1页
第1页 / 共10页
编译原理专业课程设计词法分析器附含源代码.doc_第2页
第2页 / 共10页
编译原理专业课程设计词法分析器附含源代码.doc_第3页
第3页 / 共10页
编译原理专业课程设计词法分析器附含源代码.doc_第4页
第4页 / 共10页
编译原理专业课程设计词法分析器附含源代码.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

1、编译原理-词法分析器设计 一 设计阐明及设计规定普通来说,编译程序整个过程可以划分为五个阶段:词法分析、语法分析、中间代码生成、优化和目的代码生成。本课程设计即为词法分析阶段。词法分析阶段是编译过程第一种阶段。这个阶段任务是从左到右一种字符一种字符地读入源程序,对构成源程序字符流进行扫描和分解,从而辨认出一种个单词(也称单词符号或符号)。如保存字(核心字或基本字)、标志符、常数、算符和界符等等。二 设计中有关核心字阐明1 基本字:也称核心字,如C语言中 if ,else ,while ,do ,for,case,break,return 等。2 标志符:用来表达各种名字,如常量名、变量名和过程

2、名等。3 常数:各种类型常数,如12,6.88,和“ABC”等。4 运算符:如 + ,- ,* ,/ ,%, ,= 等。5 界符,如逗点,冒号,分号,括号,# , , 等。三 、程序分析词法分析是编译第一种阶段,它重要任务是从左到右逐个字符地对源程序进行扫描,产生一种个单词序列,用以语法分析。词法分析工作可以是独立一遍,把字符流源程序变为单词序列,输出在一种中间文献上,这个文献做为语法分析程序输入而继续编译过程。然而,更普通状况,常将词法分析程序设计成一种子程序,每当语法分析程序需要一种单词时,则调用该子程序。词法分析程序每得到一次调用,便从源程序文献中读入某些字符,直到辨认出一种单词,或说直

3、到下一种单词第一种字符为止。四 、模块设计下面是程序流程图五、程序简介在程序当前目录里建立一种文本文档,取名为infile.txt,所有需要分析程序都写在此文本文档里,程序结尾必要以“”标志符结束。程序成果输出在同一种目录下,文献名为outfile.txt,此文献为自动生成。本程序所输出单词符号采用如下二元式表达:(单词种别,单词自身值)如程序输出成果 (57,#)(33,include)(52,)(33,iostream) 等。程序功能:(1) 能辨认C语言中所有核心字(共32个)(单词种别分别为1 32 ,详情见程序代码有关某些,下同)(2) 能辨认C语言中自定义标示符 (单词种别为 33

4、)(3) 能辨认C语言中常数 (单词种别为0)(4) 能辨认C语言中几乎所有运算符(单词种别分别为41 54)(5) 能辨认C语言中绝大多数界符 (单词种别分别为 55 66)六 、运营成果输入文献infile.txt运营成果(输出文献 outfile.txt)七、设计体会八、附录某些(程序代码)单词符号类别编码单词符号类别编码单词符号类别编码单词符号类别编码if3float21+31#62then4short22-32.63else5unsigned23*33,64while6continue24/34:65do7for25=39begin8signed2636=38end9void27=3

5、7=41long10default28:=51!=42switch11goto29(52%40case12sizeof30)53标记符1enum13volatile43;54常数2register14auto4455typedef15double4556char16int 4657extern17struct4758return18break4860const20“61提示:文献打开和读写函数:FILE *fp,*out; /定义文献指针fp=fopen(infile.txt,r)如果打开文献infile.txt失败,则函数返回NULL,即fp=NULL,第二个参数“r”表达以只读方式打开,如

6、果为”w”, 则以可写方式打开调用fgetc(fp)这个函数一次从fp所指向文献读取一种字符char ch=fgetc(fp);想文献写字符函数为fprintf(FILE * fp,写进内容)例如下面调用fprintf(outfile,abcdn)是把字符串“abcd”写到文献outfile末尾,并且在背面加上了一种换行标志文献读写完毕后要用函数fclose(fp)关闭。源代码#include stdio.h#include string.h#include ctype.hvoid analzid(FILE *output,char *p) int i=0; int count=0; if (

7、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)=0) fprintf(output,(6,while)n);else if(strcmp(p,do)=0) fprintf(output,(7,do)n);else if(strcmp(p,begin)=0) fprintf(outp

8、ut,(8,begin)n);else if(strcmp(p,end)=0) fprintf(output,(9,end)n);else if(strcmp(p,long)=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(strcmp(p,re

9、gister)=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,(17,extern)n);else if(strcmp(p,return)=0) fprintf(output,(18,return)n);else if(strcmp(p,union)=0) fprin

10、tf(output,(19,union)n);else if(strcmp(p,const)=0) fprintf(output,(20,const)n);else if(strcmp(p,float)=0) 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,co

11、ntinue)n);else if(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,default)n);else if(strcmp(p,goto)=0) fprintf(output,(29,goto)n);else if(strcmp(p,

12、sizeof)=0) fprintf(output,(30,sizeof)n);else if(strcmp(p,volatile)=0) fprintf(output,(43,volatile)n);else if(strcmp(p,auto)=0) fprintf(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(ou

13、tput,(47,struct)n);else if(strcmp(p,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); else for(;i(int)strlen(p);i+) if(isdigit(pi) count+; if (count=(int)strlen(p) fprintf(output,(2,%s)n,p); else if (p0=_&(isalpha(p1) fprin

14、tf(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); else if(strcmp(p,-)=0) fprintf(outfile,(32,-)n); else if(strcmp(p,*)=0) fprintf(outfile,(33,*)n); else if(strcmp(p,/

15、)=0) fprintf(outfile,(34,/)n); else if(strcmp(p,()=0) fprintf(outfile,(52,()n); else 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,)=0) fprintf(ou

16、tfile,(58,)n); else if(strcmp(p,)=0) fprintf(outfile,(59,)=0) fprintf(outfile,(60,)n); else if(strcmp(p,)=0) fprintf(outfile,(61,)n); 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,/)

17、=0) fprintf(outfile,(34,/)n); 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(strcmp(p,)=0) fprintf(outfile,(36,)n); else if(strcmp(p,)=0) fprintf

18、(outfile,(35,=)=0) fprintf(outfile,(39,=)n); else if(strcmp(p,=)=0) fprintf(outfile,(38,=a&stri=A&stri=0&stri=9)|(stri=_) idstrx=stri;idstrx+1=0;x+;i+;EA=1; else x=0; if(strlen(idstr)!=0)&(EA) analzid(out,idstr); idstrx=0; if(stri9&striZ&striz) if(stri!=!&stri!=&stri!=) systry=stri;systry+1=0;analzs

19、y(out,systr);i+; else if(stri=!&stri+1=)|(stri=&stri+1=)| (stri=&stri+1=)|(stri=&stri+1=)| (stri=&stri+1=&stri+1=) systry=stri;systry+1=stri+1;systry+2=0; analzsy(out,systr);i+;i+; else systry=stri;systry+1=0;analzsy(out,systr);i+;printf(-所有成果已经存入outfile.txt文档-n);fprintf(out,-完毕-);fclose(fp);fclose(out);

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服