Kavi Assign7
Kavi Assign7
Consider the Three Address Code sequences and apply the following techniques to
optimize the code.
1. Constant folding
2. Algebraic identities
3. Strength reduction
4. Dead code elimination
Aim
To Develop an optimized code generator to generate optimized code for the three
address code.
Code
Input.txt
t1= 10 * 10
t2= a + 0
t3= b * 1
t4= c * d
t5= c**2
t6= c * d
t7= c * d
t8= a + 0
Intermediate.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int yylex();
typedef struct {
char *name;
char *expression;
} entry;
entry symbol_table[100];
int symbol_table_index = 0;
%}
%union {
int intval;
char *strval;
%left ADD
%left MULTIPLY
%right POWER
%%
input:
line:
if (index == -1) {
add_expression($1, $3);
} else {
expression:
NUMBER {
char buffer[12];
$$ = strdup(buffer);
| VARIABLE {
$$ = strdup($1);
$$ = strdup(buffer);
else {
char buffer[12];
$$ = strdup(buffer);
else {
%%
int main() {
yyparse();
return 0;
if (strcmp(symbol_table[i].expression, expr) == 0) {
return i;
return -1;
symbol_table[symbol_table_index].name = strdup(name);
symbol_table[symbol_table_index].expression = strdup(expr);
symbol_table_index++;
return 1;
return result;
Intermediate.l
%{
#include "optimize.tab.h"
%}
digit [0-9]+
variable [a-zA-Z][a-zA-Z0-9]*
%%
. { return yytext[0]; }
%%
int yywrap() {
return 1;
}
Output
Input 1 :
t1=5*3
t2=a+0
t3=b*1
t4=b*d
t5=d**2
t6=b*d
Input 2 :
t1= 10 * 10
t2= a + 0
t3= b * 1
t4= c * d
t5= c**2
t6= c * d
t7= c * d
t8= a + 0
Learning Outcome