IMPLEMENTATION OF BACK END OF COMPILER

AIM:
TO write a C program to implement the back end of the Compiler.
ALGORITHM:
Step 1: Start the Program.
Step 2: Get the three variables from statements and stored in the text file k.txt.
Step 3: Compile the program and give the path of the source file
Step 4: Execute the program.
Step 5: Target code for the given statement was produced
Step 6: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
int i=2,j=0,k=2,ki=0;
char ip[10[, kk[10];
FILE *fp;
clrscr();
printf(“\n Enter the filename of the intermediate code”);
scanf(“%s”,&kk);
fp=fopen(kk,”r”);
if(fp==NULL)
{
printf(“\n Error in opening the file”);
getch();
}
clrscr();
while(!feof(fp))
{
fscanf(fp,”%s\n”,ip);
printf(“\t\t%s\n”,ip);
}
rewind(fp);
printf(“\n ………………..\n”);
printf(“\t Statement \t\t Targetcode\n”);
printf(“\n…………………\n”);
while(!feof(fp))
{
fscanf (fp,”%s”,ip);
printf(“\t%s”,ip);
printf(“\t\t MOV%c,R%d\n\t”,ip[i+k],j);
if(ip[i+1]==’+’)
printf(“\t\tADD”);
else
printf(“\t\t SUB”);
if(islower(ip[i]))
printf(“%c,R%d\n\n”,ip[i+k1],j);
else
printf(“%c,%c\n”,ip[i],ip[i+2]);
j++;
k1=2;
k=0;
}
printf(“\n…………….\n”);
getch();
fclose(fp);
}

OUTPUT:
Enter the file name of the intermediate code: k.txt
X=a-b
Y=a-c
Z=A+B
C=A-B
C=A-B
……………………………………
Statement Target Code
……………………………………

X=a-b MOV b,R0
SUB a,R0
Y=a-c MOV a,R1
SUB c,R1
Z=A+B MOV A,R2
ADD A,B
C=A-B MOV A,R3
SUB A,B
C=A-B MOV A,R4
SUB A,B
……………………………………..
RESULT:
Thus the program was written and executed successfully.