IF-ELSE条件语句的翻译程序设计(递归下降法、输出四元式) 下载本文

武汉理工大学《编译原理》课程设计说明书

//

bool cifafenxi() {

char buf[16]; char ch; while(1) {

ins>>ch; if(ins.fail()) break; if(ch=='I') {

ins>>buf;

if(strcmp(buf,\ tokentable[total_len++].type=$IF; }

else if(ch=='T') { }

ins>>buf;

if(strcmp(buf,\ tokentable[total_len++].type=$THEN;

else if(ch=='E') {

ins>>buf;

if(strcmp(buf,\

tokentable[total_len++].type=$ELSE;

}

else if(ch=='>') {

tokentable[total_len++].type=$GREAT;

}

else if(ch=='<') { }

tokentable[total_len++].type=$LESS;

else if(ch=='=') {

tokentable[total_len++].type=$ASSIGN; }

else if(ch=='x' || (ch>='a' && ch<='z')) {

tokentable[total_len].type=$ID;

15

武汉理工大学《编译原理》课程设计说明书

}

}

}

tokentable[total_len++].ch=ch;

return true;

#define AD_RESULT(nlabel,nop,npar1,npar2,nresult)

quad[quad_len].par1=npar1;

{quad[quad_len].label=nlabel; quad[quad_len].par2=npar2;

quad[quad_len].op=nop;

quad[quad_len].result=nresult; quad_len++; }

#define AD_ADDRESS(nlabel,nop,npar1,npar2,naddress) { quad[quad_len].label=nlabel; quad[quad_len].op=nop; quad[quad_len].par1=npar1; quad[quad_len].par2=npar2;

quad[quad_len].address=naddress;

quad_len++; }

Wtoken cur; bool nexttoken() { if(token_index>=total_len) }

void ERROR(char str[20]) { }

void S(int begin,int next) {

return false;

cur.type=tokentable[token_index].type; cur.ch=tokentable[token_index].ch; token_index++; return true;

cout<<\ \

if(cur.type==$ID) {

char a,b;

cout<<\a=cur.ch;

if(!nexttoken()) ERROR(\if(cur.type!=$ASSIGN)

ERROR(\

16

武汉理工大学《编译原理》课程设计说明书

}

cout<<\if(!nexttoken()) ERROR(\if(cur.type!=$ID) ERROR(\cout<

AD_RESULT(begin,ASSIGN,b,0,a); AD_ADDRESS(-1,JUMP,0,0,next); nexttoken();

}

else if(cur.type==$IF) { }

if(!nexttoken()) { }

ERROR(\

cout<<\int etrue=newlabel(); int efalse=newlabel();

E(begin,etrue,efalse); if(cur.type==$THEN) { }

if(!nexttoken()) ERROR(\

S(etrue,next); if(cur.type==$ELSE) { }

if(!nexttoken()) ERROR(\S(efalse,next);

else { ERROR(\}

else ERROR(\

17

武汉理工大学《编译原理》课程设计说明书

void E(int begin,int etrue,int efalse) { }

if(cur.type==$ID) { }

char a,b; int mark=0; a=cur.ch;

cout<<\if(!nexttoken()) ERROR(\if(cur.type==$GREAT) {cout<<'>';mark=1;} else if(cur.type==$LESS) cout<<'<'; else ERROR(\

ERROR(\if(cur.type!=$ID) ERROR(\cout<

if(mark==0) AD_ADDRESS(begin,JL,a,b,etrue); if(mark==1)

AD_ADDRESS(begin,JG,a,b,etrue);

if(!nexttoken())

AD_ADDRESS(-1,JUMP,0,0,efalse); if(!nexttoken())

ERROR(\

else ERROR(\

void yufafenxi() { }

18

S(-1,1000);

AD_RESULT(1000,END,0,0,'-');

武汉理工大学《编译原理》课程设计说明书

void print() { }

for(token_index=0;token_index

if(tokentable[token_index].type==$IF) cout<<\ \关键字\if(tokentable[token_index].type==$ELSE) cout<<\ \关键字\

if(tokentable[token_index].type==$THEN) cout<<\ \关键字\if(tokentable[token_index].type==$ID) cout<'<<\ \运算符\if(tokentable[token_index].type==$LESS) cout<<'<'<<\ \运算符\

}

token_index=0;

void printFourtable() {

for(int i=0;i

if(quad[i].label>-1) cout<<\else cout<<\ \if(quad[i].op==JG) {

cout<<\

}

else if(quad[i].op==JL) { }

cout<<\

else if(quad[i].op==JUMP) { cout<<\}

else if(quad[i].op==ASSIGN)

19