0% found this document useful (0 votes)
11 views10 pages

Compiler 12,13,14

The document contains multiple C program codes that demonstrate the generation of intermediate code, dead code elimination, and optimization techniques. It includes functionalities for reading expressions from a file, processing them, and producing optimized assembly-like output. The programs utilize structures and functions to manage variables and operations efficiently.

Uploaded by

MAHESH V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Compiler 12,13,14

The document contains multiple C program codes that demonstrate the generation of intermediate code, dead code elimination, and optimization techniques. It includes functionalities for reading expressions from a file, processing them, and producing optimized assembly-like output. The programs utilize structures and functions to manage variables and operations efficiently.

Uploaded by

MAHESH V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Program Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct three {
char data[10], temp[7];
} s[30];

int main() {
char d1[7], d2[7] = "t";
int i = 0, j = 1, len = 0;
FILE *f1; f1 = fopen("code.txt", "r");

while (fscanf(f1, "%s", s[len].data) != EOF) {


len++;
}

fclose(f1);
itoa(j, d1, 10);
strcat(d2, d1);
strcpy(s[j].temp, d2);
strcpy(d1, "");
strcpy(d2, "t");

if (!strcmp(s[3].data, "+")) {
printf("%s = %s + %s\n", s[j].temp, s[i+2].data, s[i+4].data);
} else if (!strcmp(s[3].data, "-")) {
printf("%s = %s - %s\n", s[j].temp, s[i+2].data, s[i+4].data);
} else if (!strcmp(s[3].data, "*")) {
printf("%s = %s * %s\n", s[j].temp, s[i+2].data, s[i+4].data);
} else if (!strcmp(s[3].data, "/")) {
printf("%s = %s / %s\n", s[j].temp, s[i+2].data, s[i+4].data);
}
j++;

for (i = 4; i < len - 2; i += 2) {


itoa(j, d1, 10);
strcat(d2, d1);
strcpy(s[j].temp, d2);

if (!strcmp(s[i+1].data, "+")) {
printf("%s = %s + %s\n", s[j].temp, s[j-1].temp, s[i+2].data);
} else if (!strcmp(s[i+1].data, "-")) {
printf("%s = %s - %s\n", s[j].temp, s[j-1].temp, s[i+2].data);
} else if (!strcmp(s[i+1].data, "*")) {
printf("%s = %s * %s\n", s[j].temp, s[j-1].temp, s[i+2].data);
} else if (!strcmp(s[i+1].data, "/")) {
printf("%s = %s / %s\n", s[j].temp, s[j-1].temp, s[i+2].data);
}
strcpy(d1, "");
strcpy(d2, "t");
j++;
}

printf("%s = %s\n", s[0].data, s[j-1].temp);


getch();
}
Input File:

Output:

Result:
Program Code:

#include <stdio.h>
#include <string.h>
struct op {
char l;
char r[20];
};

int main() {
int i, j, n, z = 0;
struct op op[10], pr[10];
printf("Enter the Number of Expressions: ");
scanf("%d", &n);
printf("\n");
for (i = 0; i < n; i++) {
printf("left: ");
scanf(" %c", &op[i].l);
printf("right: ");
scanf(" %s", op[i].r);
}

printf("\nIntermediate Code\n\n");
for (i = 0; i < n; i++) {
printf("%c = %s\n", op[i].l, op[i].r);
}

for (i = 0; i < n; i++) {


for (j = 0; j < n; j++) {
if (i != j && strchr(op[j].r, op[i].l)) {
pr[z++] = op[i];
break;
}
}
}

printf("\nAfter Dead Code Elimination\n\n");


for (i = 0; i < z; i++) {
printf("%c = %s\n", pr[i].l, pr[i].r);
}

for (i = 0; i < z; i++) {


for (j = i + 1; j < z; j++) {
if (strcmp(pr[i].r, pr[j].r) == 0) {
pr[j].l = '\0';
}
}
}

printf("\nOptimized Code\n\n");
for (i = 0; i < z; i++) {
if (pr[i].l != '\0') {
printf("%c = %s\n", pr[i].l, pr[i].r);
}
}
return 0;
}
Output:

Result:
Program Code:
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef struct {
char var[10];
int alive;
} regist;
regist preg[10];
void substring(char exp[], int st, int end) {
int i, j = 0;
char dup[10] = "";
for (i = st; i < end; i++)
dup[j++] = exp[i];
dup[j] = '\0';
strcpy(exp, dup);}
int getregister(char var[]) {
int i;
for (i = 0; i < 10; i++) {
if (preg[i].alive == 0) {
strcpy(preg[i].var, var);
break;}}
return i;}
void getvar(char exp[], char v[]) {
int i, j = 0;
char var[10] = "";
for (i = 0; exp[i] != '\0'; i++) {
if (isalpha(exp[i]))
var[j++] = exp[i];
else
break;}
var[j] = '\0';
strcpy(v, var);}
int main() {
char basic[10][10], var[10][10], fstr[10], op;
int i, j, k, reg, vc = 0, flag = 0;
printf("\nEnter the Three Address Code:\n");
for (i = 0;; i++) {
gets(basic[i]);
if (strcmp(basic[i], "exit") == 0)
break; }
printf("\nThe Equivalent Assembly Code is:\n");
for (j = 0; j < i; j++) {
getvar(basic[j], var[vc++]);
strcpy(fstr, var[vc - 1]);
substring(basic[j], strlen(var[vc - 1]) + 1, strlen(basic[j]));
getvar(basic[j], var[vc++]);
reg = getregister(var[vc - 1]);
if (preg[reg].alive == 0) {
printf("\nMov R%d,%s", reg, var[vc - 1]);
preg[reg].alive = 1;
}
op = basic[j][strlen(var[vc - 1])];
substring(basic[j], strlen(var[vc - 1]) + 1, strlen(basic[j]));
getvar(basic[j], var[vc++]);
switch (op) {
case '+':
printf("\nAdd");
break;
case '-':
printf("\nSub");
break;
case '*':
printf("\nMul");
break;
case '/':
printf("\nDiv");
break;
}
flag = 1;
for (k = 0; k <= reg; k++) {
if (strcmp(preg[k].var, var[vc - 1]) == 0) {
printf(" R%d, R%d", k, reg);
preg[k].alive = 0;
flag = 0;
break;
}
}
if (flag) {
printf(" %s,R%d", var[vc - 1], reg);
printf("\nMov %s,R%d", fstr, reg);
}
strcpy(preg[reg].var, var[vc - 3]);
}
return 0;
}
Output:

Result:

You might also like