From f2a0fbd1bc8dc171034e99eb81ce053bddc21797 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 19:07:21 -0500 Subject: [PATCH] added a simple test of symbol table and entry creation --- symbol_table.c | 148 +++++++++++++++++++++++----------------- symbol_table.h | 34 +++++++++ test_comment_issues.tok | 0 3 files changed, 118 insertions(+), 64 deletions(-) delete mode 100644 test_comment_issues.tok diff --git a/symbol_table.c b/symbol_table.c index 70cb359..2c3f3a2 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -2,84 +2,104 @@ //Using a Linked List Structure. Head of linked List points to parent scope (if one exists) //Tail of Linked List points to a Linked List of all the child scopes //T -#include "symbol_table.h" +//*#include "symbol_table.h" +/* +#include +#include +#include typedef struct ListOfTable{ - struct SymbolTable* table; - //struct ListOfTable* prev; - struct ListOfTable* next; + struct SymbolTable* table; + //struct ListOfTable* prev; + struct ListOfTable* next; }ListOfTable; typedef union Value{ - int* value_of_int; + int* value_of_int; void* value_of_pointer; bool* value_of_bool; char* value_of_char; }Value; - + typedef struct TableNode{ - string* theType; - string* theName; - Value* value; - struct TableNode* next; - //this next value is an int for string types to tell you how far to traverse a buffer for the string - int StrLength; -}TableNode; - + char* theType; + char* theName; + Value* value; + struct TableNode* next; + //this next value is an int for string types to tell you how far to traverse a buffer for the string + int StrLength; + }TableNode; + typedef struct SymbolTable{ - TableNode* entries; - struct SymbolTable* Parent_Scope; - struct ListOfTable* Children_Scope; - int Line_Number; + TableNode* entries; + struct SymbolTable* Parent_Scope; + struct ListOfTable* Children_Scope; + int Line_Number; int Column_Number; -}SymbolTable; + }SymbolTable; +*/ + 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->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; + 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; - } - - return table; -} - -TableNode* CreateEntry(SymbolTable* table, string typeOf, string id, Value value, int StringLength){ - if(table->entries == NULL){ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->StrLength = StringLength; - return newEntry; - } else{ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->StrLength = StringLength; - TableNode* oldEntry = table->entries; - table->entries = newEntry; - newEntry->next = oldEntry; - return newEntry; - } - } - - + } + return table; + } + + +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ + if(table->entries == NULL){ + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->value = value; + newEntry->StrLength = StringLength; + return newEntry; + } else{ + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->value = value; + newEntry->StrLength = StringLength; + 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"; + Value* ofX = (Value*)malloc(sizeof(Value)); + ofX->value_of_char = 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,ofX,-1); + printf("The value of the first entry is %s\n",First_Entry->value->value_of_char); + return 0; + } +*/ diff --git a/symbol_table.h b/symbol_table.h index e69de29..539f2e4 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -0,0 +1,34 @@ +#include +#include +#include + +typedef struct ListOfTable{ + struct SymbolTable* table; + //struct ListOfTable* prev; + struct ListOfTable* next; + +}ListOfTable; + +typedef union Value{ + int* value_of_int; + void* value_of_pointer; + bool* value_of_bool; + char* value_of_char; + }Value; + +typedef struct TableNode{ + char* theType; + char* theName; + Value* value; + struct TableNode* next; + //this next value is an int for string types to tell you how far to traverse a buffer for the string + int StrLength; +}TableNode; + +typedef struct SymbolTable{ + TableNode* entries; + struct SymbolTable* Parent_Scope; + struct ListOfTable* Children_Scope; + int Line_Number; + int Column_Number; +}SymbolTable; diff --git a/test_comment_issues.tok b/test_comment_issues.tok deleted file mode 100644 index e69de29..0000000