实验一--识别无符号数的词法分析器设计实现

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

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

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

资源描述

实验一识别无符号数的词法分析器设计实现学院:计算机学院班级:学号:一、实验目的与要求通过编写并上机调试一个词法分析程序,掌握在对程序设计语言的源程序进行扫描的过程中,将其分解成各类单词的词法分析方法。二、实验内容选取无符号数的算术四则运算中的各类单词为识别对象,要求将其中的各个单词识别出来。输入:由无符号数和+,-,*,/,(,)构成的算术表达式,如1.5E+2-100。输出:对识别出的每一单词均单行输出其类别码(无符号数的值暂不要求计算)。单词符号类别码(CLASS)单词值(VALUE)无符号数1数字值+2无值—3无值*4无值/5无值(6无值)7无值如1(对应1.5E+2)3(对应-)1(对应100)三、程序源代码:#includeiostreamusingnamespacestd;#defineM101//最多可输入的字符数#defineUNSIGNEDNUMBER1//无符号数#definePLUS2//加号#defineSUBTRACT3//减号#defineMULTIPLY4//乘号#defineDIVIDE5//除号#defineLEFTBRACKET6//左括号#defineRIGHTBRACKET7//右括号classAccidenceAnalysis//定义词法分析器类{private:chartestStr[M],*p;//私有数据public:AccidenceAnalysis();//构造函数voidInputStr();//输入函数voidOutput(inta,char*p1,char*p2);//输出函数intIsAcceptantCharacter(char*p);//判断输入字符是否属于字符集intIsOperator(char*p);//判断字符是否是字符集[+,-,*,/,(,)]中的字符intIsUnsignedNum(char*p);//判断字符是否是0--9的整数voidAbnormityExamine(chara[]);voidIdentifyOperator(char*p);//识别字符集[+,-,*,/,(,)]中的字符voidAssortIdentify();//对输入字符分类识别};AccidenceAnalysis::AccidenceAnalysis(){inti;for(i=0;iM;i++)testStr[i]='\0';p=&testStr[0];//指针P指向字符数组首元素}voidAccidenceAnalysis::InputStr(){cout\t请按要求输入您要分析的语句,所输字符应在要求范围(不超过M)之内,并按回车键运行:;charch;inti=0;while((ch=cin.get())!='\n'){testStr[i]=ch;i++;}AbnormityExamine(testStr);}voidAccidenceAnalysis::AbnormityExamine(chara[]){intj=0;char*ptr1,*ptr2;ptr1=a;ptr2=a;while(*ptr2!='\0'){j++;if(!IsAcceptantCharacter(ptr2)){cout\t您输入的第j个字符*ptr2不可以被此程序识别!\将被跳过.endl;ptr2++;continue;}else{*ptr1=*ptr2;ptr1++;ptr2++;}}while(ptr1=ptr2){*ptr1='\0';ptr1++;}}voidAccidenceAnalysis::Output(inta,char*p1,char*p2){cout\t类别码:a单词值:;while(p1=p2){cout*p1;p1++;}coutendl;}intAccidenceAnalysis::IsOperator(char*p){charch=*p;if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')')return1;elsereturn0;}intAccidenceAnalysis::IsUnsignedNum(char*p){charch=*p;if('0'=ch&&ch='9')return1;elsereturn0;}intAccidenceAnalysis::IsAcceptantCharacter(char*p){charch=*p;if(IsOperator(p)||IsUnsignedNum(p)||ch=='E'||ch=='.')return1;elsereturn0;}voidAccidenceAnalysis::IdentifyOperator(char*p){charch=*p;switch(ch){case'+':Output(PLUS,p,p);break;case'-':Output(SUBTRACT,p,p);break;case'*':Output(MULTIPLY,p,p);break;case'/':Output(DIVIDE,p,p);break;case'(':Output(LEFTBRACKET,p,p);break;case')':Output(RIGHTBRACKET,p,p);break;default:break;}}voidAccidenceAnalysis::AssortIdentify(){while(*p!='\0'){if(IsOperator(p)){IdentifyOperator(p++);continue;}elseif(IsUnsignedNum(p)||*p=='.'){char*p1=p;if(IsUnsignedNum(p)){while(IsUnsignedNum(p))p++;if(*p=='\0'){Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}elseif(*p=='E'){p++;if(IsUnsignedNum(p)){while(IsUnsignedNum(p))p++;Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}elseif(*p=='+'||*p=='-'){p++;while(IsUnsignedNum(p))p++;Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}}elseif(*p=='.'){p++;while(IsUnsignedNum(p))p++;if(*p=='\0'){Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}elseif(*p=='E'){p++;if(IsUnsignedNum(p)){while(IsUnsignedNum(p))p++;Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}elseif(*p=='+'||*p=='-'){p++;while(IsUnsignedNum(p))p++;Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}}else{Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}}else{Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}}if(*p=='.'){p++;if(IsUnsignedNum(p)){p++;while(IsUnsignedNum(p))p++;if(*p=='\0'){Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}elseif(*p=='E'){p++;if(IsUnsignedNum(p)){while(IsUnsignedNum(p))p++;Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}elseif(*p=='+'||*p=='-'){p++;while(IsUnsignedNum(p))p++;Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}}else{Output(UNSIGNEDNUMBER,p1,--p);p++;continue;}}}}}coutendl;cout\t单词分析完毕.endl;}intmain(){AccidenceAnalysisaccidenceanalysis;accidenceanalysis.InputStr();accidenceanalysis.AssortIdentify();return0;}四、运行结果:1正确输入:2错误输入:

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

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

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

×
保存成功