#include<stdio.h> #include<string.h> #include<iostream> #include<ctype.h> using namespace std; struct lexer { int symbol; char value[10]; }in0[1000],looktoken; int tokencount; int labelcount; int linecount; int tempcount; char *keytab[19]={"","","begin","end","do","while","if","then",";","(",")","+","-","*","/",":=",">","<"}; char *term(); char *expr(); char *factor(); void statementlist(char *nextlabel); void statement(); char *condition(); char *newtemp() { char *temp; temp=(char *)malloc(8); tempcount++; itoa(tempcount,temp+1,10); temp[0]='t'; return temp; } char *newlabel() { char *label; label=(char *)malloc(8); labelcount++; itoa(labelcount,label+1,10); label[0]='L'; return label; } void scaner() { char ch; char temp[10]; int i,k; while(scanf("%c",&ch)!=EOF) { i=0; while(ch==' '||ch=='/n'||ch=='/t') scanf("%c",&ch); temp[i++]=ch; if(ch=='.') break; while(ch=getchar()) if(ch!=' '&&ch!='/n'&&ch!='t') temp[i++]=ch; else break; temp[i++]='/0'; strcpy(in0[tokencount].value,temp); for(k=2;k<18;k++) if(strcmp(temp,keytab[k])==0) { in0[tokencount++].symbol=k+1; break; } if(k==18) { if(isdigit(temp[0])) in0[tokencount++].symbol=1; else in0[tokencount++].symbol=2; } } } void gen(char *op,char *arg1,char *arg2,char *result) { printf("%s %s %s %s/n",op,arg1,arg2,result); } char *expr() { char *op,*arg1,*arg2,*result; op=(char *)malloc(10); arg1=(char *)malloc(10); arg2=(char *)malloc(10); result=(char *)malloc(10); arg1=term(); strcpy(result,arg1); while(looktoken.symbol==13||looktoken.symbol==12) { strcpy(op,looktoken.value); strcpy(arg2,term()); strcpy(result,newtemp()); char *L1=newlabel(); printf("%d %s ",linecount++,L1); gen(op,arg1,arg2,result); } return result; } char *term() { char *op,*arg1,*arg2,*result; op=(char *)malloc(10); arg1=(char *)malloc(10); arg2=(char *)malloc(10); result=(char *)malloc(10); arg1=factor(); strcpy(result,arg1); while(looktoken.symbol==14||looktoken.symbol==15) { strcpy(op,looktoken.value); strcpy(arg2,term()); strcpy(result,newtemp()); char *L1=newlabel(); printf("%d %s ",linecount++,L1); gen(op,arg1,arg2,result); } return result; } char *factor() { char *tt; tt=(char *)malloc(10); strcpy(tt," "); looktoken=in0[tokencount]; if(looktoken.symbol==1||looktoken.symbol==2) { strcpy(tt,looktoken.value); tokencount++; looktoken=in0[tokencount++]; } else { if(looktoken.symbol==10) { looktoken=in0[tokencount++]; tt=expr(); if(looktoken.symbol==11) { looktoken=in0[tokencount++];} else printf("error/n"); } else return looktoken.value; } return tt; } char *condition(char *nextlabel) { char *expr1,*expr2,*op; expr1=(char *)malloc(10); expr2=(char *)malloc(10); op=(char *)malloc(10); expr1=expr(); strcpy(op,looktoken.value); expr2=expr(); char *L1=newlabel(); printf("%d %s ",linecount++,L1); char *tt=newtemp(); gen(op,expr1,expr2,tt); return tt; } void statement(char *nextlabel) { looktoken=in0[tokencount++]; if(looktoken.value[0]=='.') {looktoken.symbol=0;return ;} switch(looktoken.symbol) { case 2: //ident := stament { char *result,*arg1; result=(char *)malloc(10); arg1=(char *)malloc(10); strcpy(result,looktoken.value); looktoken=in0[tokencount++]; if(looktoken.symbol==16) // := { arg1=expr(); char *L1=newlabel(); printf("%d %s ",linecount++,L1); gen("=",arg1," ",result); } break; } case 6: // while condition do stament { char *L1=newlabel(); char *L2=newlabel(); char *result; result=(char *)malloc(10); printf("%d %s/n",linecount++,L1); result=condition(L1); printf("%d if %s is true goto %s/n",linecount++,result,L2); printf("%d else goto %s/n",linecount++,nextlabel); printf("%d %s/n",linecount++,L2); statement(L2); printf("%d goto %s/n",linecount++,L1); printf("%d %s/n",linecount++,nextlabel); break; } case 3: //begin statement-list end { statementlist(nextlabel);break; } case 7: //if condition then statement { char *L1=newlabel(); char *tt=condition(L1); printf("%d if %s is true goto %s/n",linecount++,tt,L1); printf("%d else goto %s /n",linecount++,nextlabel); printf("%d %s/n",linecount++,L1); statement(nextlabel); printf("%d %s/n",linecount++,nextlabel); break; } default:break; } } void statementlist(char *nextlabel) { statement(nextlabel); looktoken=in0[tokencount++]; while(looktoken.symbol!=4) //end { tokencount--; statement(newlabel()); looktoken=in0[tokencount++]; } } void block(char *nextlabel) { statement(nextlabel); while(looktoken.symbol==9) { statement(newlabel()); } } int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scaner(); tokencount=0; char *L1=newlabel(); block(L1); return 0; }