語(yǔ)言詞法分析器源代碼風(fēng)君_第1頁(yè)
語(yǔ)言詞法分析器源代碼風(fēng)君_第2頁(yè)
語(yǔ)言詞法分析器源代碼風(fēng)君_第3頁(yè)
語(yǔ)言詞法分析器源代碼風(fēng)君_第4頁(yè)
語(yǔ)言詞法分析器源代碼風(fēng)君_第5頁(yè)
已閱讀5頁(yè),還剩18頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、C語(yǔ)言詞法分析器_源代碼_風(fēng)君版.txt小時(shí)候覺(jué)得父親不簡(jiǎn)單,后來(lái)覺(jué)得自己不簡(jiǎn)單,再后來(lái)覺(jué)得自己孩子不簡(jiǎn)單。越是想知道自己是不是忘記的時(shí)候,反而記得越清楚。#include <stdio.h>#include <string.h>#include <stdlib.h>char *key0=" ","auto","break","case","char","const","continue","default&q

2、uot;,"do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static&

3、quot;,"struct","switch","typedef","_Complex","_Imaginary","union","unsigned","void","volatile","while"/*保留字表*/char *key1=" ","(",")","","",""

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

5、uot;|","<<",">>","","|","","&","=","?:","->","+","-",".","+=","-=","*=","/="/*運(yùn)算符表*/int xx035,xx110,xx231;int temp_k

6、ey3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;/* 初始化函數(shù) */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't create file : key0.tx

7、t");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("Error! Can't create

8、file : key2.txt");return;fclose(floading);/*建立運(yùn)算符表文件:key2.txt*/if (floading=fopen("key3.txt","w")=NULL)printf("Error! Can't create file : key3.txt");return;fclose (floading);/*建立標(biāo)識(shí)符表文件:key3.txt*/if (floading=fopen("c40.txt","w")=NULL)printf(

9、"Error! Can't create file : c40.txt");return;fclose (floading);/*建立整數(shù)類型常量表文件:c40.txt*/if (floading=fopen("c41.txt","w")=NULL)printf("Error! Can't create file : c41.txt");return;fclose (floading);/*建立浮點(diǎn)類型常量表文件:c41.txt*/if (floading=fopen("c42.txt&q

10、uot;,"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);/*建立字符串類型常量表文件:c43.txt*/

11、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 create file : output.txt&q

12、uot;);return;fclose (floading);/*建立內(nèi)部碼文件:output.txt*/if (floading=fopen("temp_key1","w")=NULL)printf("Error! Can't create file : temp_key1");return;fclose (floading);/*建立保留字臨時(shí)表文件:temp_key1*/if (floading=fopen("temp_key3","w")=NULL)printf("Er

13、ror! Can't create file : temp_key3");return;fclose (floading);/*建立標(biāo)識(shí)符臨時(shí)文件:temp_key3*/if (floading=fopen("temp_c40","w")=NULL)printf("Error! Can't create file : temp_c40");return;fclose (floading);/*建立整數(shù)類型常量臨時(shí)文件:temp_c40*/if (floading=fopen("temp_c41&qu

14、ot;,"w")=NULL)printf("Error! Can't create file : temp_c41");return;fclose (floading);/*建立浮點(diǎn)類型常量臨時(shí)文件:temp_c41*/if (floading=fopen("temp_c42","w")=NULL)printf("Error! Can't create file : temp_c42");return;fclose (floading);/*建立字符類型常量臨時(shí)文件:temp_c

15、42*/if (floading=fopen("temp_c43","w")=NULL)printf("Error! Can't create file : temp_c43");return;fclose (floading);/*建立字符串類型常量臨時(shí)文件:temp_c43*/* 保留字及標(biāo)識(shí)符判斷函數(shù) */void char_search(char *word)int m,line=0,csi=0;int value=0;int value2=0;char c,cs100;FILE *foutput,*finput;fo

16、r (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%dtt%sn",m,word);fc

17、lose(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("temp_key3","

18、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,"3t%dtt%sn",l

19、ine,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=fopen("key3.txt&quo

20、t;,"a");fprintf(foutput,"3t%dtt%sn",temp_key3,word);fclose(foutput);/* 整數(shù)類型判斷函數(shù) */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(foutput,"%sn"

21、,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='0'csi=0;line+;if

22、(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",word);fclose(foutp

23、ut);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);/* 浮點(diǎn)類型判斷函數(shù) */void intb_search(char *word)FIL

24、E *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%sn",word);fclose(

25、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","a");fpr

26、intf(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_c41,word);fclose(foutpu

27、t);foutput=fopen("c40.txt","a");fprintf(foutput,"4t1t%dt%sn",temp_c41,word);fclose(foutput);/* 字符串常量判斷函數(shù) */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_c43","a"

28、);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')cscsi+=c;c=fgetc(finput)

29、;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");fprintf(foutput,&

30、quot;%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);fclose(foutput);/* 字符常量判斷函數(shù)

31、*/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");fprintf(foutput,"

32、;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)foutput=fopen("outpu

33、t.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(foutput,"4t2t%dt%sn"

34、;,temp_c42,word);fclose(foutput);foutput=fopen("c42.txt","a");fprintf(foutput,"4t2t%dt%sn",temp_c42,word);fclose(foutput);/* 主掃描函數(shù) */void scan()int count;char chin;FILE *fin;FILE *fout;char filename50;char temp100;char target3="'"printf("請(qǐng)輸入文件名:"

35、);scanf("%s",filename);if (fin=fopen(filename,"r")=NULL)printf("Error! Can't open file : %sn",filename);return;chin=fgetc(fin);while (chin!=EOF)/*對(duì)文件包含、宏定義進(jìn)行處理*/if (chin='#')while (chin!='>')chin=fgetc(fin);/*chin=fgetc(fin);*/*對(duì)空格符、水平制表符進(jìn)行處理*/els

36、e if (chin=' ')|(chin='t');/*對(duì)回車符進(jìn)行處理*/else if (chin='n');/*對(duì)單引號(hào)內(nèi)的字符常量進(jì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=chin;chin=fgetc(fin);if

37、(chin!=target0)temp2=chin;chin=fgetc(fin);temp3=chin;temp4='0'elsetemp2=chin;temp3='0'c_search(temp);/*對(duì)雙引號(hào)內(nèi)的字符串常量進(jìn)行處理*/else if (chin='"')int i=0;tempi+='"'chin=fgetc(fin);while (chin!='"')tempi+=chin;chin=fgetc(fin);tempi='"'tempi+

38、1='0'cc_search(temp);/*對(duì)保留字、標(biāo)識(shí)符進(jìn)行處理*/else if (chin>='A')&&(chin<='Z')|(chin>='a')&&(chin<='z')|(chin='_')int i=0;while (chin>='A')&&(chin<='Z')|(chin>='a')&&(chin<='z&#

39、39;)|(chin='_')|(chin>='0')&&(chin<='9')tempi+=chin;chin=fgetc(fin);tempi='0'char_search(temp);if (chin!=EOF)fseek (fin,-1L,SEEK_CUR);/*對(duì)整型、浮點(diǎn)型數(shù)據(jù)進(jìn)行處理*/else if (chin>='0')&&(chin<='9')int dotcount=0;int i=0;while (chin>=&#

40、39;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);/*對(duì)注釋進(jìn)行處理*/else if (chin='/')chin=fgetc(

41、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='*')cou

42、nt=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='/')count+;fprintf(fout,

43、"%cn",chin);elsefprintf(fout,"%c",chin);chin=fgetc(fin);/*對(duì)運(yùn)算符、分隔符進(jìn)行處理*/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)fout=fopen("key2.

44、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=1;time<=9;time+)if (

45、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=1;time<=30;time+)if

46、 (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=fgetc(fin);fout=fopen(&qu

47、ot;output.txt","a");fprintf(fout,"1t6ttn");fclose(fout);/* Main函數(shù) */void main()FILE *fread;char charin;char command='Q'printf("n");printf("* C語(yǔ)言詞法分析工具 *n");printf("* *n");printf("* *n");printf("* 命令如下: *n");printf(&qu

48、ot;* 0 -> 查看保留字表文件 *n");printf("* 1 -> 查看分隔符表文件 *n");printf("* 2 -> 查看運(yùn)算符表文件 *n");printf("* 3 -> 查看標(biāo)識(shí)符表文件 *n");printf("* 4 -> 查看整數(shù)類型常量表 *n");printf("* 5 -> 查看浮點(diǎn)類型常量表 *n");printf("* 6 -> 查看字符類型常量表 *n");printf("

49、* 7 -> 查看字符串類型常量表 *n");printf("* 8 -> 查看注釋文件 *n");printf("* 9 -> 查看內(nèi)部碼文件 *n");printf("* - *n");printf("* Q -> 退出 *n");printf("*n");printf("n");load();scan();printf("n");printf("分析完成!n");getchar();printf(

50、"n");printf("請(qǐng)輸入命令:");command=getchar();while (command!='Q')&&(command!='q')switch (command)case '0':printf("*n");printf("n");fread=fopen("key0.txt","r");charin=fgetc(fread);while (charin!=EOF)putchar(charin)

51、;charin=fgetc(fread);printf("n");printf("*n");printf("n");printf("請(qǐng)輸入命令:");break;case '1':printf("*n");printf("n");fread=fopen("key1.txt","r");charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(f

52、read);printf("n");printf("*n");printf("n");printf("請(qǐng)輸入命令:");break;case '2':printf("*n");printf("n");fread=fopen("key2.txt","r");charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf(&q

53、uot;n");printf("*n");printf("n");printf("請(qǐng)輸入命令:");break;case '3':printf("*n");printf("n");fread=fopen("key3.txt","r");charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf("n");pr

54、intf("*n");printf("n");printf("請(qǐng)輸入命令:");break;case '4':printf("*n");printf("n");fread=fopen("c40.txt","r");charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf("n");printf("*n");printf("n");printf("請(qǐng)輸入命令:");break;case '5':printf(&qu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論