#includestdio.h#includeiostream#includectype.h#includeconio.h#includestring.h#includestring#includealgorithmintTESTparse();intprogram();intcompound_Stat();intstatement();intexpression_Stat();intexpression();intbool_expr();intExchange();intadditive_expr();intterm();intfactor();intif_stat();intwhile_stat();intfor_stat();intwrite_stat();intread_stat();intdeclaration_stat();intdeclaration_list();intstatement_list();intcompound_stat();intexpression_stat();chartoken[20],token1[40];//token保存单词符号,token1保存单词值externcharScanout[300];//保存词法分析输出文件名FILE*fp;//用于指向输入文件的指intTESTparse(){intes=0;if((fp=fopen(Scanout,r))==NULL){printf(\n打开%s错误!\n,Scanout);es=10;}if(es==0)es=program();printf(=====语法分析结果!=====\n);switch(es){case0:printf(语法分析成功!\n);break;case10:printf(打开文件%s失败!\n,Scanout);break;case1:printf(缺少{!\n);break;case2:printf(缺少}!\n);break;case3:printf(缺少标识符!\n);break;case4:printf(少分号!\n);break;case5:printf(缺少(!\n);break;case6:printf(缺少)!\n);break;case7:printf(缺少操作数!\n);break;}fclose(fp);return(es);}//program::={declaration_liststatement_list}intprogram(){intes=0;fscanf(fp,%s%s\n,token,token1);printf(%s%s\n,token,token1);if(strcmp(token,{))//判断是否为'{'{es=1;return(es);}fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=declaration_list();if(es0)return(es);es=statement_list();if(es0)return(es);if(strcmp(token,}))//判断是否为'}'{es=2;return(es);}return(es);}//声明序列::=声明序列声明语句|$//delaration_list::=//declaration_listdeclaration_stat|$//改成declaration_list::={declaration_stat}intdeclaration_list(){intes=0;while(strcmp(token,int)==0){es=declaration_stat();if(es0)return(es);}return(es);}//声明语句::=int变量;//declaration_stat::=intID;intdeclaration_stat(){intes=0;fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);if(strcmp(token,ID))return(es=3);//不是标识符fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);if(strcmp(token,;))return(es=4);fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);return(es);}//语句序列::=语句序列语句|$//statement_list::=statement_liststatement|$//改成statement_list::={statement}intstatement_list(){intes=0;while(strcmp(token,})){es=statement();if(es0)return(es);}return(es);}//语句::=if语句|while语句|for语句|read语句|write语句|复合语句|表达式语句//statement::=if_stat|while_stat|for_stat|compound_stat|expression_statintstatement(){intes=0;if(es==0&&strcmp(token,if)==0)es=if_stat();//if语句if(es==0&&strcmp(token,while)==0)es=while_stat();//while语句if(es==0&&strcmp(token,for)==0)es=for_stat();//for语句if(es==0&&strcmp(token,read)==0)es=read_stat();//read语句if(es==0&&strcmp(token,write)==0)es=write_stat();//write语句if(es==0&&strcmp(token,{)==0)es=compound_stat();//复合语句if(es==0&&strcmp(token,ID)==0||strcmp(token,NUM)==0||strcmp(token,()==0)es=expression_stat();//表达式语句return(es);}//if语句::=if(表达式)语句[else语句]//if_stat::=if(expression)statement[elsestatement]intif_stat(){intes=0;//iffscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);if(strcmp(token,())return(es=5);//少左括号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=expression();if(es0)return(es);if(strcmp(token,)))return(es=6);//少右括号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=statement();if(es0)return(es);if(strcmp(token,else)==0)//else部分处理{fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=statement();if(es0)return(es);}return(es);}//while语句::=while(表达式)语句//while_stat::=while(expr)statementintwhile_stat(){intes=0;fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);if(strcmp(token,())return(es=5);//少左括号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=expression();if(es0)return(es);if(strcmp(token,)))return(es=6);//少右括号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=statement();return(es);}//for语句::=for(表达式;表达式;表达式)语句//for_stat::=for(expression;expression;expression)statementintfor_stat(){intes=0;fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);if(strcmp(token,())return(es=5);//少左括号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=expression();if(es0)return(es);if(strcmp(token,;))return(es=4);//少分号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=expression();if(es0)return(es);if(strcmp(token,;))return(es=4);//少分号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=expression();if(es0)return(es);if(strcmp(token,)))return(es=6);//少右括号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=statement();return(es);}//write_语句::=write表达式;//write_stat::=writeexpression;intwrite_stat(){intes=0;fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=expression();if(es0)return(es);if(strcmp(token,;))return(es=4);//少分号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);return(es);}//read_语句::=read变量;//read_stat::=readID;intread_stat(){intes=0;fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);if(strcmp(token,ID))return(es=3);//少标识符fscanf(fp