编译原理――无符号数的算术四则运算语义处理程序实现【源代码】

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

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

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

资源描述

编译原理实验三源程序及实验结果【源程序】#includeiostream#includeiomanipusingnamespacestd;//使用标准命名空间#defineM101//定义可输入的字符个数#defineUNSIGNEDNUMBER1//无符号数#definePLUS2//加号#defineSUBTRACT3//减号#defineMULTIPLY4//乘号#defineDIVIDE5//除号#defineLEFTBRACKET6//左括号#defineRIGHTBRACKET7//右括号#defineINEFFICACIOUSLABEL8//无效字符串/*----------------------------------------------i1+2-3*4/5(6)7#8E9T10F11S代表移近操作1【S15,115例外】R代表归约操作2acc3识别成功-----------------------------------------------*///ACTON表,十位代表是移进还是归约操作,分别为1||11,2//个位代表下一状态号或所用来归约的产生式号intACTION[16][8]={{15,0,0,0,0,14,0,0},{0,16,17,0,0,0,0,30},{0,23,23,18,19,0,23,23},{0,26,26,26,26,0,26,26},{15,0,0,0,0,14,0,0},{0,28,28,28,28,0,28,28},{15,0,0,0,0,14,0,0},{15,0,0,0,0,14,0,0},{15,0,0,0,0,14,0,0},{15,0,0,0,0,14,0,0},{0,16,17,0,0,0,115,0},{0,21,21,18,19,0,21,21},{0,22,22,18,19,0,22,22},{0,24,24,24,24,0,24,24},{0,25,25,25,25,0,25,25},{0,27,27,27,27,0,27,27}};//GOTO表intGOTO[10][3]={{1,2,3},{0,0,0},{0,0,0},{0,0,0},{10,2,3},{0,0,0},{0,11,3},{0,12,3},{0,0,13},{0,0,14}};/*---------------------------------------------------------------------------------------无符号数形式分类情况1:.123E+4||123.4E+5包含.+E情况2:.123E4||123.E4包含.E情况3:123E+4包含.+E情况4:.123||123.4包含.情况5:123E4包含E情况6:123-----------------------------------------------------------------------------------------*/charpause;intstaticcount=0;//初始化余留分析串数组intstaticMark1=0;//标记余留分析串数组是否有无效串产生classCompile//定义词法分析类{private:charTestStr[M],*p;//输入的待分析字符串,和指向它的首地址的指针intLeftSymbelStack[M],*p1;//定义余留符号串栈和指向它的指针intStatusStack[M],*p2;//定义状态栈和指向它的指针intAnalysisStack[M],*p3;//定义分析栈和指向它的指针intTopSat,InpSym;//栈顶状态,当前输入单词doubleCalculateStack[M],*ptrcal;//定义计算数值用的计算栈和指向它的首地址的指针doubleUnsignedFigure[M];//存放每个无符号数的值public:Compile();//构造函数~Compile();//析构函数voidInputTestStr();//输入字符序列函数intEllegalCharExamine();//非法字符检测函数//词法分析所用函数voidStartAccidenceAnalysis();//词法分析控制函数intIfAcceptantChar(char*p);//判断输入字符是否属于字符集intIfOperator(char*p);//判断字符是否是字符集[+,-,*,/,(,)]中的字符intIfZeroToNine(char*p);//判断字符是否是0--9的整数voidIdentifyOperator(char*p);//识别字符集[+,-,*,/,(,)]中的字符char*IdentifyUnsignedFigure(char*p);//识别无符号数,并具有纠错能力voidAcciAnaly_Out(inta,char*p1,char*p2);//输出词法分析结果的函数//语法分析所用函数voidStartGrammarAnalysis();//语法分析控制函数intSearchAction(inta,intb);//查询动作表intSearchGoto(inta,intb);//查询状态转移表voidPushStaStack(inta);//下一个状态号压入状态栈voidPushAnalyStack(inta);//将余留符号串的当前字符移入分析栈intPopPtrBackNum(inta);//返回用产生式归约时分析栈和状态栈指针要回退字符的个数voidPopAnalyStack(inta);//归约时分析栈指针回退,回退字符数由PopPtrBackNum(inta)提供voidPopStaStack(inta);//归约时状态栈指针回退,回退字符数由PopPtrBackNum(inta)提供intGramAnaly_ConcludETFtoNum(inta);//归约时状态栈中用来确定产生式左边的符号数号,返回ETF,9,10,11intGOTO_ETFto012(inta);//查GOTO表用来转换voidGramAnaly_OutNumToChar(inta);//进行语法分析时,分析栈和预留符号栈中的数字转换为字符函数:i1+2-3*4/5(6)7#8E9T10F11voidStaStack10_15ToA_F(inta);//状态栈中状态号大于10时转换为字符:10~15=A~FvoidGramAnaly_OutAndCalAll(int*p1,int*p2,int*p3);//输出语法分析过程,同时进行运算式数值计算//计算无符号数所用函数intCalculateIntegerPart(char*p1,char*p2);//计算整数部分doubleCalculateDecimalPart(char*p1,char*p2);//计算小数部分doubleCalculateScientificFigure(inta,doubleb,intc);//计算科学计数doubleCalculateUnsignedFigure(char*p1,char*p2);//计算无符号数//计算运算式数值函数voidCalculateAllValue(inta);//计算整个运算式的数值voidScroll();//输出条线};char*Compile::IdentifyUnsignedFigure(char*p){char*p1=p;//p指针已变为本函数的局部变量,不再影响主调函数if(IfZeroToNine(p)){while(IfZeroToNine(p))p++;if(*p=='\0'){AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}elseif(*p=='E'){p++;if(IfZeroToNine(p)){while(IfZeroToNine(p))p++;AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}elseif(*p=='+'||*p=='-'){p++;while(IfZeroToNine(p))p++;AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}else{AcciAnaly_Out(INEFFICACIOUSLABEL,p1,--p);gotoEnd;}}elseif(*p=='.'){p++;while(IfZeroToNine(p))p++;if(*p=='\0'){AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}elseif(*p=='E'){p++;if(IfZeroToNine(p)){while(IfZeroToNine(p))p++;AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}elseif(*p=='+'||*p=='-'){p++;if(IfZeroToNine(p)){while(IfZeroToNine(p))p++;AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}else{AcciAnaly_Out(INEFFICACIOUSLABEL,p1,--p);p++;gotoEnd;}}else{AcciAnaly_Out(INEFFICACIOUSLABEL,p1,--p);gotoEnd;}}else{AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}}else{AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}}elseif(*p=='.'){p++;if(IfZeroToNine(p)){p++;while(IfZeroToNine(p))p++;if(*p=='\0'){AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}elseif(*p=='E'){p++;if(IfZeroToNine(p)){while(IfZeroToNine(p))p++;AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}elseif(*p=='+'||*p=='-'){p++;while(IfZeroToNine(p))p++;AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}}else{AcciAnaly_Out(UNSIGNEDNUMBER,p1,--p);gotoEnd;}}else{AcciAnaly_Out(INEFFICACIOUSLABEL,p1,--p);gotoEnd;}}End:returnp;}intCompile::SearchAction(inta,intb){returnACTION[a][b-1];}intCompile::SearchGoto(inta,intb){returnGOTO[a][b];}voidCompile::StaStack10_15ToA_F(inta){switch(a){case10:coutA;break;case11:coutB;break;case12:coutC;break;case13:coutD;break;case14:coutE;break;case15:coutF;break;default:break;}}voidCompile::GramAnaly_OutNumToChar(inta){switch(a){case1:couti;break;case2:cout+;break;case3:cout-;break;case4:cout*;brea

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

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

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

×
保存成功