0% found this document useful (0 votes)
9 views3 pages

CD Exp19

The document describes an experiment for implementing intermediate code generation for simple expressions using the gcc compiler. It includes a C program that reads an expression, identifies operators, and generates intermediate code. The program utilizes functions to find operators and explore the expression for code generation, displaying the results in a formatted manner.

Uploaded by

kpu21195
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)
9 views3 pages

CD Exp19

The document describes an experiment for implementing intermediate code generation for simple expressions using the gcc compiler. It includes a C program that reads an expression, identifies operators, and generates intermediate code. The program utilizes functions to find operators and explore the expression for code generation, displaying the results in a formatted manner.

Uploaded by

kpu21195
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

Experiment Name: Intermediate code generation

Experiment No. : 19 Date :


Compiler : gcc file name: inte.c
Aim : Implement intermediate code generation for simple expressions.

PROGRAM

#include <stdio.h>
#include <string.h>

int i = 1, j = 0, no = 0, tmpch = 90;


char str[100], left[15], right[15];

void findopr();
void explore();
void fleft(int);
void fright(int);

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:\n");
findopr();
explore();
}

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

void explore() {
i = 1;
while (k[i].op != '\0') {
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos] = tmpch--;
printf("%c := %s %c %s\t", str[k[i].pos], left, k[i].op, right);
printf("\n");
i++;
}
fright(-1);
if (no == 0) {
fleft(strlen(str));
printf("\t%s := %s\n", right, left);
exit(0);
}
printf("\t%s := %c\n", right, str[k[i - 1].pos]);
}

void fleft(int x) {
int w = 0, flag = 0;
x--;
while (x != -1 && str[x] != '+' && str[x] != '*' && str[x] != '=' && str[x] != '-' && str[x] != '/' && str[x]
!= '\0' && 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 (x != -1 && str[x] != '+' && str[x] != '*' && str[x] != '=' && str[x] != '-' && str[x] != '/' && str[x]
!= '\0' && str[x] != ':') {
if (str[x] != '$' && flag == 0) {
right[w++] = str[x];
right[w] = '\0';
str[x] = '$';
flag = 1;
}
x++;
}
}

OUTPUT

You might also like