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

Constant Propagation Experiment

The document describes an experiment on constant propagation using a C program. The program takes a set of arithmetic expressions as input, evaluates them if both operands are constants, and outputs the optimized code. It includes functions for input, constant evaluation, and output of the results.

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)
101 views3 pages

Constant Propagation Experiment

The document describes an experiment on constant propagation using a C program. The program takes a set of arithmetic expressions as input, evaluates them if both operands are constants, and outputs the optimized code. It includes functions for input, constant evaluation, and output of the results.

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

Experiment Name: Constant Propagation

Experiment No. : 18 Date :


Compiler : gcc file name: prop.c
Aim : Program to perform constant propagation.

PROGRAM
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

void input();
void output();
void change(int p, char *res);
void constant();

struct expr {
char op[2], op1[5], op2[5], res[5];
int flag;
} arr[10];

int n;

int main() {
input();
constant();
output();
return 0;
}

void input() {
int i;
printf("\nEnter the maximum number of expressions: ");
scanf("%d", &n);
printf("\nEnter the input (operation op1 op2 result):\n");
for (i = 0; i < n; i++) {
scanf("%s", arr[i].op);
scanf("%s", arr[i].op1);
scanf("%s", arr[i].op2);
scanf("%s", arr[i].res);
arr[i].flag = 0;
}
}

void constant() {
int i;
int op1, op2, res;
char op, res1[5];
for (i = 0; i < n; i++) {
if ((isdigit(arr[i].op1[0]) && isdigit(arr[i].op2[0])) || strcmp(arr[i].op, "=") == 0) {
op1 = atoi(arr[i].op1);
op2 = atoi(arr[i].op2);

op = arr[i].op[0];
switch (op) {
case '+':
res = op1 + op2;
break;
case '-':
res = op1 - op2;
break;
case '*':
res = op1 * op2;
break;
case '/':
if (op2 != 0)
res = op1 / op2;
else {
printf("Error: Division by zero\n");
return;
}
break;
case '=':
res = op1;
break;
}
sprintf(res1, "%d", res);
arr[i].flag = 1;
change(i, res1);
}
}
}

void output() {
int i = 0;
printf("\nOptimized code is:");
for (i = 0; i < n; i++) {
if (!arr[i].flag) {
printf("\n%s %s %s %s", arr[i].op, arr[i].op1, arr[i].op2, arr[i].res);
}
}
}

void change(int p, char *res) {


int i;
for (i = p + 1; i < n; i++) {
if (strcmp(arr[p].res, arr[i].op1) == 0)
strcpy(arr[i].op1, res);
else if (strcmp(arr[p].res, arr[i].op2) == 0)
strcpy(arr[i].op2, res);
}
}

OUTPUT

You might also like