资源描述:
《词法语法语义分析器代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#includeusingnamespacestd;voidscaner();//扫Α?描èvoidcheckIDF();//检ì查é是?否?是?保馈?留?字?voidcheckNum();//检ì查é是?否?是?数簓字?voidretract();//回?退?chargetChar();//获?取?下?一?个?字?符?voidconcatenation();//连?接ó字?符?intreserve();//检ì查é是?否?是?保馈?留?字?void
2、buildlist1();//建¨立ⅰ?标括?识?符?表括?voidbuildlist2();//建¨立ⅰ?数簓字?表括?voiderror();//报馈?错洙?boolletter();//字?母?booldigit();//数簓字?charcharacter[80],token[8],ch,tk[4];//character数簓组哩?保馈?存?输?入?的?所ù有瓺字?符?token当獭?前°要癮检ì查é的?字?符?intsy,syn,pt,pc,pL1=0,pL2=0,p5,p6;//syn种
3、?别纄编括?码?pttoken数簓组哩?下?标括?pccharacter数簓组哩?下?标括?pL1标括?志?符?表括?下?标括?pL2数簓字?表括?下?标括?vectorlist1;//标括?志?符?表括?vectorlist2;//数簓字?表括?voidscaner(){pt=0;for(inti=0;i<8;i++){token[i]=NULL;}chars=getChar();while((s=='')
4、
5、(s=='')){s=getChar();}if(((s
6、<='z')&&(s>='a'))
7、
8、((s<='Z')&&(s>='A'))){checkIDF();}elseif((s>='0')&&(s<='9')){syn=7;checkNum();}elseswitch(s){case'=':s=getChar();if(s=='='){sy=1;syn=14;strcpy(token,"relop");strcpy(tk,"EQ");}else{syn=8;retract();token[pt]='=';}break;case'+':token[p
9、t]='+';syn=9;break;case'*':s=getChar();if(s=='*'){syn=11;strcpy(token,"**");}else{retract();token[pt]='*';syn=10;}break;case'-':token[pt]='-';syn=12;break;case'/':token[pt]='/';syn=13;break;case'>':syn=14;s=getChar();if(s=='='){sy=3;strcpy(token,"relo
10、p");strcpy(tk,"ME");}else{sy=2;retract();strcpy(token,"relop");strcpy(tk,"MT");}break;case'<':syn=14;s=getChar();if(s=='='){sy=5;strcpy(token,"relop");strcpy(tk,"LE");}else{sy=4;retract();strcpy(token,"relop");strcpy(tk,"LT");}break;case'!':s=getChar(
11、);if(s=='='){syn=14;strcpy(token,"relop");strcpy(tk,"UEQ");}else{retract();cout<<"第台?<12、7;token[pt]=')';break;case'&':s=getChar();if(s=='&'){syn=18;strcpy(token,"&&");}else{retract();cout<<"第台?<13、':s=getChar();if(s=='
14、'){syn=19;strcpy(token,"
15、
16、");}else{retract();cout<<"第台?<