编译原理及实现-附录C-语法分析程序-代码(包含词法分析代码)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

TESTparse.cpp#includestring.h#includestdio.h#includectype.h#includeconio.hintTESTparse();intprogram();intcompound_stat();intstatement();intexpression_stat();intexpression();intbool_expr();intadditive_expr();intterm();intfactor();intif_stat();intwhile_stat();intfor_stat();intwrite_stat();intread_stat();intdeclaration_stat();intdeclaration_list();intstatement_list();intcompound_stat();//token保存单词符号,token1保存单词值chartoken[20],token1[40];//保存词法分析输出文件名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);}//声明序列::=声明序列声明语句1声明语句//declaration_list::=//declaration_listdeclaration_stat|e//3tJ^ldeclaration_list::*{declaration_stat}intdeclaration_list(){intes=0;while(strcmp(token,int)==0){es=declaration_stat();if(es0)return(es);}return(es);}//声明语句::=ID变量;//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);}//语句序列::=语句序列语句|e//statement_list::=statement_liststatement|e//改成statement_list::8={statement}intstatement_list(){intes=0;while(strcmp(token,})){es=statement();if(es0)return(es);}return(es);}//语句::=if语句|while參语句|for语句丨write语句|resd语句丨复合语句丨表达式语句//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,resd)==0)es=read_stat();//readS^Jif(es==0&&strcmp(token,write)==0)es=write_stat();//writeS^Iif(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(表达式)语句4136语句]//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,())if(es0)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,;))if(es0)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,)))if(es0)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,())if(es0)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,)))if(es0)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,())if(es0)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,)))if(es0)return(es=6);//少右括号fscanf(fp,%s%s\n,&token,&token1);printf(%s.%s\n,token,token1);es=statement();return(es);if(strcmp(token,;))if(es0)return(es=4);//少分号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);es=expression();if(es0)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,;))if(es0)return(es=4);//少分号fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token1);return(es);}//resci_j5^:^找日化变量;//resd_stat::=resdID;intread_stat(){intes=0;fscanf(fp,%s%s\n,&token,&token1);printf(%s%s\n,token,token

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功