From 7836f6ecd0866854848950797d652f59c1be431a Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 01:18:22 -0500 Subject: [PATCH 1/4] started symbol table file --- symbol_table.c | 50 +++++++++++++++++++++++++++++++++++ symbol_table.h | 0 test_comment_issues.tok | 0 tests/test_comment_issues.tok | 0 4 files changed, 50 insertions(+) create mode 100644 symbol_table.c create mode 100644 symbol_table.h create mode 100644 test_comment_issues.tok create mode 100644 tests/test_comment_issues.tok diff --git a/symbol_table.c b/symbol_table.c new file mode 100644 index 0000000..31d2945 --- /dev/null +++ b/symbol_table.c @@ -0,0 +1,50 @@ +//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 Linked List of all the child scopes +//T +#include "symbol_table.h" + +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; + int* size_of_char_array; + }Value; + +typedef struct TableNode{ + string* theType; + string* theName; + Value* value; + struct TableNode* next; +}TableNode; + +typedef struct SymbolTable{ + TableNode* entries; + struct SymbolTable* Parent_Scope; + struct ListOfTable* Children_Scope; +}SymbolTable; + +SymbolTable* CreateScope(SymbolTable* ParentScope){ + SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); + table.Parent_Scope = ParentScope; + table.Children_Scope = NULL; + table.entries = NULL; + return table; +} + +Entry* CreateEntry(SymbolTable* table, string typeOf, string id, Value value){ + if( + + + + + + + diff --git a/symbol_table.h b/symbol_table.h new file mode 100644 index 0000000..e69de29 diff --git a/test_comment_issues.tok b/test_comment_issues.tok new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_comment_issues.tok b/tests/test_comment_issues.tok new file mode 100644 index 0000000..e69de29 From 5f35308361a2f594c0145aa45e5b55633a501170 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 15:19:50 -0500 Subject: [PATCH 2/4] continued working on table structure --- symbol_table.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/symbol_table.c b/symbol_table.c index 31d2945..196bf06 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -8,6 +8,8 @@ typedef struct ListOfTable{ struct SymbolTable* table; struct ListOfTable* prev; struct ListOfTable* next; + int Line_Number; + int Column_Number; }ListOfTable; typedef union Value{ @@ -15,7 +17,6 @@ typedef union Value{ void* value_of_pointer; bool* value_of_bool; char* value_of_char; - int* size_of_char_array; }Value; typedef struct TableNode{ @@ -23,6 +24,8 @@ typedef struct TableNode{ 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; typedef struct SymbolTable{ @@ -31,18 +34,23 @@ typedef struct SymbolTable{ struct ListOfTable* Children_Scope; }SymbolTable; -SymbolTable* CreateScope(SymbolTable* ParentScope){ +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; return table; } -Entry* CreateEntry(SymbolTable* table, string typeOf, string id, Value value){ - if( - - +TableNode* CreateEntry(SymbolTable* table, string typeOf, string id, Value value, int StringLength){ + if(table.entries == NULL){ + TableNode* newEntry = (TableNode*)malloc(sizeof(SymbolTable)); + newEntry.theType = typeOf; + newEntry.theName = id; + newEntry.StrLength = StringLength; + } else{ From b21d7039a2e2e68b86d6a49e1108f80a7c731617 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 17:50:25 -0500 Subject: [PATCH 3/4] finished base code for table setup. Still have to check grammar --- symbol_table.c | 57 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/symbol_table.c b/symbol_table.c index 196bf06..70cb359 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -6,10 +6,9 @@ typedef struct ListOfTable{ struct SymbolTable* table; - struct ListOfTable* prev; + //struct ListOfTable* prev; struct ListOfTable* next; - int Line_Number; - int Column_Number; + }ListOfTable; typedef union Value{ @@ -32,27 +31,55 @@ typedef struct SymbolTable{ TableNode* entries; struct SymbolTable* Parent_Scope; struct ListOfTable* Children_Scope; + int Line_Number; + int Column_Number; }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; + 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; + } + 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(SymbolTable)); - newEntry.theType = typeOf; - newEntry.theName = id; - newEntry.StrLength = 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; + } + } - From f2a0fbd1bc8dc171034e99eb81ce053bddc21797 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 19:07:21 -0500 Subject: [PATCH 4/4] 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