0% found this document useful (0 votes)
9 views

CD Lab1

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CD Lab1

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

BCSE307P - COMPILER DESIGN LAB

ASSESSMENT – 1

Name: Anvita Manne

Register no.: 22bce0272

Lab Slot: 19+20

Date: 05.08.2024

1. Write a C/C++ Program to simulate the lexical analysis phase of a compiler using
files.
CODING:
Filename:lexical_analyzer.c
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>

bool isKeyword(char* str) {


const char* keywords[] = {
"if", "else", "while", "do", "break", "continue", "int", "double",
"float", "return", "char", "case", "sizeof", "long", "short",
"typedef", "switch", "unsigned", "void", "static", "struct", "goto"
};
int numKeywords = sizeof(keywords) / sizeof(keywords[0]);
for (int i = 0; i < numKeywords; i++) {
if (strcmp(str, keywords[i]) == 0) {
return true;
}
}
return false;
}

bool isOperator(char ch) {


return (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' || ch == '=');
}

bool isDelimiter(char ch) {


return (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}');
}

bool isIdentifier(char* str) {


return !(str[0] >= '0' && str[0] <= '9') && !isDelimiter(str[0]);
}

bool isInteger(char* str) {


int len = strlen(str);
if (len == 0) return false;
for (int i = 0; i < len; i++) {
if (!isdigit(str[i]) || (str[i] == '-' && i > 0)) {
return false;
}
}
return true;
}

bool isRealNumber(char* str) {


int len = strlen(str);
bool hasDecimal = false;
if (len == 0) return false;
for (int i = 0; i < len; i++) {
if ((!isdigit(str[i]) && str[i] != '.') || (str[i] == '-' && i > 0)) {
return false;
}
if (str[i] == '.') {
hasDecimal = true;
}
}
return hasDecimal;
}

char* subString(char* str, int left, int right) {


char* subStr = (char*)malloc(sizeof(char) * (right - left + 2));
strncpy(subStr, &str[left], right - left + 1);
subStr[right - left + 1] = '\0';
return subStr;
}

void parsestring(char* str) {


int left = 0, right = 0;
int len = strlen(str);

while (right <= len) {


// Move right pointer if not a delimiter
if (!isDelimiter(str[right])) {
right++;
}
// Process token when delimiter is found or at the end of the string
if (isDelimiter(str[right]) || right == len) {
if (left != right) {
char* subStr = subString(str, left, right - 1);

if (isKeyword(subStr)) {
printf("'%s' IS A KEYWORD\n", subStr);
} else if (isInteger(subStr)) {
printf("'%s' IS AN INTEGER\n", subStr);
} else if (isRealNumber(subStr)) {
printf("'%s' IS A REAL NUMBER\n", subStr);
} else if (isIdentifier(subStr)) {
printf("'%s' IS A VALID IDENTIFIER\n", subStr);
} else {
printf("'%s' IS NOT A VALID IDENTIFIER\n", subStr);
}

free(subStr);
}

// Check if current delimiter is an operator


if (isOperator(str[right])) {
printf("'%c' IS AN OPERATOR\n", str[right]);
}

left = ++right; // Move both pointers forward


}
}
}

int main() {
FILE* file = fopen("input.txt", "r"); // Open the input file in read mode
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}

char str[1000]; // Buffer for reading lines from the file


while (fgets(str, sizeof(str), file) != NULL) {
parsestring(str); // Parse each line from the file
}

fclose(file); // Close the file


return 0;
}
Input filename:input.txt
int a=a+2;

INPUT:
int a=a+2;

OUTPUT:
'int' IS A KEYWORD
'a' IS A VALID IDENTIFIER
'=' IS AN OPERATOR
'a' IS A VALID IDENTIFIER
'+' IS AN OPERATOR
'2' IS AN INTEGER

2. Write a C/C++ Program to create symbol table using files.


CODING:
Filename:symbol_table.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef enum {
KEYWORD, IDENTIFIER, SYMBOL, LITERAL
} TokenType;

typedef struct {
TokenType type;
char value[100];
} Token;

typedef struct {
char name[100];
char dataType[100];
} Variable;

int typeSizes[256];

void initializeTypeSizes() {
typeSizes['i'] = 4; // int
typeSizes['d'] = 8; // double
typeSizes['f'] = 4; // float
typeSizes['b'] = 1; // bool
typeSizes['c'] = 2; // char
}
Token* tokenize(const char* code, int* tokenCount) {
Token* tokens = malloc(100 * sizeof(Token));
*tokenCount = 0;
char currentToken[100];
int currentTokenIndex = 0;

for (int i = 0; code[i] != '\0'; i++) {


char c = code[i];
if (isspace(c)) {
if (currentTokenIndex > 0) {
currentToken[currentTokenIndex] = '\0';
Token token;
if (typeSizes[(int)currentToken[0]] > 0) {
token.type = KEYWORD;
} else {
token.type = IDENTIFIER;
}
strcpy(token.value, currentToken);
tokens[(*tokenCount)++] = token;
currentTokenIndex = 0;
}
} else if (isalnum(c) || c == '_') {
currentToken[currentTokenIndex++] = c;
} else {
if (currentTokenIndex > 0) {
currentToken[currentTokenIndex] = '\0';
Token token;
if (typeSizes[(int)currentToken[0]] > 0) {
token.type = KEYWORD;
} else {
token.type = IDENTIFIER;
}
strcpy(token.value, currentToken);
tokens[(*tokenCount)++] = token;
currentTokenIndex = 0;
}

if (ispunct(c)) {
Token token;
token.type = SYMBOL;
token.value[0] = c;
token.value[1] = '\0';
tokens[(*tokenCount)++] = token;
}
}
}

return tokens;
}

void create_SymbolTable(Token* tokens, int tokenCount) {


int address = 1000;
Variable variables[100];
int variableCount = 0;

printf("%-20s %-15s %-10s %-10s\n", "Symbol", "Type", "Size", "Address");


printf("%s\n", "-------------------------------------------------------");

for (int i = 0; i < tokenCount; i++) {


if (tokens[i].type == KEYWORD) {
Variable variable;
strcpy(variable.dataType, tokens[i].value);

if (i + 1 < tokenCount && tokens[i + 1].type == IDENTIFIER) {


strcpy(variable.name, tokens[i + 1].value);
printf("%-20s %-15s %-10d %-10d\n", variable.name, variable.dataType,
typeSizes[(int)variable.dataType[0]], address);

variables[variableCount++] = variable;
address += typeSizes[(int)variable.dataType[0]];
i++; // Skip the identifier
}
}
}
}

int main() {
initializeTypeSizes();

FILE* inputFile = fopen("input.c", "r");


if (!inputFile) {
printf("Error opening file!\n");
return 1;
}

fseek(inputFile, 0, SEEK_END);
long fileSize = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
char* code = malloc(fileSize + 1);
fread(code, 1, fileSize, inputFile);
code[fileSize] = '\0';
fclose(inputFile);

int tokenCount;
Token* tokens = tokenize(code, &tokenCount);
create_SymbolTable(tokens, tokenCount);
free(tokens);
free(code);
return 0;
}

Input filename:input.c
#include <stdio.h>
#include<math.h>

int main()
{
int length=10;
int breadth=20;
float area=length*breadth;
char a;
char p;
int perimeter=2*(length+breadth);
printf("area of rectangle %c= %f",a,area);
printf("perimeter is rectangle %p=%d",p,perimeter);
return 0;
}

INPUT:
length=10;
breadth=20;

OUTPUT:
Symbol Type Size Address
-------------------------------------------------------
main int 4 1000
length int 4 1004
area float 4 1008
a char 2 1012
p char 2 1014
perimeter int 4 1016
rectangle is 4 1020

You might also like