Professional Documents
Culture Documents
181-15-961 - 59 - A1 (Lab - 6) - Compiler - Design
181-15-961 - 59 - A1 (Lab - 6) - Compiler - Design
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
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:
#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("\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: