c語言詞法分析器

2021-04-01 07:06:42 字數 3434 閱讀 1115

#include

#include

#include

void  main(void);

int reserver(char *);

void main(void)

if((output=fopen("output.txt","wt"))==null)

fprintf(output,"%4s/t/%8s/t %-16s)/t%4s/n","位置","符號型別","識別符","長度");

ch=fgetc(input);

while(ch!=eof)

token[i]='/0';

isreserver=reserver(token);

if(isreserver==1)

else if(isreserver==0)

else if(isreserver==2)

continue;   

}/*識別數字*/

else if(isdigit(ch))

/*出現非數字且為點時*/   

if (ch=='.')

/*如果是數字加點再加數字再出現字母時,就是錯誤*/

if(isalpha(ch))

token[i]='/0';

count++;fprintf(output,"%4d/t(標識錯誤/t,%-16s)/t%4d/n",count,token,i);

continue; 

}/*當出現結束符時,就收入為實數內*/

else}}

/*如果是字元,則判斷為標識錯誤*/

else if(isalpha(ch))

token[i]='/0';

count++;fprintf(output,"%4d/t(標識錯誤/t,%-16s)/t%4d/n",token);

continue; 

}/*如果是單詞段結束符時,就判斷為常數*/  

else

}     

else if(ch=='(')

else if(ch==')')

else if(ch=='[')

else if(ch==']')

else if(ch=='-')

else if (ch=='>')

token[i]='/0';

count++;fprintf(output,"%4d/t(負運算子/t,%-16s)/t%4d/n",count,token,i);

continue;

}else if(ch=='.')

else if(ch=='&')  

else if(ch=='!')  

token[i]='/0';

count++;fprintf(output,"%4d/t(取反運算子/t,%-16s)/t%4d/n",count,token,i);

continue;   

}else if(ch=='~')  

else if(ch=='*')

else if(ch=='%')  

else if(ch=='/')  

else if(ch=='<')

if(ch=='<')

token[i]='/0';

count++;fprintf(output,"%4d/t(小於比較符/t,%-16s)/t%4d/n",count,token,i);

continue;

}else if(ch=='>')

if(ch=='>')

token[i]='/0';

count++;fprintf(output,"%4d/t(大於比較符/t,%-16s)/t%4d/n",count,token,i);

continue;

}else if(ch=='=')

token[i]='/0';

count++;fprintf(output,"%4d/t(賦值運算子/t,%-16s)/t%4d/n",count,token,i);

continue;

}else if(ch==',')

else if(ch==';')

else if(ch=='

else if(ch=='}')

else if(ch=='+')

token[i]='/0';

count++;fprintf(output,"%4d/t(加運算子/t,%-16s)/t%4d/n",count,token,i);

continue;

}else if(ch=='"')

if(ch=='"')

else

token[i]='/0';

count++;fprintf(output,"%4d/t(字串錯誤/t,%-16s)/t%4d/n",count,token,i);}} 

else

token[i]='/0';

count++;fprintf(output,"%4d/t(無法識別錯誤/t,%-16s)/t%4d/n",count,token,i);

continue;}}

fclose(input);

fclose(output);

}/*判斷是不是關鍵字:

1,則是識別符號

0,則是關鍵字

2,則是運算子

*/int reserver(char *str)

while(fscanf(fpreserver,"%s",tmpstr)!=eof)

if(strcmp(str,tmpstr)==0)

fclose(fpreserver);

return 1; 

} 輸入檔案input.txt

#include

void e(char *);

void e1(char *);

void t(char *);

void t1(char *);

void f(char *);

void main()

void e(char *str)

void e1(char *str)

void t(char *str)

void t1(char *str)

}void f(char *str)

}else if(*str=='i')

str++;

else

}關鍵字檔案keyword.txt

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語言詞法分析器

include include include include define len 180 int syn,p,m,n char token 10 ch char keyword 32 struct wordline 定義結構體 struct wordline creat struct wordl...

編譯原理 C語言詞法分析器

本程式預設的種別碼的編碼 主介面 功能1提到的input.txt檔案的內容可以是任意c語言 這裡我的input.txt隨便打了一段c語言 進入1功能,直接從input.txt檔案讀取 這裡沒寫路徑輸入,必須把input.txt和本程式的cpp檔案放在同一目錄下 經過分析後得到結果 沒做清屏處理,寫得...

C語言詞法分析器 編譯原理

這是老師佈置的編譯原理的實驗課任務,課餘時間花了近一個星期的時間去編寫 主要是c 太久沒有用了,好多函式都不熟悉,查閱了很多資料 這次的詞法分析只有簡單的語法錯誤判斷功能,約等於沒有.如果想要增加功能可以在相關函式 段增加即可.而且token的種別碼只做了粗略的區分,想要加上也很簡單 c語言詞法分析...

C語言詞法分析器 C 語言實現

這是老師佈置的編譯原理的實驗課任務,課餘時間花了近一個星期的時間去編寫 主要是c 太久沒有用了,好多函式都不熟悉,查閱了很多資料 這次的詞法分析也沒有語法錯誤判斷功能,如果想要增加功能可以在相關函式 段增加即可.對於此法單元的種別碼也只是簡單區分,不過自己加上去就可以了 c語言詞法分析流程如圖所示 ...

用C語言寫的Pascal語言詞法分析器

雖然只是個詞法分析器,但是重拾c語言好難!好久沒用過c了。放在這兒勉勵自己做事一定要有始有終 trans.c main,trans include include include include char name char sign int match code 0 char buffer1 25...