首页 > 编程知识 正文

递归下降分析计算表达式(图基检验)

时间:2023-05-03 06:56:38 阅读:74464 作者:935

语法:

电子问答

Q-真正的黑米e

叔调频

R-*FR|e

f-a|(e ) )。

ll(1)递归下降句法分析器

/*E-体贴的帅哥-真正的黑米et-frr-*fr|ef-a|(e )/# include iostream # include cstdio # include string # include vece char s[MAX_N]; vectorstringres; int tot=0; int flag=1; void E (; void Q (; void T (; void R (; void F (; intmain(void ) {int i; 扫描(' % s ',s ); for(I=0; Istrlen(s; I () if (s ) I ) )、=(e )、I )|s )、I )、z )|s )、I )='0) s )=(e ); if(s[tot]!='# ' ) {flag=0; printf(no(n ); (if ) flag ) printf (是(n ); for(I=0; ires.size (; I ) coutres[i]'n '; }返回0; }void E () RES.push_back(e-TQ ); t (); q ); ) }void Q () if ) s[tot]==' ' ) ) ({//printf ) ); RES.push_back(q-TQ ); 托托; t (); q ); }else{RES.push_back(q-e ); }}void T () ) RES.push_back ) ) t-fr ); f (; r (); }void R () if ) s[tot]=='* ' ) RES.push_back ) (r-*fr ) ); 托托; f (; r (); }else{RES.push_back(r-e ); }}void F () if ) s[tot]=='a ' ) ) RES.push_back ) ) f-a ); 托托; }elseif(s[tot]==' (() RES.push_back ) ) f-(e ) ); 托托; e (); if(s[tot]==') ' ) tot; ELSE{printf(no ) n ); flag=0; 返回; }else{printf('non ' ); flag=0; 返回; }ll(1)预测分析器

只需用堆栈模拟替换递归

/*E-体贴的帅哥-真正的黑米et-frr-*fr|ef-a|(e )/# include iostream # include cstdio # include string # include vece char s[MAX_N]; int tot=0; int flag=1; int st[MAX_N],num=0; intmain(void ) {int i; 扫描(' % s ',s ); for(I=0; Istrlen(s; I () if (s ) I ) )、a (s )、I )|s )、I )、z )|s )、I )='9) ) s )=(}int num=0; st[ tot]=1; wile(tot ) ) tot----; if(ST[tot1]==1) printf(e-te(n ) ); st[ tot]=2,st[ tot]=3; }elseif(ST[tot1]==2) if ) s[num]==' ' ) num; printf(e(-te ) (n ); st[ tot]=4,st[ tot]=3; }else{printf(q-(n ); }elseif(ST[tot1]==3) printf(t-ft(n ); st[ tot]=4,st[ tot]=5; }elseif(ST[tot1]==4) if ) s[num]=='* ' ) printf('t'-*ft'n ' ); num; st[ tot]=4,st[ tot]=5; (else ) printf ) ) t(-(n ); }elseif(ST[tot1]==5) if ) s[num]=='a ' ) printf ) f-an ' ); num; }elseif(s[num]==' () ) printf ) ) f-(e ) (n ); num; st[ tot]=1; if(s[num]==') ' ) num; ELSE{printf(error(NNO ) n ); flag=0; 布雷克; }else{printf(error(NNO ) n ); flag=0; 布雷克; }}if(s[num]!='# ' ) {flag=0; printf (错误(nno ) n ); (if ) flag ) printf (是(n ); }返回0; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。