IMPLEMENTATION OF THE BACK END OF THE COMPILER
To write a program to implement the back end of the compiler.
- Start the program.
- Get the three variables from statements and stored in the text file k.txt.
- Compile the program and give the path of the source file.
- Execute the program.
- Target code for the given statement is produced.
- Stop the program.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main() {
int i = 2, j = 0, k = 2, k1 = 0;
char ip[10], kk[10];
FILE *fp;
printf("Enter the filename of the intermediate code: ");
scanf("%s", kk);
fp = fopen(kk, "r");
if (fp == NULL) {
printf("\nError in opening the file\n");
return 1;
}
printf("\nStatement\tTarget Code\n\n");
while (fscanf(fp, "%s", ip) != EOF) {
printf("%s\tMOV %c,R%d SUB ", ip, ip[i + k], j);
if (ip[i + 1] == '+')
printf("ADD ");
else
printf("SUB ");
if (islower(ip[i]))
printf("%c,R%d\n", ip[i + k1], j);
else
printf("%c,%c\n", ip[i], ip[i + 2]);
j++;
k1 = 2;
k = 0;
}
fclose(fp);
return 0;
}
The back end of the compiler is implemented successfully, and the output is verified.