下载

0下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 词法分析器文档

词法分析器文档.doc

词法分析器文档

万能青年zhang
2019-05-21 0人阅读 举报 0 0 0 暂无简介

简介:本文档为《词法分析器文档doc》,可适用于IT/计算机领域

词法分析程序文档说明:该词法分析器实现对C语言源代码进行词法分析的功能由文件输入将输入的源代码输出为token序列用二元组形式由文件和屏幕输出文件输出首先输入源文件路径和文件名称将注释过滤能够报告不合法字符并且记录源代码的行数单词数(包括关键字标识符和常量)和字符数(包括字母数字标点符号运算符空格和换行符)。程序设计:具体见源代码源代码:#include<stdioh>#include<string>#include<iostream>#include<cstdlib>usingnamespacestd#define MAXcharch=''intline=,zifu=,word=*line记录行数zifu记录字符数(包括字母数字标点符号运算符空格和换行符)word记录单词数(包括关键字标识符和常量)*stringkey={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"}c语言中的所有关键字intIskey(stringc)     关键字判断{intifor(i=i<MAXi){if(keyicompare(c)==){将字符串与关键字一一进行比较return}}return}intIsLetter(charc)     判断是否为字母{if(((c<='z')(c>='a'))||((c<='Z')(c>='A'))){return}elsereturn}intIsDigit(charc)     判断是否为数字{if(c>=''c<=''){return}elsereturn}voidanalyse(FILE*fpin,FILE*fpout){stringarr=""stringstr=""while((ch=fgetc(fpin))!=EOF){ zifu将文件中的字符逐一进行扫描包括字母数字标点符号运算符空格和换行符arr=""if(IsLetter(ch))对开头是字母或数字的字符串进行判断{while(IsLetter(ch)||IsDigit(ch)||ch=='')符合标识符或者关键字标准的字符串进行判断{if((ch<='Z')(ch>='A'))ch=charr=arrch将每个单个读取的字符连成字符串ch=fgetc(fpin)}fseek(fpin,L,SEEKCUR)不满足条件的多读取的字符回退一格if(Iskey(arr))判断字符串是否是关键字{fprintf(fpout,"(s s)n",arrcstr(),"key")printf("(s s)n",arrcstr(),"key")word}else字符串不是关键字则判定为标识符{fprintf(fpout,"(s s)n",arrcstr(),"id")printf("(s s)n",arrcstr(),"id")word}}elseif(IsDigit(ch))开头为数字的串即为常量{while(IsDigit(ch)||ch==''IsDigit(fgetc(fpin))){arr=arrchch=fgetc(fpin)}fseek(fpin,L,SEEKCUR)fprintf(fpout,"(s s)n",arrcstr(),"num")printf("(s s)n",arrcstr(),"num")word}elseswitch(ch)既不是数字也不是字母的符号判定{case'':{ch=fgetc(fpin)判定=if(ch==''){fprintf(fpout,"(s s)n","","")printf("(s s)n","","")zifu}if(ch=='='){fprintf(fpout,"(s s)n","=","")printf("(s s)n","=","")zifu}else{fprintf(fpout,"(s s)n","","")printf("(s s)n","","")fseek(fpin,L,SEEKCUR)}}breakcase'':{ch=fgetc(fpin)if(ch=='')判定={fprintf(fpout,"(s s)n","","")printf("(s s)n","","")zifu}if(ch=='='){fprintf(fpout,"(s s)n","=","")printf("(s s)n","=","")zifu}else{fprintf(fpout,"(s s)n","","")printf("(s s)n","","")fseek(fpin,L,SEEKCUR)}}breakcase'*':fprintf(fpout,"(s s)n","*","")printf("(s s)n","*","")breakcase'=':{ch=fgetc(fpin)判定===

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

评分:

/9

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利