Pascal语言的语法分析器基于文档最下面词法分析器写的#includeiostream#includecstring#includefstream#includesstream#includestringusingnamespacestd;fstreamf;charch,ch1[100];intcla,no,num;stringstr;//program11//var12//begin13//end14//integer15//real16//while17//if18//then19//else110//do111//;5,0//.6,0//:7,0//,8,0//:=9,0//+10,0;//-11,0//*12,0///13,0//14,0//=15,0//=16,0;//17,0//=18,0//19,0//||20,0//&21,0//!22,0//(23,0//)24,0voidscan(){f.getline(ch1,100);stringstreamss(ch1);sschclachnochstr;}boolisprogram();boolvariable();boolconstant();boolfactor();boolterm();boolarithmetic();boolassignment();boolrelation();boolcondition();boolrepeat();boolcomplex();boolstatement();boolstatementlist();boolblock();booljudge();intmain(){f.open(out.txt,ios::in);boolflag=false;num=0;if(judge()){flag=true;}if(flag){//判断是不是程序coutItisaprogram!endl;}else{coutItisnotaprogram!endl;}system(pause);return0;}boolisprogram(){//判断程序scan();//判断progreamif(cla!=1&&no!=1)returnfalse;scan();//判断程序名if(cla!=3&&no!=1)returnfalse;num++;scan();//判断分号if(cla!=5&&no!=0)returnfalse;scan();//判断varif(cla!=1&&no!=2)returnfalse;while(true){//if(cla==1&&no==3)break;//如果遇到begin跳出//if(cla!=3)returnfalse;//既不是begin也不是变量while(true){scan();if(cla==1&&no==3)returntrue;elseif(cla==3){num++;}elsereturnfalse;scan();if(cla!=8){//不是,跳出break;}}if(cla!=7)returnfalse;//不是:scan();if(cla!=1&&(no!=5||no!=6))returnfalse;//不是intgerrealscan();if(cla!=5)returnfalse;}returntrue;}boolvariable(){//变量scan();if(cla==3)returntrue;else{f.unget();returnfalse;}}boolconstant(){//常数scan();if(cla!=2&&cla!=6){//2为整数6为.f.unget();returnfalse;}if(cla==2){//前面是整数,scan();if(cla!=6){//后面不是点,则返回,此数为整数f.unget();returntrue;}else{//后面是点scan();if(cla!=2){//点后面不是整数返回falsef.unget();returnfalse;}returntrue;}}elseif(cla==6){//前面是.scan();if(cla!=2){//点后面不是整数返回falsef.unget();returnfalse;}returntrue;}}boolfactor(){//因式if(variable())returntrue;//变量elseif(constant())returntrue;//常数scan();if(cla==23){if(!arithmetic())returnfalse;//算术表达式scan();if(cla==24)returntrue;f.unget();returnfalse;}f.unget();returnfalse;}boolterm(){//项if(factor()){scan();if(cla!=12&&cla!=13){f.unget();returntrue;}else{if(term())returntrue;returnfalse;}}returnfalse;}boolarithmetic(){//判断算术表达式if(term()){scan();if(cla!=10&&cla!=11){f.unget();returntrue;}if(arithmetic())returntrue;returnfalse;}}boolassignment(){//赋值scan();if(cla!=3){f.unget();returnfalse;}//判断变量scan();if(cla!=9||no!=0){f.unget();returnfalse;}//判断:=if(!arithmetic())returnfalse;/*scan();if(cla!=5)returnfalse;*/returntrue;}boolrelation(){//关系表达式if(!arithmetic())returnfalse;scan();if(cla==14||cla==15||cla==16||cla==17||cla==18||cla==19){//如果是关系运算符if(!arithmetic()){f.unget();returnfalse;}returntrue;}f.unget();//如果不是关系运算符returnfalse;}boolcondition(){//条件scan();if(cla!=1||no!=8){//iff.unget();returnfalse;}intflag=0;while(true){scan();if(cla!=23){f.unget();//判断是否有(有的话过滤掉break;}flag++;}intnr=0;while(true){scan();if(cla!=20&&cla!=21&&cla!=22){if(cla==24){f.unget();break;}//遇到)跳出if(cla==1&&no==9){//遇到then返回f.unget();break;}f.unget();}intfl=0;while(true){scan();if(cla!=23){f.unget();//判断是否有(有的话过滤掉break;}fl++;}if(!relation()){//关系表达式returnfalse;}nr++;if(nr==1){while(true){scan();if(cla==24)flag--;if(fl==0){//关系表达式的右括号等于左括号跳出break;}}}if(nr!=1){while(true){scan();if(cla==24)fl--;if(fl==0){//关系表达式的右括号等于左括号跳出break;}}}}while(true){//判断(==)scan();if(cla!=24){f.unget();//判断是否有(有的话过滤掉break;}flag--;}if(flag!=0)returnfalse;scan();if(cla!=1||no!=9){//thenf.unget();returnfalse;}if(!statement())returnfalse;//语句scan();if(cla!=1||no!=10){//elsef.unget();returnfalse;}if(!statement())returnfalse;//语句returntrue;}boolrepeat(){//whilescan();if(cla!=1||no!=7){f.unget();returnfalse;}if(!relation())returnfalse;scan();if(cla!=1||no!=11){f.unget();returnfalse;}if(!statement())returnfalse;returntrue;}boolcomplex(){//复合f.getline(ch1,100);stringstreamss(ch1);sschclachnochstr;if(cla!=1||no!=3){f.unget();returnfalse;}if(!statementlist())returnfalse;f.getline(ch1,100);ssch1;sschclachnochstr;if(cla!=1||no!=4){f.unget();returnfalse;}returntrue;}boolstatement(){//语句判断函数boola=assignment();if(a)returntrue;//赋值boolb=condition();if(b)returntrue;//条件boolc=repeat();if(c)returntrue;//whileboold=complex();if(d)returntrue;//复合//if(a||b||c||d)returntrue;returnfalse;}boolstatementlist(){//语句表判断函数if(!statement())returnfalse;//不是语句返回falsescan();if(cla!=5){//是语句后面不是分号if(cla==1&&no==4){returntrue;}f.unget();returnfalse;}if(!statementlist()){//后面分号但后面不是语句表。//if(cla==1&&no==4)returntrue;returnfalse;}returntrue;}boolblock(){//判断子程序if(!statementlist())returnfalse;//判断是不是语句表scan();if(cla!=6){f.unget();returnfalse;}returntrue;}booljudge(){//判断是否为程序if(!isprogram()){//判断是否为程序returnfalse;}if(!block()){//判断是否为子程序returnfalse;}returntrue;}词法分析器%{#includestdio.h#includestring.hstaticintflag1=1;staticintflag2=1;intin=1;intvar=1;intloop=0;inti;chartmp_c[12];%}%%//flag1=0;/*flag2=0;*/flag2=1;\nflag1=1;programif(is_com())out(1,1,yytext);varif(is_com())out(1,2,yytext);beginif(is_com())out(1,3,yytext);endif(is_com())out(1,4,y