0% found this document useful (0 votes)
23 views

Intermediate Code

Uploaded by

ATHUL KRISHNA
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)
23 views

Intermediate Code

Uploaded by

ATHUL KRISHNA
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/ 3

#include <stdio.

h>
#include <string.h>
int i = 0, j = 0, tmpch = 90; // Initialize global variables
char str[100], left[15], right[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
void sort_operators(); // Function prototype for sorting operators

struct exp {
int pos;
char op;
} k[15];

void main() {
printf("\t\tINTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression :");
scanf("%s", str);
printf("The intermediate code:\t\tExpression\n");
findopr();
sort_operators(); // Sort operators by precedence
explore();
}

void findopr() {
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ':') {
k[j].pos = i;
k[j++].op = ':';
} else if (str[i] == '/') {
k[j].pos = i;
k[j++].op = '/';
} else if (str[i] == '*') {
k[j].pos = i;
k[j++].op = '*';
} else if (str[i] == '+') {
k[j].pos = i;
k[j++].op = '+';
} else if (str[i] == '-') {
k[j].pos = i;
k[j++].op = '-';
}
}
}

void sort_operators() {
// Bubble sort based on operator precedence
for (int p = 0; p < j - 1; p++) {
for (int q = 0; q < j - p - 1; q++) {
int precedence1 = (k[q].op == '+' || k[q].op == '-') ? 1 : 2;
int precedence2 = (k[q + 1].op == '+' || k[q + 1].op == '-') ? 1 : 2;

if (precedence1 < precedence2) {


// Swap k[q] and k[q + 1]
struct exp temp = k[q];
k[q] = k[q + 1];
k[q + 1] = temp;
}
}
}
}

void explore() {
i = 0; // Start with the first operator in sorted order
while (i < j) {
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos] = tmpch--;
printf("\t%c := %s%c%s\t\t", str[k[i].pos], left, k[i].op, right);

// Print the updated expression


for (int l = 0; str[l] != '\0'; l++) {
if (str[l] != '$') {
printf("%c", str[l]);
}
}
printf("\n");
i++;
}
// Print final result
printf("\t%c := %c\n", str[0], str[k[--i].pos]);
}

void fleft(int x) {
int w = 0, flag = 0;
x--;
while (x != -1 && str[x] != '+' && str[x] != '*' && str[x] != '=' && str[x] != '\0' && str[x] != '-' && str[x] != '/' &&
str[x] != ':') {
if (str[x] != '$' && flag == 0) {
left[w++] = str[x];
left[w] = '\0';
str[x] = '$';
flag = 1;
}
x--;
}
}

void fright(int x) {
int w = 0, flag = 0;
x++;
while (str[x] != '\0' && str[x] != '+' && str[x] != '*' && str[x] != '/' && str[x] != '-' && str[x] != ':') {
if (str[x] != '$' && flag == 0) {
right[w++] = str[x];
right[w] = '\0';
str[x] = '$';
flag = 1;
}
x++;
}
}
OUTPUT:
INTERMEDIATE CODE GENERATION
Enter the Expression :a+b*c+d
The intermediate code: Expression
Z := b*c a+Z+d
Y := a+Z Y+d
X := Y+d X
$ := X

=== Code Exited With Errors ===

You might also like