You are on page 1of 5

Ex No 04.

AIM

IMPLEMENTATION OF FRONT END OF A COMPILER (Arithmetic & Assignment Operator)

To implement front end of a compiler INPUT A regular expression(Arithmetic & Assignment operators) OUTPUT 3 address code in quadruple format ALGORITHM Step 1:Start the process Step 2: Get the regular expression as the input Step 3: Scan the input and generate the 3 address code *, / having higher precedence and +, while lower precedence

PROGRAM #include<stdio.h> #include<conio.h> void func(int); struct { char op,arg1[3],arg2[3],result[3]; }table[50]; char str[15][3]; int cnt=0,tcnt=0; void main() { char c; int i=0; clrscr(); printf("\n\nEnter The Expression:"); while((c=getch())!=13) { putch(c); if(c=='-'||c=='+'||c=='/'||c=='*'||c=='=') { str[cnt++][i]='\0'; str[cnt][0]=c; str[cnt++][1]='\0'; i=0; }

else str[cnt][i++]=c; } cnt++; while(cnt>3) { for(i=0;i<cnt;i++) if(str[i][0]=='*'||str[i][0]=='/') func(i); for(i=0;i<cnt;i++) if(str[i][0]=='+'||str[i][0]=='-') func(i); } strcpy(table[tcnt].result,str[0]); table[tcnt].op=str[1][0]; strcpy(table[tcnt].arg1,table[tcnt-1].result); strcpy(table[tcnt++].arg2,""); printf("\n\nPos\tOp\tArg1\tArg2\tRes\n\n"); for(i=0;i<tcnt;i++) printf("%d\t%c\t%s\t%s\t%s\n",i,table[i].op,table[i].arg1,table[i].arg2,table[i].result); printf("\n\n\n"); for(i=0;i<tcnt-1;i++) printf("%s=%s%c%s\n",table[i].result,table[i].arg1,table[i].op,table[i].arg2); printf("%s%c%s\n",table[i].result,table[i].op,table[i].arg1); getch(); } void func(int i) { int j,k; table[tcnt].op=str[i][0]; strcpy(table[tcnt].arg1,str[i-1]); strcpy(table[tcnt].arg2,str[i+1]); sprintf(table[tcnt].result,"t%d",tcnt);tcnt++; strcpy(str[i-1],table[tcnt-1].result); for(j=0;j<2;j++) { for(k=i;k<cnt;k++) strcpy(str[k],str[k+1]); cnt--; } }

SAMPLE OUTPUT: Enter the expression: a=b+c*d POS To T1 T2 T0=c*d T1+b+0 A=t1 OP * + + ARG1 c b t1 ARG2 d 0 RESULT t0 t1 a

RESULT: Thus the front end of a compiler is implemented by generating 3 address codes as input for the regular expression EX No :5 IMPLEMENTATION OF INTERMEDIATE CODE GENERATION AIM: To write a C program to implement the intermediate code for the given set of input expressions. ALGORITHM: 1. 2. 3. 4. 5. Start the program. Get the input expression from the user. Check the expressions for its validation. If it is invalid return the error message. Otherwise, for each computation store the result in the three address statement (store it in temporary variable say t1, t2, etc.,) . 6. Assign the final temporary value to the variable in which the result has to be stored. 7. Stop the program. PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> char exp[10][10],*ope; inti,j,n,s[10],flag2,flag3,r1,r2; void check(); void oper(); void main() { clrscr(); printf("\nEnter the No. of Expressions:\n"); scanf("%d",&n);

printf("\nEnter The Expression (In Three Address Code):\n"); for(i=0;i<n;i++) { scanf("%s",exp[i]); s[i]=i; } printf("\n\nGenerated Code Is:\n\n"); oper(); getch(); } void oper() { for(i=0;i<n;i++) { flag2=0; flag3=0; if(exp[i][3]=='*') ope="MUL"; if(exp[i][3]=='/') ope="DIV"; if(exp[i][3]=='+') ope="ADD"; if(exp[i][3]=='-') ope="SUB"; check(); printf("MOV R%d,%c\t(Result Moved To %c)\n",s[i],exp[i][0],exp[i][0]); } } void check() { for(j=0;j<i;j++) { if(exp[i][2]==exp[j][0]) { flag2=1; r1=s[j]; } if(exp[i][4]==exp[j][0]) { flag3=1; r2=s[j]; } } if(flag2==1&&flag3==1) { s[i]=0; printf("%s R%d,R%d\t(Result In R%d)\n",ope,r2,r1,r1); }

else if(flag2==1&&flag3!=1) { s[i]=r1; printf("%s %c,R%d\t(Result In R%d)\n",ope,exp[i][4],r1,r1); } else if(flag2!=1&&flag3==1) { s[i]=r2; printf("%s %c,R%d\t(Result In R%d)\n",ope,exp[i][2],r2,r2); } else { printf("MOV %c,R%d\t(%c Moved To R%d)\n",exp[i][2],s[i],exp[i][2],s[i]); printf("%s %c,R%d\t(Result In R%d)\n",ope,exp[i][4],s[i],s[i]); } } OUTPUT: RESULT: Thus the program for Implementing implementation of intermediate code generation was written and executed successfully.

You might also like