?? recursivedropanalysis.c
字號:
/* 4.6 遞歸下降分析法 */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define INPUT_AND_DISPLAY(); /* 讀一個單詞的二元式并輸出單詞種別 */ \
fscanf(fileInput,"%c ",&t.code);\
fscanf(fileInput,"%s ",t.val); \
fputc(t.code,fileOutput);
void E(void);
void E1(void);
void T(void);
void T1(void);
void F(void);/* 函數原型 */
struct code_val{char code;char val[20];} t; /* 定義臨時結構變量,存放單詞二元式。 */
FILE *fileInput,*fileOutput;
int main(int argc,char *argv[])
{
if(argc==1) /*參數個數為1,即46.exe,沒輸入文件名,例如fin.txt*/
{
printf("Have not enter file name.Press any key to exit...");
getch();
exit(0);
}
if((fileInput=fopen(argv[1],"rt"))==NULL)
{ /*"rt",只讀打開一個文本文件,只允許讀數據*/
printf("Cannot open %s\n",argv[1]);
getch();
exit(1);
}
if((fileOutput=fopen("fout.txt","wt+"))==NULL)
{ /*"wt+",讀寫打開或建立一個文本文件,允許讀寫*/
printf("Cannot create fout.txt FILE.Press any key to exit...");
getch();
exit(1);
}
INPUT_AND_DISPLAY();
E( );
if(t.code=='#')
fprintf(fileOutput,"\nok\n");
else
fprintf(fileOutput,"\nerror in main()\n");
fclose(fileInput);
fclose(fileOutput);
getch();
return 0;
}
void E(void){/* E→T E' */
if(t.code=='i'||t.code=='x'||t.code=='y'||t.code=='('){/* if (t.code∈first(T) */
T( );E1( );
}
else{
fprintf(fileOutput,"\nError in E()>%c\n",t.code);
exit(0);
}
}
void E1(void)/* E'→+TE'|ε,E'→-TE'|ε */
{
if(t.code=='+'||t.code=='-'){
INPUT_AND_DISPLAY();
T( );E1( );
}
else if(!(t.code=='#'||t.code==')'))/* if(!t.code∈follow(E')) */
fprintf(fileOutput,"\nError in E1()\n%c\n",t.code);
}
void T(void)/* T→FT' */
{
if(t.code=='i'||t.code=='x'||t.code=='y'||t.code=='('){/* if(t.code∈first(F)) */
F( );T1( );
}
else{
fprintf(fileOutput,"\nError in T()>%c\n",t.code);exit(0);
}
}
void T1(void)/* T'→*FT'|ε, T'→/FT'|ε*/
{
if(t.code=='*'||t.code=='/'){
INPUT_AND_DISPLAY();
F( );T1( );
}
else if(!(t.code=='#'||t.code==')'||t.code=='+'||t.code=='-')){ /* if(!t.code∈follow(T')) */
fprintf(fileOutput,"\nError in T1()>%c\n",t.code);exit(0);
}
}
void F(void)/* F→(E)|i|x|y */
{
if(t.code=='i'||t.code=='x'||t.code=='y'){
INPUT_AND_DISPLAY();
}
else if(t.code=='('){
INPUT_AND_DISPLAY();
E( );
if(t.code==')'){
INPUT_AND_DISPLAY();
}
else{
fprintf(fileOutput,"\nError in F1>%c\n",t.code);exit(0);
}
}
else{
fprintf(fileOutput,"\nError in F2>%c\n",t.code);exit(0);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -