📄 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 + -