Files
compiler-the-translators/symbol_table.c

62 lines
2.3 KiB
C

//Defining a symbol table
//Using a Linked List Structure. Head of linked List points to parent scope (if one exists)
//Tail of Linked List points to a List of child scopes
#include "symbol_table.h"
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
table->Line_Number = Line;
table->Column_Number = Column;
table->Parent_Scope = ParentScope;
table->Children_Scope = NULL;
table->entries = NULL;
if(ParentScope != NULL){
if(ParentScope->Children_Scope == NULL){
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
newEntry->next = NULL;
//newEntry->prev = NULL;
newEntry->table = table;
ParentScope->Children_Scope = newEntry;
} else{
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
//newEntry->prev = NULL;
newEntry->table= table;
ListOfTable* oldEntry = ParentScope->Children_Scope;
ParentScope->Children_Scope = newEntry;
newEntry->next = oldEntry;
}
}
return table;
}
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id){
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf;
newEntry->theName = id;
if(table->entries == NULL){
table->entries = newEntry;
return newEntry;
} else{
TableNode* oldEntry = table->entries;
table->entries = newEntry;
newEntry->next = oldEntry;
return newEntry;
}
}
//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation
int main(){
char* String = "STRING";
char* X = "X";
SymbolTable* Second = CreateScope(NULL, 2,2);
printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number);
TableNode* First_Entry = CreateEntry(Second,String,X);
printf("The type of the first entry is %s\n",First_Entry->theType);
return 0;
}