您好,欢迎访问三七文档
编译原理实验报告实验名称:语法分析实验类型:设计性实验指导教师:蒋勇专业班级:软件1001班姓名:李岳东学号:20101557实验地点:东6A-319实验成绩:____________________________日期:2012年月日一、实验目的编译技术是理论与实践并重的课程,而其实验课要综合运用一、二年级所学的多门课程的内容,用来完成一个小型编译程序。从而巩固和加强对词法分析、语法分析、语义分析、代码生成和报错处理等理论的认识和理解;培养学生对完整系统的独立分析和设计的能力,进一步培养学生的独立编程能力。二、实验设计开始输入源文件路径路径是否有效是初始化文件指针否将字符加入字符数组Word[]是空格,空白或换行吗是字母吗是数字吗否否是界符吗否打开源文件跳过该字符是是文件结束?否将字符加入字符数组Word[]否将字符加入字符数组Word[]是指向下一字符识别指针内容指向下一字符是字母惑数字吗是将word与关键字表key进行匹配否匹配?是输出word为关键字输出word为普通标示符否将字符加入字符数组Word[]指向下一字符输出word为常数识别指针内容回退是数字吗是否输出word为界符指向下一字符结束是输出Word内容为不可识别将字符加入字符数组Word[]三、实验过程1)给定文法:(文法举例)EE+T|TTT*F|FF(E)|i2)构造FIRST()集和FOLLOW()集表3-1FIRST()集和FOLLOW()集FIRSTFOLLOWE(,i#,+,)T(,i#,+,),*F(,i#,+,),*四、实验结果表4-1分析成功表4-2分析失败表4-3载入文件出错流程图如下所示:五、附录:关键代码#includeiostream#includestdio.h#includectype.h#includeconio.h#includestring.husingnamespacestd;intTESTparse();intprogram();intcompound_Stat();intstatement();intexpression_Stat();intexpression_r();intbool_expr();intadditive_expr();intterm();intfactor();intif_stat();intwhile_stat();intfor_stat();intwrite_stat();intread_stat();intdeclaration_stat();intdeclaration_list();intstatement_list();intcompound_stat();chartoken[20],token1[40];//token保存单词符号,token1保存单词值charScanout[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);}//声明序列::=声明序列声明语句|声明语句//declaration_list::=//declaration_listdeclaration_stat|declaration_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//改成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语句//可在此处添加do语句调用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(expr)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_r();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_r();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(expr,expr,expr)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_r();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_r();if(es0)return(es
本文标题:语法分析 实验报告
链接地址:https://www.777doc.com/doc-3960833 .html