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

CD Lab

This document defines structures and functions for tokenizing, parsing, and generating symbol tables from C code. It includes struct definitions for nodes in a hash table and symbols in a symbol table. Functions are defined for checking keywords, operators, and data types. Additional functions insert elements into the hash table and symbol table, search the tables, and display the symbol table. Main executes preprocessing on a C file, inserts tokens into the hash table and symbols into the symbol table, and finally displays the populated symbol table.

Uploaded by

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

CD Lab

This document defines structures and functions for tokenizing, parsing, and generating symbol tables from C code. It includes struct definitions for nodes in a hash table and symbols in a symbol table. Functions are defined for checking keywords, operators, and data types. Additional functions insert elements into the hash table and symbol table, search the tables, and display the symbol table. Main executes preprocessing on a C file, inserts tokens into the hash table and symbols into the symbol table, and finally displays the populated symbol table.

Uploaded by

Bhavik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include "spaces.h"
#include "preprocess.h"
#define MAX_SIZE 20

char keywords[32][10] = {"auto", "double", "int", "struct",


"break", "else", "long", "switch", "case", "enum", "register",
"typedef", "char", "extern", "return", "union", "const", "float",
"short", "unsigned", "continue", "for", "signed", "void",
"default", "goto", "sizeof", "voltile", "do", "if", "static",
"while"}; // list of keywords

char operators[5] = {'+','-','/','%','*'};


char brackets[6] = {'(', ')', '[', ']', '{', '}'};
char data_types[][10] = {"double", "int", "char", "float"};
char data_type_buffer[100];
//element for hash table created:

typedef struct node{


char *cur;
int row,col;
struct node *next;
} *Node;

typedef struct symbol{


char *name;
char *data_type;
char *return_type;
struct symbol *next;
unsigned int arg;
char *Tame;

} *Symbol;

Symbol st[MAX_SIZE]; //symbol table created

Node hashTable[MAX_SIZE]; //hash table created

//to check for keyword


int compare(char buffer[]){
for(int i=0;i<32;i++){
if(strcmp(buffer, keywords[i]) == 0){
return 1;
}
}
return 0;
}

int isKeyword(char buffer[]){


for(int i=0;i<32;i++){
if(strcmp(buffer, keywords[i]) == 0){
return 1;
}
}
return 0;
}
int isDatatype(char buffer[]){
for(int i=0;i<4;i++){
if(strcmp(buffer, data_types[i]) == 0){
return 1;
}
}
return 0;
}

int isOperator(char ch){


for(int i=0;i<5;i++){
if(operators[i] == ch)
return 1;
}
return 0;
}

int isBracket(char ch){


for(int i=0;i<6;i++){
if(brackets[i] == ch)
return 1;
}
return 0;
}

//creating a hashing function


int hash(int size){
return (size % MAX_SIZE);
}

//searching in the hash table


int search(char buffer[]){
int index = hash(strlen(buffer));
if(hashTable[index] == NULL)
return 0;
Node cur = hashTable[index];
while(cur != NULL){
if(strcmp(cur->cur, buffer) == 0)
return 1;
cur = cur->next;
}
return 0;
}

//inserting elements in the hash table


void insert(char buffer[], int row, int col, int type){
if(type == 1){
if(search(buffer) == 0){
printf("<%s,%d,%d>\n", buffer, row, col);
int index = hash(strlen(buffer));
Node n = (Node)malloc(sizeof(struct node));
char *str = (char *)calloc(strlen(buffer) + 1, sizeof(char));
strcpy(str, buffer);
n->cur = str;
n->next = NULL;
n->row = row;
n->col = col;
if(hashTable[index] == NULL){
hashTable[index] = n;
return;
}
Node cur = hashTable[index];
while(cur->next != NULL){
cur = cur->next;
}
cur->next = n;
}
}
else{
printf("<%s,%d,%d>\n", buffer, row, col);
int index = hash(strlen(buffer));
Node n = (Node)malloc(sizeof(struct node));
char *str = (char *)calloc(strlen(buffer) + 1, sizeof(char));
strcpy(str, buffer);
n->cur = str;
n->next = NULL;
n->row = row;
n->col = col;
if(hashTable[index] == NULL){
hashTable[index] = n;
return;
}
Node cur = hashTable[index];
while(cur->next != NULL){
cur = cur->next;
}
cur->next = n;
}
}

int searchSymbolTable(char identifier[], char data_type[]){


int index = hash(strlen(identifier));
if(st[index] == NULL)
return -1;
Symbol cur = st[index];
int i = 0;
while(cur != NULL){
if(strcmp(identifier, cur->name) == 0)
return i;
cur = cur->next;
i++;
}
return -1;
}

//inserting in the symbol table


void insertSymbolTable(char identifier[], char type [],char data_type[],int arg){
if(searchSymbolTable(identifier, data_type) == -1){
Symbol n = (Symbol)malloc(sizeof(struct symbol));
n->data_type = "NULL";
n->return_type = "NULL";
char *str = (char *)calloc(strlen(identifier)+1, sizeof(char));
strcpy(str, identifier);
n->name = str;
n->next = NULL;
char *typee = (char *)calloc(strlen(data_type)+1, sizeof(char));
strcpy(typee, data_type);

if(type == "id"){
n->data_type = typee;
n->Tame = "Var";
}
else{
n->return_type = typee;
n->Tame = "Func";
}
if(strcmp(str,"printf")==0 || strcmp(str,"scanf")==0 ){
n->return_type = "NULL";
}
n->arg = arg;

int index = hash(strlen(identifier));


if(st[index] == NULL){
st[index] = n;
return;
}
Symbol cur = st[index];
while(cur->next != NULL)
cur = cur->next;
cur->next = n;
}
}
void displaySymbolTable(){
printf("\nSymbol table (in the format < Name , DType , Rtype, No. of args,
Tname > ): \n\n");
for(int i=0;i<MAX_SIZE;i++){
if(st[i] == NULL)
continue;
else{
Symbol cur = st[i];
while(cur){
printf(" %8s\t %8s\t %8s\t %8d\t %8s\t \n",cur->name, cur-
>data_type, cur->return_type, cur->arg, cur->Tame);
cur = cur->next;
}
}
}
}

int main(){
//executing space function from spaces.h
space();
//executing process function from preprocess.h
process();

for(int i=0;i<MAX_SIZE;i++)
hashTable[i] = NULL;

FILE *fin = fopen("process_output.c", "r");


if(fin == NULL){
printf("Cannot open file!\n");
return 0;
}
int n;
char buffer[100], c = 0;
int i = 0, row = 1, col_global = 1, col;
while(c != EOF){
if(isalpha(c) != 0 || c == '_'){
buffer[i++] = c;
col = col_global;
while(isalpha(c) != 0 || c == '_' || isdigit(c) != 0){
c = fgetc(fin);
col_global++;
if (isalpha(c) != 0 || c == '_' || isdigit(c) != 0)
buffer[i++] = c;
}
buffer[i] = '\0';

if(isDatatype(buffer) == 1){
insert(buffer, row, col, 1);
strcpy(data_type_buffer, buffer);
}
else if(isKeyword(buffer) == 1){
insert(buffer, row, col, 1);
}
else{
insert(buffer, row, col, 0);
if(c == '('){
n = ftell(fin);
int arg=0;
c = fgetc(fin);
if(c!=')'){
arg+=1;
while (c!=')'){
c=fgetc(fin);
if(c==',')
arg+=1;
}
fseek( fin, n-1, SEEK_SET );
}

insertSymbolTable(buffer,
"func",data_type_buffer,arg);

}else
insertSymbolTable(buffer, "id",data_type_buffer,0);
data_type_buffer[0] = '\0';
}

i = 0;
if(c == '\n')
row++, col_global = 1;
buffer[0] = '\0';
}
else if(isdigit(c) != 0){
buffer[i++] = c;
col = col_global;
while(isdigit(c) != 0 || c == '.'){
c = fgetc(fin);
col_global++;
if(isdigit(c) != 0 || c == '.')
buffer[i++] = c;
}
buffer[i] = '\0';
insert("num", row, col, 0); // numerical constant
i = 0;
if(c == '\n')
row++, col_global = 1;
buffer[0] = '\0';
c = fgetc(fin);
col_global++;
}
else if(c == '\"'){
col = col_global;
buffer[i++] = c;
c = 0;
while(c != '\"'){
c = fgetc(fin);
col_global++;
buffer[i++] = c;
}
buffer[i] = '\0';
insert("String", row, col, 0); // string literals
buffer[0] = '\0';
i = 0;
c = fgetc(fin);
col_global++;
}
else{
col = col_global;
if(c == '='){ // relational and logical operators
c = fgetc(fin);
col_global++;
if(c == '='){
insert("==", row, col, 1);
}
else{
insert("=", row, col, 1);
fseek(fin, -1, SEEK_CUR);
col_global--;
}
}
if(c == '>' || c == '<' || c == '!'){
c = fgetc(fin);
col_global++;
if(c == '='){
char temp_str[2] = {c, '='};
insert(temp_str, row, col, 1);
}
else{
char temp_str[1] = {c};
insert(temp_str, row, col, 1);
fseek(fin, -1, SEEK_CUR);
col_global--;
}
}
if(isOperator(c) == 1 || isBracket(c) == 1){ //parentheses
char temp_string[1] = {c};
insert(temp_string, row, col, 1);
}
if(c == '\n')
row++, col_global = 1;
c = fgetc(fin);
col_global++;
}

}
displaySymbolTable();
return 0;
}

You might also like