KAKATIYA INSTITUTE OF TECHNOLOGY AND SCIENCE,WARANGAL
U18IT505-COMPILERS
ASSIGNMENT-2
NAME : D.Sathwika
ROLL.NO : B22IT031
CLASS : 5IT1
➢ Write a C/JAVA/PYTHON program to implement implicit and explicit type
conversion.
Sol:
#include <stdio.h>
int main() {
int num1 = 10;
double num2 = 3.5;
double result1 = num1 + num2;
printf("Implicit Conversion:\n");
printf("Result of adding int and double: %lf\n", result1);
double num3 = 7.8;
int result2;
result2 = (int) num3;
printf("\nExplicit Conversion:\n");
printf("Result of casting double to int: %d\n", result2);
return 0;
}
➢ Write a C/JAVA/PYTHON program to implement any one storage
allocation strategies
sol:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int i;
printf("Static Memory Allocation:\n");
for (i = 0; i < 5; i++) {
printf("Element at index %d: %d\n", i, arr[i]);
}
return 0;
}
➢ Write a C/JAVA/PYTHON program to implement symbol table
Sol:
#include <stdio.h>
#include <string.h>
#define SIZE 10
struct Symbol {
char name[50];
int address;
};
struct Symbol symbolTable[SIZE];
int count = 0; // Number of symbols in the table
void insert(char name[], int address) {
if (count < SIZE) {
strcpy(symbolTable[count].name, name);
symbolTable[count].address = address;
count++;
} else {
printf("Symbol table is full!\n");
}
}
void display() {
printf("Symbol Table:\n");
printf("Name\tAddress\n");
for (int i = 0; i < count; i++) {
printf("%s\t%d\n", symbolTable[i].name, symbolTable[i].address);
}
}
int main() {
insert("x", 100);
insert("y", 104);
insert("z", 108);
display();
return 0;
}
➢ Write a C/JAVA/PYTHON program to implement simple code generator
Sol:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
void generateCode(char* expression) {
int i, j = 0, tempVar = 1;
char lhs, rhs, operator;
char temp[2];
printf("Three Address Code:\n");
for (i = 0; expression[i] != '\0'; i++) {
if (isOperator(expression[i])) {
operator = expression[i];
lhs = expression[i - 1];
rhs = expression[i + 1];
printf("t%d = %c %c %c\n", tempVar, lhs, operator, rhs);
expression[i - 1] = 't';
temp[0] = tempVar + '0';
temp[1] = '\0';
strcat(expression, temp);
i += 1;
tempVar++;
}
}
}
int main() {
char expression[100];
printf("Enter an arithmetic expression (e.g., a+b, x*y): ");
scanf("%s", expression);
generateCode(expression);
return 0;
}
➢ Write a C/JAVA/PYTHON program to implement code optimization
Techniques
Sol:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void constantFolding(char* expression) {
int result = 0;
char operator = ' ';
int num;
printf("Optimized Code:\n");
char *token = strtok(expression, " ");
while (token != NULL) {
if (isdigit(token[0])) {
num = atoi(token); // Convert to integer
if (operator == ' ') {
result = num; // First number
} else {
if (operator == '+') result += num;
else if (operator == '-') result -= num;
else if (operator == '*') result *= num;
else if (operator == '/') result /= num;
}
} else {
operator = token[0];
}
token = strtok(NULL, " "); // Move to the next token
}
printf("Result: %d\n", result);
}
int main() {
char expression[100];
printf("Enter a mathematical expression (e.g., 3 + 5 * 2): ");
fgets(expression, sizeof(expression), stdin);
expression[strcspn(expression, "\n")] = 0; // Remove newline character
constantFolding(expression);
return 0;
}