0% found this document useful (0 votes)
19 views4 pages

A2 B22it031

The document contains an assignment for a programming course at Kakatiya Institute of Technology and Science, detailing various tasks related to compilers. It includes C code implementations for implicit and explicit type conversion, static memory allocation, symbol table creation, simple code generation, and code optimization techniques. Each section provides sample code along with explanations of the respective concepts.

Uploaded by

dasari Sathwika
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)
19 views4 pages

A2 B22it031

The document contains an assignment for a programming course at Kakatiya Institute of Technology and Science, detailing various tasks related to compilers. It includes C code implementations for implicit and explicit type conversion, static memory allocation, symbol table creation, simple code generation, and code optimization techniques. Each section provides sample code along with explanations of the respective concepts.

Uploaded by

dasari Sathwika
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/ 4

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;
}

You might also like