C语言词法研究器源代码风君版_第1页
C语言词法研究器源代码风君版_第2页
C语言词法研究器源代码风君版_第3页
C语言词法研究器源代码风君版_第4页
C语言词法研究器源代码风君版_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

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

文档简介

1、#include #include *keyOF#include char,auto,break,case,char,const,continue,default,do,double,else ,enum,extern,float,for,goto,if,int,long,register,return,sho rt,signed,sizeof,static,struct,switch,typedef,_Complex,_Imaginar y,union,unsigned,void,volatile,while 。II 11111。 , 。/* 保留字表 */ char *key1= ,(,)

2、,/* 分隔符表 */char*key2F,+,-,*,/,%,=,=,=,!=,!,&,|,JJJJ * J /u JJJJJ,J 11 JJJJA & 一 ?+ + +一 一 *JJJJ 5JJJ JJJ/* 运算符表 */ int xx035, xx110 , xx231int temp_key3=0,temp_c40=0,temp_c4 仁 0,temp_c42=0,temp_c43=0 void load()初始化函数*/int mm 。for (mm=0。mm=34 mm+)xx0mm=0。for (mm=0 。 mm=9 mm+)xx1mm=0。for (mm=0 。 mm=30

3、 mm+)xx2mm=0。FILE *floading 。if (floadi ng一fope n(keyO.txt,w)一一NULL)printf(Error! Cant create file : key0.txt) return 。fclose (floading) 。/* 建立保留字表文件: key0.txt*/if (floading=fopen(key1.txt,w)=NULL)printf(Error! Cant create file : key1.txt) return 。/* 建立分隔符表文件: key1.txt*/if (floading=fopen(key2.txt,w

4、)=NULL)printf(Error! Cant create file : key2.txt) return 。fclose(floading) 。/* 建立运算符表文件: key2.txt*/if (floading=fopen(key3.txt,w)=NULL)printf(Error! Cant create file : key3.txt) return 。fclose (floading) 。/* 建立标识符表文件: key3.txt*/if (floading=fopen(c40.txt,w)=NULL)printf(Error! Cant create file : c40.

5、txt) return 。fclose (floading) 。/* 建立整数类型常量表文件: c40.txt*/if (floading=fopen(c41.txt,w)=NULL) printf(Error! Cant create file : c41.txt) return 。fclose (floading) 。/* 建立浮点类型常量表文件: c41.txt*/if (floading=fopen(c42.txt,w)=NULL) printf(Error! Cant create file : c42.txt) return 。fclose (floading) 。/* 建立字符类

6、型常量表文件: c42.txt*/if (floading=fopen(c43.txt,w)=NULL)printf(Error! Cant create file : c43.txt) return 。fclose (floading) 。/* 建立字符串类型常量表文件: c43.txt*/if (floading=fopen(defination.txt,w)=NULL) printf(Error! Cant create file : defination.txt) return 。fclose (floading) 。/* 建立注释文件: defination.txt*/if (flo

7、ading=fopen(output.txt,w)=NULL) printf(Error! Cant create file : output.txt) return 。fclose (floading) 。/* 建立内部码文件: output.txt*/if (floading=fopen(temp_key1,w)=NULL) printf(Error! Cant create file : temp_key1) return 。fclose (floading) 。/* 建立保留字临时表文件: temp_key1*/ if (floading=fopen(temp_key3,w)=NULL

8、) printf(Error! Cant create file : temp_key3) return 。 fclose (floading) 。/* 建立标识符临时文件: temp_key3*/ if (floading=fopen(temp_c40,w)=NULL) printf(Error! Cant create file : temp_c40) return 。fclose (floading) 。/* 建立整数类型常量临时文件: temp_c40*/ if (floading=fopen(temp_c41,w)=NULL) printf(Error! Cant create fi

9、le : temp_c41) return 。fclose (floading) 。/* 建立浮点类型常量临时文件: temp_c41*/ if (floading=fopen(temp_c42,w)=NULL) printf(Error! Cant create file : temp_c42) return 。fclose (floading) 。/* 建立字符类型常量临时文件: temp_c42*/ if (floading=fopen(temp_c43,w)=NULL) printf(Error! Cant create file : temp_c43) return 。fclose

10、(floading) 。/* 建立字符串类型常量临时文件: temp_c43*/ /* 保留字及标识符判断函数 */void char_search(char *word)int m,line=0,csi=0 。int value=0 。int value2=0 。char c,cs100 。FILE *foutput,*finput 。for (m=1 。 m=34 m+)if (strcmp(word,key0m)=0)value=1 。break 。if (value=1)if (xx0m=0)foutput=fopen(key0.txt,a)。fprintf(foutput,0t%dt

11、t%sn,m,word) fclose(foutput) 。xx0m=1 。foutput=fopen(output.txt,a) fprintf(foutput,0t%dtt%sn,m,word) fclose(foutput) 。elseif (temp_key3=0) foutput=fopen(temp_key3,a) fprintf(foutput,%sn,word) fclose(foutput) 。 temp_key3+ 。 foutput=fopen(key3.txt,a) fprintf(foutput,3t1tt%sn,word) fclose(foutput) 。finp

12、ut=fopen(temp_key3,r) 。 c=fgetc(finput) 。while (c!=EOF)while (c!=n)cscsi+=c 。 c=fgetc(finput) 。 cscsi=0 。 csi=0 。 line+ 。if (strcmp(cs,word)=0) value2=1 。 break 。else value2=0 。 c=fgetc(finput) 。fclose(finput) 。if (value2=1)foutput=fopen(output.txt,a)fprintf(foutput,3t%dtt%sn,line,word) fclose(foutp

13、ut) 。else foutput=fopen(temp_key3,a) 。 fprintf(foutput,%sn,word) 。 fclose(foutput) 。 temp_key3+ 。 foutput=fopen(output.txt,a) 。 fprintf(foutput,3t%dtt%sn,temp_key3,word) fclose(foutput) 。 foutput=fopen(key3.txt,a) 。 fprintf(foutput,3t%dtt%sn,temp_key3,word) fclose(foutput) 。/* 整数类型判断函数 */void inta_s

14、earch(char *word)FILE *foutput,*finput 。char c 。char cs100 。int csi=0 。int line=0 。 int value2=0 。if (temp_c40=0)foutput=fopen(temp_c40,a) 。 fprintf(foutput,%sn,word) 。 fclose(foutput) 。temp_c40+。 foutput=fopen(c40.txt,a) 。 fprintf(foutput,4t0t1t%sn,word) 。 fclose(foutput) 。 finput=fopen(temp_c40,r)

15、 。 c=fgetc(finput) 。while (c!=EOF)while (c!=n)。 andlno4oso_o4 (pOM七。 anduu=oso_o4 宀。 anduu=)oo6To宀。LHCXI nro(0upOM-so)dEOls)七。+u=。0.LS。O一-一S。一 S。 宀。 (Indu 匸)006170。0+土 so一 S。 O.LS。茎一。00 匸 soElp。0elp Indurlndlnor-山 J 匚 (POM*elp)lloe slu- po 懸国B冢尉絲oldlu9)七。 OHCXI nro lu-。 o$u=c一。 O.LS。茎一00 匸 soElp。0el

16、p Indurlndlnor-山 J 匚 (POM*elp)lpe SI。po* 懸国B冢呱(nFee*。 andlno4oso_o4 (pOM-LtoldE9-=ss%AP%AI4=ndlno4=lu_d4 。 (=e=-=pqo芸=)u dorlndlnop。 andlno4oso_o4 (pOM-LtoldE9-=ss%AP%Awindlno4=lu_d4 (-e=cxnndlno=)u dorlndlncxlcxl/6。LHCXI nro(0upOM-so)dEOls)七。+u=。O.LS。O一-一S。一 S。宀。 (Indu 匸)006170。0+土 so一 S。(-ubl!.。)

17、2ZM(丄0山I!.。)壬M。 anduu=)oo6To (=-寸0ldlu9=)u dorlndujj宀。 andlno4oso_o4(p0M-=ss%=5$2M7=ndln04u_d4 (=e=-=pqe 芸=)u dorlndln。+2 寸 0ldlu9。 andlno4oso_o4 (pOM-=ss%=ndlno4=lu_d4 (=e=-=e 寸0ldlu9=)u dorlndlnoA (=e=cxye 芸=)u dorlndlnoA。 andlno4oso_o4 (p0MQtoldE9-=ss%AP%A$2=ndln04u_d4 (=e=cxnndlno=)u dorlndlnoA。

18、+2 寸 0ldlu9。 andlno4oso_o4。 (pOM-=ss%=ndlno4=lu_d4 (=寸。ldE2=)ued04ndm04 so 宀。 andlno4oso_o4 (p0M(Du=-=ss%AP%A$2=ndln04u_d4 (=e=cxnndlno=)u dorlndlnoA(UHCXI n_e七。 anduu=oso_o4 宀。 anduu=)oo6To宀。CXICXI、0 L(pOM-=ss%=l40M7=ndlno4u_d4(-B-CXTCXI芸=)u dorlndln。+CXI寸 0ldlu9。 andlno4oso_o4(pOM-=ss%=ndlno4=lu_d

19、4寸0ldlu9=)u dorlndlnoA) (0UUEIduJ9)七 。 OHCXI nro lu- o$u=c一 。 0.LS。茎一 。00 匸 soElp。0elp Indurlndlnor-山 J 匚 (POM*elp)lpees。po*。 andlno4oso_o4(EOMO芸 ldlu9-=ss%AP%A$2一寸=ndln04=lu_d4。 anduu=)oo6To宀。LHCXI nro(0upOM-so)dEOls)七。+u=。O.LS。O一-一S。一 S。 宀(Indu 匸)006170。0+土 so一 S。(-ubl!.。) 2ZM(丄0山I!.。)壬M 。 anduu=)

20、oo6To (=-&寸0lduJ9=)u dorlndujj 宀 。 (Indlno4oso_o4*。 andlno4oso_o4 (pOM7toldlu9-=ss%AP%10=ndlno4u_d4 。 (=e=-=pqcxl芸=)u dorlndln。 andlno4oso_o4 (pOM7toldlu9-=ss%AP%10=ndlno4u_d4 (=e=cxnndlno=)u dorlndln。+CXI寸 0ldlu9。 andlno4oso_o4。 (pOM-=ss%=ndlno4=lu_d4 寸0ldlu9=)u dorlndlnoA so 宀。 andlno4oso_o4 (pOM七

21、 。 anduu=oso_o4void scan()int count 。 char chin 。 FILE *fin 。 FILE *fout 。 char filename50 。 char temp100 。 char target3= 。 printf( 请输入文件名: ) 。 scanf(%s,filename) 。 if (fin=fopen(filename,r)=NULL) printf(Error! Cant open file : %sn,filename) return 。chin=fgetc(fin) 。while (chin!=EOF)/* 对文件包含、宏定义进行处理

22、 */if (chin=#)while (chin!=) chin=fgetc(fin) 。/*chin=fgetc(fin)。 */* 对空格符、水平制表符进行处理 */ else if (chin= )|(chin=t) 。/* 对回车符进行处理 */else if (chin=n)。/* 对单引号内地字符常量进行处理 */ else if (chin=target0) if (xx19=0)fout=fopen(key1.txt,a) 。 fprintf(fout,1t9tt%cn,target0)fclose(fout) 。 xx19=1 。 temp0=chin 。 chin=fge

23、tc(fin) 。 temp1=chin 。 chin=fgetc(fin) 。 if (chin!=target0) temp2=chin 。 chin=fgetc(fin) 。 temp3=chin 。 temp4=0 。 else temp2=chin 。 temp3=0 。 c_search(temp) 。/* 对双引号内地字符串常量进行处理 */ else if (chin=) int i=0 。 tempi+= 。 chin=fgetc(fin) 。 while (chin!=) tempi+=chin 。 chin=fgetc(fin) 。 tempi= 。 tempi+1=0

24、。 cc_search(temp) 。if/* 对保留字、标识符进行处理 */ else(chin=A)&(chin=a)&(chin=A)&(chin=a)&(chin=0 )&(chin=0)&(chin=0)&(chin=9)|(chin=.) if (chin=.) dotcount+ 。if (dotcount=2) break 。tempi+=chin 。 chin=fgetc(fin) 。tempi=0 。if (dotcount=1) intb_search(temp) 。else inta_search(temp) 。if (chin!=EOF)fseek (fin,-1L,

25、SEEK_CUR) 。/* 对注释进行处理 */else if (chin=/)chin=fgetc(fin) 。if (chin=)fout=fopen(output.txt,a) 。 fprintf(fout,2t30tt/=n) 。 fclose(fout) 。else if (chin!=*)fout=fopen(output.txt,a) fprintf(fout,2t4tt/n) fclose(fout) 。 fseek(fin,-1L,SEEK_CUR) 。else if (chin=*)count=0 。 chin=fgetc(fin) 。 fout=fopen(definat

26、ion.txt,a) fprintf(fout,/*) 。 while (count!=2) count=0 。 while (chin!=*) fprintf(fout,%c,chin) chin=fgetc(fin) 。 count+ 。 fprintf(fout,%c,chin) chin=fgetc(fin) 。 if (chin=/)count+ 。 fprintf(fout,%cn,chin) else fprintf(fout,%c,chin) chin=fgetc(fin) 。/* 对运算符、分隔符进行处理 */elseint time=0 。int firstblood=0

27、。 temp0=chin 。 chin=fgetc(fin) 。if (chin!=EOF)temp1=chin 。temp2=0 。for (time=1 。 time=30 。 time+) if (strcmp(temp,key2time)=0)firstblood=1 。if (xx2time=0)fout=fopen(key2.txt,a) fprintf(fout,2t%dtt%sn,time,temp) fclose(fout) 。xx2time=1 。fout=fopen(output.txt,a) 。 fprintf(fout,2t%dtt%sn,time,temp) fcl

28、ose(fout) 。break 。if (firstblood!=1)fseek(fin,-1L,SEEK_CUR) 。 temp1=0 。for (time=1 。 time=9 。 time+) if (strcmp(temp,key1time)=0)if (xx1time=0)fout=fopen(key1.txt,a) fprintf(fout,1t%dtt%sn,time,temp) fclose(fout) 。xx1time=1 。fout=fopen(output.txt,a) fprintf(fout,1t%dtt%sn,time,temp) fclose(fout) 。br

29、eak 。for (time=1 。 time查看保留字表文件printf(* 1 -查看分隔符表文件printf(* 2 -查看运算符表文件printf(* 3 -查看标识符表文件printf(* 4 -查看整数类型常量表printf(* 5 -查看浮点类型常量表printf(* 6 -查看字符类型常量表printf(* 7 -查看字符串类型常量表printf(* 8 -查看注释文件C 语 言 词 法 分 析 工 具*n)。*n)。*n)。*n)。*n)。*n)。*n)。*n)。*n)。*n)。*n)。*n)。printf(* 9 -查看内部码文件 *n)printf(* *n)printf

30、(* Q -退出 *n)*n)printf( printf(n) load() 。scan() 。printf(n) 。printf( 分析完成! n) 。getchar() 。printf(n) 。printf( 请输入命令: ) 。 command=getchar() 。while (command!=Q)&(command!=q) switch (command)case 0:printf(H*n)printf(n) fread=fopen(key0.txt,r) charin=fgetc(fread) while (charin!=EOF)putchar(charin) charin=

31、fgetc(fread) printf(n)printf(H*n)printf(n) 。printf( 请输入命令: ) 。break 。case 1:printf(H*n)printf(n) fread=fopen(key1.txt,r) charin=fgetc(fread) 。while (charin!=EOF)putchar(charin)charin=fgetc(fread) printf(n)*n)printf( printf(n) 。printf( 请输入命令: ) 。 break 。case 2:*n)printf( printf(n) fread=fopen(key2.tx

32、t,r) charin=fgetc(fread) 。while (charin!=EOF)putchar(charin)charin=fgetc(fread) printf(n) 。printf(H*n)printf(n) 。printf( 请输入命令: ) 。break 。case 3:printf(H*n)printf(n) 。 fread=fopen(key3.txt,r) charin=fgetc(fread) 。 while (charin!=EOF)putchar(charin)charin=fgetc(fread) printf(n) 。printf(H*n)printf(n) 。printf( 请输入命令: ) 。break 。case 4:printf(H*n)case 5:printf(n) 。 fread=fopen(c40.txt,r) charin=fgetc(fre

温馨提示

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

评论

0/150

提交评论