《C语言词法分析器》开发文档及源代码_第1页
《C语言词法分析器》开发文档及源代码_第2页
《C语言词法分析器》开发文档及源代码_第3页
《C语言词法分析器》开发文档及源代码_第4页
《C语言词法分析器》开发文档及源代码_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

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

文档简介

1、C语言词法分析器开发文档Powered By 萌萌的玉雪一、实验题目编制并调试C词法分析程序。二、实验目的全面深入理解高级语言程序设计知识,掌握应用技巧,提高应用与分析能力。三、主要函数void load ( ) void char_search (char *word)void main ( ) void intb_search(char *word)void scan ( ) void c_search(char *word)void inta_search(char *word) void cc_search(char *word)四、设计1.主函数 void main ( )绘制程序界

2、面调用初始化函数:void load ( ) 实现文件的建立调用主扫描函数:void scan ( ) 实现文件的扫描分析完成后指引用户查看相关文件,直到用户输入退出命令函数结束2. 初始化函数 void load ( )通过文件指针建立相关文件函数结束3. 保留字及标识符判断函数 void char_search(char *word)接收数据是否为保留字否:查标识符表是:写入输出文件没找到:写入表文件找到:写入输出文件写入输出文件4. 整数类型判断函数 void inta_search(char *word)接收数据找到:写入输出文件写入输出文件查表没找到:写入整数常量表文件5. 浮点类型

3、判断函数 void intb_search(char *word)没找到:写入整数常量表文件找到:写入输出文件查表接收数据写入输出文件6. 字符串常量判断函数 void cc_search(char *word)7. 字符常量判断函数 void c_search(char *word) 同4、5函数图8.主扫描函数 void scan ( )函数开始读入源文件中的一个单词判断宏定义忽略无效字符忽略调用保留字及标识符函数void char_search进行处理字符串字符常量调用字符常量判断函数void c_search进行处理调用字符串常量判断函数void cc_search进行处理字符串常量整

4、数常量调用整数类型判断函数void inta_search进行处理调用浮点类型判断函数void intb_search进行处理浮点数常量注释写入注释文件查运算符、分隔符表并写入输出文件限制符五、关键代码#include <stdio.h>#include <string.h>#include <stdlib.h>char *key0=" ","auto","break","case","char","const","continu

5、e","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","siz

6、eof","static","struct","switch","typedef","_Complex","_Imaginary","union","unsigned","void","volatile","while"/*保留字表*/char *key1=" ","(",")","",&

7、quot;","","",",","","'"/*分隔符表*/char *key2=" ","+","-","*","/","%","<",">","=",">=","<=","!=","!",&q

8、uot;&&","|","<<",">>","","|","","&","=","?:","->","+","-",".","+=","-=","*=","/="/*运算符表*/int xx035

9、,xx110,xx231;int temp_key3=0,temp_c40=0,temp_c41=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;mm+)xx2mm=0;FILE *floading;if (floading=fopen("key0.txt","w")=NULL)printf("Error! Can'

10、t create file : key0.txt");return;fclose (floading);/*建立保留字表文件:key0.txt*/if (floading=fopen("key1.txt","w")=NULL)printf("Error! Can't create file : key1.txt");return;/*建立分隔符表文件:key1.txt*/if (floading=fopen("key2.txt","w")=NULL)printf("E

11、rror! Can't create file : key2.txt");return;fclose(floading);/*建立运算符表文件:key2.txt*/if (floading=fopen("key3.txt","w")=NULL)printf("Error! Can't create file : key3.txt");return;fclose (floading);/*建立标识符表文件:key3.txt*/if (floading=fopen("c40.txt",&quo

12、t;w")=NULL)printf("Error! Can't create file : c40.txt");return;fclose (floading);/*建立整数类型常量表文件:c40.txt*/if (floading=fopen("c41.txt","w")=NULL)printf("Error! Can't create file : c41.txt");return;fclose (floading);/*建立浮点类型常量表文件:c41.txt*/if (floadin

13、g=fopen("c42.txt","w")=NULL)printf("Error! Can't create file : c42.txt");return;fclose (floading);/*建立字符类型常量表文件:c42.txt*/if (floading=fopen("c43.txt","w")=NULL)printf("Error! Can't create file : c43.txt");return;fclose (floading);/*

14、建立字符串类型常量表文件:c43.txt*/ if (floading=fopen("defination.txt","w")=NULL)printf("Error! Can't create file : defination.txt");return;fclose (floading);/*建立注释文件:defination.txt*/if (floading=fopen("output.txt","w")=NULL)printf("Error! Can't cre

15、ate file : output.txt");return;fclose (floading);/*建立内部码文件:output.txt*/if (floading=fopen("temp_key1","w")=NULL)printf("Error! Can't create file : temp_key1");return;fclose (floading);/*建立保留字临时表文件:temp_key1*/if (floading=fopen("temp_key3","w"

16、;)=NULL)printf("Error! Can't create file : temp_key3");return;fclose (floading);/*建立标识符临时文件:temp_key3*/if (floading=fopen("temp_c40","w")=NULL)printf("Error! Can't create file : temp_c40");return;fclose (floading);/*建立整数类型常量临时文件:temp_c40*/if (floading=

17、fopen("temp_c41","w")=NULL)printf("Error! Can't create file : temp_c41");return;fclose (floading);/*建立浮点类型常量临时文件:temp_c41*/if (floading=fopen("temp_c42","w")=NULL)printf("Error! Can't create file : temp_c42");return;fclose (floading

18、);/*建立字符类型常量临时文件:temp_c42*/if (floading=fopen("temp_c43","w")=NULL)printf("Error! Can't create file : temp_c43");return;fclose (floading);/*建立字符串类型常量临时文件:temp_c43*/* 保留字及标识符判断函数 */void char_search(char *word)int m,line=0,csi=0;int value=0;int value2=0;char c,cs100;F

19、ILE *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%dtt%sn",m,word);fclose(foutput);xx0m=1;foutput=fopen("output.txt","a");fprintf(foutput,"0t%

20、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);finput=fopen("

21、;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;elsevalue2=0;c=fgetc(finput);fclose(finput);if (value2=1)foutput=fopen("output.txt","a");fprintf(foutput,

22、"3t%dtt%sn",line,word);fclose(foutput);elsefoutput=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=f

23、open("key3.txt","a");fprintf(foutput,"3t%dtt%sn",temp_key3,word);fclose(foutput);/* 整数类型判断函数 */void inta_search(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(

24、foutput,"%sn",word);fclose(foutput);temp_c40+;foutput=fopen("c40.txt","a");fprintf(foutput,"4t0t1t%sn",word);fclose(foutput);finput=fopen("temp_c40","r");c=fgetc(finput);while (c!=EOF)while (c!='n')cscsi+=c;c=fgetc(finput);cscsi=

25、9;0'csi=0;line+;if (strcmp(cs,word)=0)value2=1;break;c=fgetc(finput);fclose(finput);if (value2=1)foutput=fopen("output.txt","a");fprintf(foutput,"4t0t%dt%sn",line,word);fclose(foutput);elsefoutput=fopen("temp_c40","a");fprintf(foutput,"%sn&q

26、uot;,word);fclose(foutput);temp_c40+;foutput=fopen("output.txt","a");fprintf(foutput,"4t0t%dt%sn",temp_c40,word);fclose(foutput);foutput=fopen("c40.txt","a");fprintf(foutput,"4t0t%dt%sn",temp_c40,word);fclose(foutput);/* 浮点类型判断函数 */void int

27、b_search(char *word)FILE *foutput,*finput;char c;char cs100;int csi=0;int line=0;int value2=0;if (temp_c41=0)foutput=fopen("temp_c41","a");fprintf(foutput,"%sn",word);fclose(foutput);temp_c41+;foutput=fopen("c41.txt","a");fprintf(foutput,"4t1t1t

28、%sn",word);fclose(foutput);finput=fopen("temp_c41","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;c=fgetc(finput);fclose(finput);if (value2=1)foutput=fopen("output.txt&q

29、uot;,"a");fprintf(foutput,"4t1t%dt%sn",line,word);fclose(foutput);elsefoutput=fopen("temp_c41","a");fprintf(foutput,"%sn",word);fclose(foutput);temp_c41+;foutput=fopen("output.txt","a");fprintf(foutput,"4t1t%dt%sn",temp_

30、c41,word);fclose(foutput);foutput=fopen("c40.txt","a");fprintf(foutput,"4t1t%dt%sn",temp_c41,word);fclose(foutput);/* 字符串常量判断函数 */void cc_search(char *word)FILE *foutput,*finput;char c;char cs100;int csi=0;int line=0;int value2=0;if (temp_c43=0)foutput=fopen("temp_

31、c43","a");fprintf(foutput,"%sn",word);fclose(foutput);temp_c43+;foutput=fopen("c43.txt","a");fprintf(foutput,"4t3t1t%sn",word);fclose(foutput);finput=fopen("temp_c43","r");c=fgetc(finput);while (c!=EOF)while (c!='n')c

32、scsi+=c;c=fgetc(finput);cscsi='0'csi=0;line+;if (strcmp(cs,word)=0)value2=1;break;c=fgetc(finput);fclose(finput);if (value2=1)foutput=fopen("output.txt","a");fprintf(foutput,"4t3t%dt%sn",line,word);fclose(foutput);elsefoutput=fopen("temp_c43","a&q

33、uot;);fprintf(foutput,"%sn",word);fclose(foutput);temp_c43+;foutput=fopen("output.txt","a");fprintf(foutput,"4t3t%dt%sn",temp_c43,word);fclose(foutput);foutput=fopen("c43.txt","a");fprintf(foutput,"4t3t%dt%sn",temp_c43,word);fclos

34、e(foutput);/* 字符常量判断函数 */void c_search(char *word)FILE *foutput,*finput;char c;char cs100;int csi=0;int line=0;int value2=0;if (temp_c42=0)foutput=fopen("temp_c42","a");fprintf(foutput,"%sn",word);fclose(foutput);temp_c42+;foutput=fopen("c42.txt","a"

35、);fprintf(foutput,"4t2t1t%sn",word);fclose(foutput);finput=fopen("temp_c42","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;c=fgetc(finput);fclose(finput);if (value2=1)fo

36、utput=fopen("output.txt","a");fprintf(foutput,"4t2t%dt%sn",line,word);fclose(foutput);elsefoutput=fopen("temp_c42","a");fprintf(foutput,"%sn",word);fclose(foutput);temp_c42+;foutput=fopen("output.txt","a");fprintf(foutpu

37、t,"4t2t%dt%sn",temp_c42,word);fclose(foutput);foutput=fopen("c42.txt","a");fprintf(foutput,"4t2t%dt%sn",temp_c42,word);fclose(foutput);/* 主扫描函数 */void scan()int count;char chin;FILE *fin;FILE *fout;char filename50;char temp100;char target3="'"pri

38、ntf("请输入文件名:");scanf("%s",filename);if (fin=fopen(filename,"r")=NULL)printf("Error! Can't open file : %sn",filename);return;chin=fgetc(fin);while (chin!=EOF)/*对文件包含、宏定义进行处理*/if (chin='#')while (chin!='>')chin=fgetc(fin);/*chin=fgetc(fin)

39、;*/*对空格符、水平制表符进行处理*/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=fgetc(fin);temp1=c

40、hin;chin=fgetc(fin);if (chin!=target0)temp2=chin;chin=fgetc(fin);temp3=chin;temp4='0'elsetemp2=chin;temp3='0'c_search(temp);/*对双引号内的字符串常量进行处理*/else if (chin='"')int i=0;tempi+='"'chin=fgetc(fin);while (chin!='"')tempi+=chin;chin=fgetc(fin);tempi

41、='"'tempi+1='0'cc_search(temp);/*对保留字、标识符进行处理*/else if (chin>='A')&&(chin<='Z')|(chin>='a')&&(chin<='z')|(chin='_')int i=0;while (chin>='A')&&(chin<='Z')|(chin>='a')&

42、&(chin<='z')|(chin='_')|(chin>='0')&&(chin<='9')tempi+=chin;chin=fgetc(fin);tempi='0'char_search(temp);if (chin!=EOF)fseek (fin,-1L,SEEK_CUR);/*对整型、浮点型数据进行处理*/else if (chin>='0')&&(chin<='9')int dotcount=0;int

43、 i=0;while (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);elseinta_search(temp);if (chin!=EOF)fseek (fin,-1L,SEEK_CUR);/*对注释进行处理*/else if (chin=

44、'/')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 i

45、f (chin='*')count=0;chin=fgetc(fin);fout=fopen("defination.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='/

46、9;)count+;fprintf(fout,"%cn",chin);elsefprintf(fout,"%c",chin);chin=fgetc(fin);/*对运算符、分隔符进行处理*/elseint time=0;int firstblood=0;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

47、)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);fclose(fout);break;if (firstblood!=1)fseek(fin,-1L,SEEK_CUR);temp1='0'for (time=

48、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);break;for (time

49、=1;time<=30;time+)if (strcmp(temp,key2time)=0)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);fclose(fout);break;chin=fg

50、etc(fin);fout=fopen("output.txt","a");fprintf(fout,"1t6ttn");fclose(fout);/* Main函数 */void main()FILE *fread;char charin;char command='Q'printf("n");printf("* C语言词法分析工具 *n");printf("* *n");printf("* *n");printf("* 命令如

51、下: *n");printf("* 0 -> 查看保留字表文件 *n");printf("* 1 -> 查看分隔符表文件 *n");printf("* 2 -> 查看运算符表文件 *n");printf("* 3 -> 查看标识符表文件 *n");printf("* 4 -> 查看整数类型常量表 *n");printf("* 5 -> 查看浮点类型常量表 *n");printf("* 6 -> 查看字符类型常量表

52、*n");printf("* 7 -> 查看字符串类型常量表 *n");printf("* 8 -> 查看注释文件 *n");printf("* 9 -> 查看内部码文件 *n");printf("* - *n");printf("* Q -> 退出 *n");printf("*n");printf("n");load();scan();printf("n");printf("分析完成!n&q

53、uot;);getchar();printf("n");printf("请输入命令:");command=getchar();while (command!='Q')&&(command!='q')switch (command)case '0':printf("*n");printf("n");fread=fopen("key0.txt","r");charin=fgetc(fread);while (char

54、in!=EOF)putchar(charin);charin=fgetc(fread);printf("n");printf("*n");printf("n");printf("请输入命令:");break;case '1':printf("*n");printf("n");fread=fopen("key1.txt","r");charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=f

温馨提示

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

评论

0/150

提交评论