您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 编译原理实现一个简单的计算器程序
《编译原理》实验报告专业:学号:班级:姓名:实验题目:设计,实现一个简单的计算器程序实验时间:指导教师:实验成绩:1实验目的1.1实现一个简单计算器2实验要求2.1单词的构词规则有明确的定义;2.2编写的分析程序能够正确识别源程序中的语法符号;2.3计算器源程序的以.c格式输出2.4对于源程序中的词法错误,能够做出简单的错误处理,给出简单的错误提示,保证顺利完成整个源程序的词法分析,语法分析;2.5计算器能够实现加,减,乘,除,混合运算,多项式运算。3实验环境3.1WindowsXP3.2Flex词法分析工具3.3VisualStudioC++6.04实验原理4.1多项式计算器的实现,采用后缀表达式来对输入的计算表达式进行计算4.2对后缀表达式进行符号识别,词法分析,语法分析4.3编写计算器的源程序,使用Flex工具生成计算器的C语言程序4.4对生成的程序进行相应的修改,再进行编译,连接,运行,测试得到可以用于进行多项式运算的源程序。5软件设计与编程5.1程序源代码:5.2新建文件夹5.3将Flex工具复制到该文件夹下5.4在DOS环境学生成目标源程序5.5在VisualStudioC++6.0环境下进行相应的修改,添加main方法和打开文件的方法6程序测试结果6.1编写源代码6.2使用Flex生成C代码:6.3在VisualStudioC++6.0上运行生成的”.c”文件文件:y.ta.h#ifndefYYTOKENTYPE#defineYYTOKENTYPEenumyytokentype{CLEAR=258,EXIT=259,LIST=260,ERASE=261,DEC=262,HEX=263,OCT=264,HELP=265,NUM=266,REG=267,ADD=268,SUB=269,MUL=270,DIV=271,MOD=272,LSHIFT=273,RSHIFT=274,AND=275,OR=276,NOT=277,LESS=278,MORE=279,BITAND=280,BITOR=281,BITXOR=282,BITREV=283,SIN=284,COS=285,TAN=286,ABS=287,SQRT=288,LOG=289,RMINUS=290};//前面定义过,此处省略宏定义#if!defined(YYSTYPE)&&!defined(YYSTYPE_IS_DECLARED)typedefintYYSTYPE;#defineyystypeYYSTYPE/*obsolescent;willbewithdrawn*/#defineYYSTYPE_IS_DECLARED1#defineYYSTYPE_IS_TRIVIAL1#endifexternYYSTYPEyylval;文件:calc.l核心程序%{#defineYYSTYPEdouble#defineBIGINTlong#includectype.h#includey.tab.h%}digit[0-9]xdigit[0-9a-fA-F]odigit[0-7]decnum(0(\.{digit}+)?)|([1-9]{digit}*(\.{digit}+)?)octnum0{odigit}+hexnum0(x|X){xdigit}+reg[a-zA-Z]opt1+|-|*|/|&|||%|^|~|!||opt2(&&)|(\|\|)|(\\)|(\\)exit((E|e)(X|x)(I|i)(T|t))|((Q|q)(U|u)(I|i)(T|t))clear(C|c)(L|l)(E|e)(A|a)(R|r)list(L|l)(I|i)(S|s)(T|t)erase(E|e)(R|r)(A|a)(S|s)(E|e)hex(H|h)(E|e)(X|x)oct(O|o)(C|c)(T|t)dec(D|d)(E|e)(C|c)help(H|h)(E|e)(L|l)(P|p)sin(S|s)(I|i)(N|n)cos(C|c)(O|o)(S|s)log(L|l)(O|o)(G|g)tan(T|t)(A|a)(N|n)abs(A|a)(B|b)(S|s)sqrt(S|s)(Q|q)(R|r)(T|t)%%inti;BIGINTval;[;\t]{}{decnum}{sscanf(yytext,%lf,&yylval);return(NUM);}{octnum}{i=1;val=0;while(iyyleng){val=(val3)+yytext[i]-'0';i++;}yylval=val;return(NUM);}{hexnum}{i=2;val=0;while(iyyleng){if(islower(yytext[i]))val=(val4)+yytext[i]-'a'+10;elseif(isupper(yytext[i]))val=(val4)+yytext[i]-'A'+10;elseval=(val4)+yytext[i]-'0';i++;}yylval=val;return(NUM);}{reg}{if(islower(yytext[0]))yylval=yytext[0]-'a';elseyylval=yytext[0]-'A';return(REG);}{opt1}{switch(yytext[0]){case'+':returnADD;break;case'-':returnSUB;break;case'*':returnMUL;break;case'/':returnDIV;break;case'%':returnMOD;break;case'^':returnBITXOR;break;case'&':returnBITAND;break;case'|':returnBITOR;break;case'':returnLESS;break;case'':returnMORE;break;case'!':returnNOT;break;case'~':returnBITREV;break;}}{opt2}{switch(yytext[0]){case'&':returnAND;break;case'|':returnOR;break;case'':returnLSHIFT;break;case'':returnRSHIFT;break;}}{sin}{return(SIN);}{cos}{return(COS);}{tan}{return(TAN);}{log}{return(LOG);}{abs}{return(ABS);}{sqrt}{return(SQRT);}{clear}{return(CLEAR);}{exit}{return(EXIT);}{list}{return(LIST);}{erase}{return(ERASE);}{hex}{return(HEX);}{oct}{return(OCT);}{dec}{return(DEC);}{help}{return(HELP);}.|\n{return(yytext[0]);}文件:y.tab.c部分代码:#defineYYBISON1/*Skeletonname.*/#defineYYSKELETON_NAMEyacc.c/*Pureparsers.*/#defineYYPURE0/*Usinglocations.*/#defineYYLSP_NEEDED0/*Tokens.*/#ifndefYYTOKENTYPE#defineYYTOKENTYPE/*Putthetokensintothesymboltable,sothatGDBandotherdebuggersknowaboutthem.*/enumyytokentype{前面定义过,此处省略};/*Copythefirstpartofuserdeclarations.*/#line1calc.y//前面定义过,此处省略宏定义#includectype.h#includestdio.h#includestdlib.h#includemath.h#includelex.yy.c//这是词法分析器生成的文件,必须包含!YYSTYPEreg[26]={0};BIGINTival;enumDisplay{DEC_ON,HEX_ON,OCT_ON};enumDisplaydflag=DEC_ON;inti;/*Enablingtraces.*/#ifndefYYDEBUG#defineYYDEBUG0#endif/*Enablingverboseerrormessages.*/#ifdefYYERROR_VERBOSE#undefYYERROR_VERBOSE#defineYYERROR_VERBOSE1#else#defineYYERROR_VERBOSE0#endif#if!defined(YYSTYPE)&&!defined(YYSTYPE_IS_DECLARED)typedefintYYSTYPE;#defineyystypeYYSTYPE/*obsolescent;willbewithdrawn*/#defineYYSTYPE_IS_DECLARED1#defineYYSTYPE_IS_TRIVIAL1#endif/*Copythesecondpartofuserdeclarations.*//*Line214ofyacc.c.*/#line173y.tab.c#if!defined(yyoverflow)||YYERROR_VERBOSE/*Theparserinvokesallocaormalloc;definethenecessarysymbols.*/#ifYYSTACK_USE_ALLOCA#defineYYSTACK_ALLOCalloca#else#ifndefYYSTACK_USE_ALLOCA#ifdefined(alloca)||defined(_ALLOCA_H)#defineYYSTACK_ALLOCalloca#else#ifdef__GNUC__#defineYYSTACK_ALLOC__builtin_alloca#endif#endif#endif#endif#ifdefYYSTACK_ALLOC/*PacifyGCC's`emptyif-body'warning.*/#defineYYSTACK_FREE(Ptr)do{/*empty*/;}while(0)#else#ifdefined(__STDC__)||defined(__cplusplus)#includestdlib.h/*INFRINGESONUSERNAMESPACE*/#defineYYSIZE_Tsize_t#endif#defineYYSTACK_ALLOCmalloc#defineYYSTACK_FREEfree#endif#endif/*!defined(yyoverflow)||YYERROR_VERBOSE*/#if(!defined(yyoverflow)\&&(!defined(__cplusplus)\||(YYSTYPE_IS_TRIVIAL)))/*Atypethatisproperlyalignedforanystackmember.*/unionyyalloc{shortyyss;YYSTYPEyyvs;};/*Thesizeofthemaximumgapbetweenonealignedstackandthenext.*/#defineYYSTACK_GAP_MAXIMUM(sizeof(unionyyalloc)-1)/*Thesizeofanarraylargetoenoughtoholdallstacks,eachwithNelements.*/#defineYYSTACK_BYTES(N)\((N)*(sizeof
本文标题:编译原理实现一个简单的计算器程序
链接地址:https://www.777doc.com/doc-2141125 .html