您好,欢迎访问三七文档
语法分析题目递归下降语法分析器的实现要求实现以下语法的递归下降分析:将要实现语言的上下文无关文法进行检查,消除左递归和左公共因子,从逻辑上检测避免死循环和低效率处理。改写后的文法为:program-blockblock-{stmts}stmts-stmtstmts|nullstmt-id=expr;|if(bool)stmtstmt1|while(bool)stmt|dostmtwhile(bool)|break|blockstmt1-elsestmt|nullbool-exprbool1|exprbool1bool1-expr|=exprexpr-termexpr1expr1-+termexpr1|-termexpr1|nullterm-factorterm1term1-*factorterm1|/factorterm1|nullfactor-(expr)|id|num每个产生式的左边的文法符号对应一个函数或过程的形式。源代码//语法分析.cpp:定义控制台应用程序的入口点。//#includestdafx.h#includestdio.h#includestring.h#includectype.h#includeiostreamusingnamespacestd;/*if=100,for=200,else=300,while=400,do=500,float=600,int=700,break=800,=17,==16,=15,==14,+=13,&&=12,||=11,}=10,{=9,;=8;)=7,(=6,==5,===4,!==3,/=2,id=1,keyword=0,num=99,*=18,-=19*/char*keyword[8]={if,for,else,while,do,float,int,break};//100200300400500600700800char*othercha[18]={/,!=,==,=,(,),;,{,},||,&&,+,=,,=,,*,-};//2345678910111213141516171819charkeywordtable[20][20],re_keywordtable[20][20];chardigittable[20][20],re_digittable[20][20];charotherchartable[20][20],re_otherchartable[20][20];charidtable[20][20],re_idtable[20][20];charnotetable[20][20];charfinaltable[100][20];intfinaltableint[100];charword[20];intkindofchr();voidinitialize();voidalpha();voiddigit();voiderror();voidotherchar();voidnote();voidprint();voidprin();voidcheck();voidprogram();voidblock();voidstmts();voidstmt();voidBool();voidexpr();voidexpr1();voidterm();voidterm1();voidfactor();voidmatch(char*t);intdigit_num=0,keyword_num=0,otherchar_num=0,id_num=0,note_num=0;intredigit_num=1,rekeyword_num=1,reotherchar_num=1,reid_num=1;intfinal_num=0,finalnum=0;intflag_error=0;intflagerror=0;charlookahead;intmain(){printf(请输入要分析的语句:\n);initialize();inti=1;while(i){lookahead=getchar();switch(kindofchr()){case0:{;}case1:{alpha();initialize();break;}case2:{initialize();break;}case4:{otherchar();initialize();break;}case3:{i=0;}}}check();if(flag_error==0){printf(词法分析结果如下:\n);print();prin();program();if(finalnum==final_num)printf(语法分析完成!\n);}system(pause);return0;}intkindofchr(){charch=lookahead;if(isalpha(ch)){return1;}elseif(isdigit(ch)){return2;}elseif(ch=='\t'||ch==''){return0;}elseif(ch=='\n'){return3;}else{return4;}}voidalpha(){charch=lookahead;inti=0;while(isalpha(ch)||isdigit(ch)){word[i++]=ch;//当前字符放入一临时字符数组;ch=getchar();//从缓冲区取下一字符};ungetc(ch,stdin);intflag=0;for(i=0;i8;i++){if(strcmp(word,keyword[i])==0){strcpy(keywordtable[keyword_num++],word);strcpy(finaltable[final_num],word);finaltableint[final_num++]=(i+1)*100;flag=1;}}if(flag==0){strcpy(idtable[id_num++],word);strcpy(finaltable[final_num],id);finaltableint[final_num++]=1;}/*inti=1,flag;charch;ch=lookahead;word[0]=ch;ch=getchar();while(isalpha(ch)||isdigit(ch)){word[i++]=ch;ch=getchar();}ungetc(ch,stdin);flag=0;for(i=0;i8;i++){if(strcmp(word,keyword[i])==0)flag=1;}if(flag==1){strcpy(keywordtable[keyword_num++],word);strcpy(finaltable[final_num],word);if(strcmp(word,if)==0)finaltableint[final_num++]=100;if(strcmp(word,for)==0)finaltableint[final_num++]=200;if(strcmp(word,else)==0)finaltableint[final_num++]=300;if(strcmp(word,while)==0)finaltableint[final_num++]=400;if(strcmp(word,do)==0)finaltableint[final_num++]=500;if(strcmp(word,float)==0)finaltableint[final_num++]=600;if(strcmp(word,int)==0)finaltableint[final_num++]=700;if(strcmp(word,break)==0)finaltableint[final_num++]=800;}else{strcpy(idtable[id_num++],word);strcpy(finaltable[final_num],id);finaltableint[final_num++]=1;}*/}voiddigit(){charch=lookahead;inti=0;while(isalpha(ch)||isdigit(ch)){word[i++]=ch;//当前字符放入一临时字符数组;ch=getchar();//从缓冲区取下一字符};ungetc(ch,stdin);intflag=0;for(i=0;word[i]!='\0';i++){if(!isdigit(word[i])){flag=1;break;}}if(flag==1){strcpy(idtable[id_num++],word);strcpy(finaltable[final_num],id);finaltableint[final_num++]=1;}else{strcpy(digittable[digit_num++],word);strcpy(finaltable[final_num],num);finaltableint[final_num++]=99;}}voidotherchar(){charch=lookahead;inti=0;word[0]=ch;ch=getchar();word[1]=ch;intflag=0;if(word[0]=='/'&&word[1]=='*'){note();}coutword[0]'\t'word[1];for(inti=0;i18;i++){if(strcmp(word,othercha[i])==0){strcpy(otherchartable[otherchar_num++],othercha[i]);strcpy(finaltable[final_num],othercha[i]);finaltableint[final_num++]=i+2;flag=1;}}if(flag==0){ungetc(ch,stdin);word[1]='\0';for(inti=0;i18;i++){if(strcmp(word,othercha[i])==0){strcpy(otherchartable[otherchar_num++],othercha[i]);strcpy(finaltable[final_num],othercha[i]);finaltableint[final_num++]=i+2;flag=1;}}}if(flag==0){cout'\t'111;error();}/*charch;ch=lookahead;switch(ch){case'!':{ch=getchar();if(ch=='='){strcpy(otherchartable[otherchar_num++],!=);strcpy(finaltable[final_num],!=);finaltableint[final_num++]=3;}else{ungetc(ch,stdin);error();}}break;case'=':{ch=getchar();if(ch=='='){strcpy(otherchartable[otherchar_num++],==);strcpy(finaltable[final_num],==);finaltableint[final_num++]=4;}else{strcpy(otherchartable[otherchar_num++],=);strcpy(finaltable[final_num],=);finaltableint[final_num++]=5;ungetc(ch,stdin);}}break;case'(':strcpy(otherchartable[otherchar_num++],();strcpy(f
本文标题:语法分析
链接地址:https://www.777doc.com/doc-7175058 .html