0% found this document useful (0 votes)
45 views13 pages

CompilerDesign LAB1 22BCE1778

The document outlines an assignment for a Compiler Design Lab where the task is to implement a Lexical Analyzer in C that generates tokens from C++ programs and prints a symbol table. It includes code for the lexer, which identifies keywords, constants, and identifiers, and adds them to a symbol table. Additionally, two C++ programs are provided as examples for analysis.
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)
45 views13 pages

CompilerDesign LAB1 22BCE1778

The document outlines an assignment for a Compiler Design Lab where the task is to implement a Lexical Analyzer in C that generates tokens from C++ programs and prints a symbol table. It includes code for the lexer, which identifies keywords, constants, and identifiers, and adds them to a symbol table. Additionally, two C++ programs are provided as examples for analysis.
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

Compiler Design Lab

Assignment 1

Name : Jayampu Akash Siva Teja

Reg.No : 22BCE1778

Write a program in C to implement Lexical Analyzer which generates


tokens for given input C++ programs and also print the symbol table which
has been generated during the lexical analysis.

C Code - lexer.c :
#include <stdio.h>

#include <string.h>

#include <ctype.h>

#include <stdlib.h>

#define MAX 100

char keywords[32][10] = {

"auto", "break", "case", "char", "const", "continue", "default",

"do", "double", "else", "enum", "extern", "float", "for", "goto",

"if", "int", "long", "register", "return", "short", "signed",

"sizeof", "static", "struct", "switch", "typedef", "union",

"unsigned", "void", "volatile", "while"

};

struct Symbol {

char name[100];

char type[20];

} symbolTable[MAX];

int symbolCount = 0;
int isKeyword(char *str) {

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

if (strcmp(str, keywords[i]) == 0)

return 1;

return 0;

int isSymbolPresent(char *str) {

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

if (strcmp(symbolTable[i].name, str) == 0)

return 1;

return 0;

void addToSymbolTable(char *str, char *type) {

if (!isSymbolPresent(str)) {

strcpy(symbolTable[symbolCount].name, str);

strcpy(symbolTable[symbolCount].type, type);

symbolCount++;

void printSymbolTable() {

printf("\nSymbol Table:\n");

printf("------------------------\n");

printf("%-15s %-10s\n", "Identifier", "Type");

printf("------------------------\n");

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

printf("%-15s %-10s\n", symbolTable[i].name, symbolTable[i].type);

}
int main() {

char filename[50], ch, buffer[100];

FILE *fp;

int i = 0;

printf("Enter the file name to analyze: ");

scanf("%s", filename);

fp = fopen(filename, "r");

if (!fp) {

printf("Error opening file.\n");

return 1;

printf("\nTokens:\n");

while ((ch = fgetc(fp)) != EOF) {

if (isalnum(ch) || ch == '_') {

buffer[i++] = ch;

} else {

if (i != 0) {

buffer[i] = '\0';

i = 0;

if (isKeyword(buffer))

printf("<KEYWORD>\t%s\n", buffer);

else if (isdigit(buffer[0]))

printf("<CONSTANT>\t%s\n", buffer);

else {

printf("<IDENTIFIER>\t%s\n", buffer);

addToSymbolTable(buffer, "IDENTIFIER");

}
if (ch == ' ' || ch == '\n' || ch == '\t')

continue;

else if (ispunct(ch))

printf("<PUNCTUATION>\t%c\n", ch);

fclose(fp);

printSymbolTable();

return 0;

}
Program1

#include <iostream>
using namespace std;

int main() {
int i, n;
bool is_prime = true;
cout << "Enter a positive integer: ";
cin >> n;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
is_prime = false;
}
// loop to check if n is prime
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";
return 0;
}
Output for program1.cpp:
Program 2

#include <iostream>
// Function to swap two integer values
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
// Function to sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
std::cout << "Original array:";
printArray(arr, n);
bubbleSort(arr, n);
std::cout << "Sorted array (ascending):";
printArray(arr, n);
return 0;
}
Output for program2.cpp:

You might also like