词法分析程序.doc_第1页
词法分析程序.doc_第2页
词法分析程序.doc_第3页
词法分析程序.doc_第4页
词法分析程序.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

* 词法分析程序 2007.12.5*/#include#include#includevoid init();/* 主函数*/main() int i; init(); /*初始化*/ i=scanner(); /*扫描源程序*/ if(i=1) printf(The answer is in output.txt:); getch();/* 初始化函数*/void init() char *key= ,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语言所有关键字,共个*/ char *limit= ,(,),-,.,!,+,-,&, *,/,%,+,-,=,=,!=,&,|, =,+=,-=,*=,/=,;,#,_,;/*共个运算、限界符*/ FILE *fp; int i; char c; fp=fopen(key.txt,w); for(i=1;i=32;i+) fprintf(fp,%sn,keyi); fclose(fp);/*初始化关键字*/ fp=fopen(limit.txt,w); for(i=1;i=38;i+) fprintf(fp,%sn,limiti); c=; fprintf(fp,%cn,c); fclose(fp); /*初始化运算、限界符表*/ fp=fopen(id.txt,w); fclose(fp); /*初始化标识符表*/ fp=fopen(constant.txt,w); fclose(fp); /*初始化常数表*/ fp=fopen(output.txt,w); fclose(fp); /*初始化输出文件*/* 十进制转二进制函数*/char * dtb(char *buf) int temp20; char *binary; int value=0,i=0,j; for(i=0;bufi!=0;i+) value=value*10+(bufi-48); /*先将字符转化为十进制数*/ if(value=0) binary=malloc(2*sizeof(char); binary0=0; binary1=0; return(binary); i=0; while(value!=0) tempi+=value%2; value/=2; tempi=0; binary=malloc(i+1)*sizeof(char); for(j=0;j=A)&(ch=a)&(ch=A)&(ch=a)&(ch=0)&(ch=0&ch=0&ch=9) arrayi+=ch; ch=fgetc(fpin); word=(char *)malloc(i+1)*sizeof(char); memcpy(word,array,i); wordi=0; cs_manage(word); if(ch!=EOF) fseek(fpin,-1L,SEEK_CUR); else if(ch= )|(ch=t) ; /*消除空格符和水平制表符*/ else if(ch=n) line+; /*消除回车并记录行数*/ else if(ch=/) /*消除注释*/ ch=fgetc(fpin); if(ch=) /*判断是否为/=符号*/ fpout=fopen(output.txt,a); fprintf(fpout,/=ttt4ttt32n); fclose(fpout); else if(ch!=*) /*若为除号,写入输出文件*/ fpout=fopen(output.txt,a); fprintf(fpout,/ttt4ttt13n); fclose(fpout); fseek(fpin,-1L,SEEK_CUR); else if(ch=*) /*若为注释的开始,消除包含在里面的所有字符*/ count=0; ch=fgetc(fpin); while(count!=2) /*当扫描到*且紧接着下一个字符为/才是注释的结束*/ count=0; while(ch!=*) ch=fgetc(fpin); count+; ch=fgetc(fpin); if(ch=/) count+; else ch=fgetc(fpin); else if(ch=) /*消除包含在双引号中的字符串常量*/ fpout=fopen(output.txt,a); fprintf(fpout,%cttt4ttt37n,ch); ch=fgetc(fpin); while(ch!=) ch=fgetc(fpin); fprintf(fpout,%cttt4ttt37n,ch); fclose(fpout); else /*首字符为其它字符,即运算限界符或非法字符*/ array0=ch; ch=fgetc(fpin); /*再读入下一个字符,判断是否为双字符运算、限界符*/ if(ch!=EOF) /*若该字符非文件结束符*/ array1=ch; word=(char *)malloc(3*sizeof(char); memcpy(word,array,2); word2=0; result=find(word,4,1); /*先检索是否为双字符运算、限界符*/ if(result=0) /*若不是*/ word=(char *)malloc(2*sizeof(char); memcpy(word,array,1); word1=0; result=find(word,4,1); /*检索是否为单字符运算、限界符*/ if(result=0) /*若还不是,则为非法字符*/ er_manage(array0,line); errorno+; fseek(fpin,-1L,SEEK_CUR); else /*若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符*/ fpout=fopen(output.txt,a); fprintf(fpout,%sttt4ttt%dtn,word,result); fclose(fpout); fseek(fpin,-1L,SEEK_CUR); else /*若为双字符运算、限界符,写输出文件*/ fpout=fopen(output.txt,a); fprintf(fpout,%sttt4ttt%dn,word,result); fclose(fpout); else /*若读入的下一个字符为文件结束符*/ word=(char *)malloc(2*sizeof(char); memcpy(word,array,1); word1=0; result=find(word,4,1); /*只考虑是否为单字符运算、限界符*/ if(result=0) /*若不是,转出错处理*/ er_manage(array0,line); else /*若是,写输出文件*/ fpout=fopen(outpu

温馨提示

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

评论

0/150

提交评论