You are on page 1of 5

Course name: Compiler Design Lab

Course Code: CSE332

Lab Report on:


Write a C program that takes user string input and shows the identifier in
the symbol table.

Submitted To:
Fahim Faisal
Lecturer
Department of Computer Science and Engineering
Daffodil International University
Submitted By:
Name: Sabrina Sharmin
ID: 181-15-961
Section: 59_A1
Program: CSE

Date of Submission: 17th March 2024


Lab Report no: 06

Title: Write a C program that takes user string input and shows the identifier in the
symbol table.

Objective:
Create a program that generates a symbol table from a given $-terminated
statement.and identify and classify symbols as either identifiers or operators.Also
allocate memory dynamically for identifiers and operators, to display the symbol table
with the symbol name, memory address, and type.

Program Logic:

● Initialize variables and arrays to store the input statement, tokens, and symbol
information.
● Prompt the user to enter a $-terminated statement.
● Read the input statement and extract tokens delimited by spaces or the $
terminator.
● Iterate through each token to classify it as an identifier or operator.
● Allocate memory dynamically for identifiers and operators using the malloc
function.
● Print the symbol table with the symbol name, memory address, and type.

Procedure:

● The program prompts the user to enter a statement terminated by $.


● The statement is read and tokens are extracted.
● Each token is analyzed:If it begins with an alphanumeric character, it is classified
as an identifier. And if it is an operator (+, -, *, /, =), it is classified as an operator.
● Memory is allocated for each identifier and operator using malloc.
● The symbol table is displayed, showing the symbol name, memory address, and
type.
Source code:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

int main() {
void *pt;
char str[50], tokens[10][10];
int n = 0, i = 0, j = 0, token_number = 0, char_index = 0, firstCharValid = 0;

printf("Enter a $-terminated statement: ");


fgets(str, sizeof(str), stdin);
n = strlen(str);
printf("Entered statement: ");

for (i = 0; i < n - 2; i++) {


printf("%c", str[i]);
}

for (i = 0; i < n; i++) {


if (!isspace(str[i])) {
if (str[i] == '$') {
tokens[token_number][char_index] = '\0';
token_number++;
char_index = 0;
}
else {
tokens[token_number][char_index] = str[i];
char_index++;
}
}
else {
tokens[token_number][char_index] = '\0';
token_number++;
char_index = 0;
}
}

printf("\n\n-------------------------------------\n");
printf("| Symbol Table |\n");
printf("-------------------------------------\n");
printf("| Symbol | Address | Type |\n");
printf("-------------------------------------\n");
for (i = 0; i < token_number; i++) {
for (j = 0; tokens[i][j] != '\0'; j++) {
if (isalnum(tokens[i][j]) && firstCharValid == 0) {
pt = malloc(tokens[i][j]);
firstCharValid = 1;
printf("| %s | %#010x | Identifier |\n", tokens[i], pt);
}
else if (tokens[i][j] == '+' || tokens[i][j] == '-' || tokens[i][j] == '*' || tokens[i][j] == '/' ||
tokens[i][j] == '=') {
pt = malloc(tokens[i][j]);
printf("| %c | %#010x | Operator |\n", tokens[i][j], pt);
}
}

firstCharValid = 0;
}

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

return 0;
}

Result:
Conclusion:

The "Symbol Table Generator" program efficiently analyzes a $-terminated statement to


generate a symbol table. It effectively distinguishes between identifiers and operators,
allocating memory dynamically for each symbol. By displaying the symbol table, the
program provides a clear overview of the symbols present in the input statement, along
with their corresponding memory addresses and types.

You might also like