From 072182dbbc9c229742925615742637e38e1420cd Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Sat, 8 Mar 2025 14:08:14 -0500 Subject: [PATCH 1/2] Fixed spacing in the files t#NONE --- src/symbol_table.c | 212 ++++++++++++++++++++++----------------------- src/symbol_table.h | 24 ++--- 2 files changed, 118 insertions(+), 118 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index 47d731b..ebd06ee 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -8,115 +8,115 @@ #include 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; + 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; - } + 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; + } } TableNode* table_lookup(SymbolTable* table, char* x) { - TableNode* entrie = table->entries; - for (; entrie != NULL; entrie = entrie->next) { - if (!strcmp(entrie->theName, x)) { - return entrie; - } - } - return NULL; + TableNode* entrie = table->entries; + for (; entrie != NULL; entrie = entrie->next) { + if (!strcmp(entrie->theName, x)) { + return entrie; + } + } + return NULL; } TableNode* look_up(SymbolTable* table, char* x) { - if (table == NULL) { - return NULL; - } - TableNode* ret = table_lookup(table, x); - if (ret != NULL) { - return ret; - } - return look_up(table->Parent_Scope, x); + if (table == NULL) { + return NULL; + } + TableNode* ret = table_lookup(table, x); + if (ret != NULL) { + return ret; + } + return look_up(table->Parent_Scope, x); } void print_symbol_table(SymbolTable* table, FILE* file_ptr) { - if (table->Parent_Scope == NULL) { - fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", - "PARENT", "TYPE", "Extra annotation"); - } - TableNode* entrie = table->entries; - fprintf(file_ptr, - "-----------------:--------:--------:----------------------:---------" - "--------------------\n"); - int parant_scope = 0; - int current_scope = 0; - if (table->Parent_Scope != NULL) { - parant_scope = table->Parent_Scope->Line_Number * 1000 + - table->Parent_Scope->Column_Number; - current_scope = table->Line_Number * 1000 + table->Column_Number; - } else { - current_scope = 1001; - } + if (table->Parent_Scope == NULL) { + fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", + "PARENT", "TYPE", "Extra annotation"); + } + TableNode* entrie = table->entries; + fprintf(file_ptr, + "-----------------:--------:--------:----------------------:---------" + "--------------------\n"); + int parant_scope = 0; + int current_scope = 0; + if (table->Parent_Scope != NULL) { + parant_scope = table->Parent_Scope->Line_Number * 1000 + + table->Parent_Scope->Column_Number; + current_scope = table->Line_Number * 1000 + table->Column_Number; + } else { + current_scope = 1001; + } - for (; entrie != NULL; entrie = entrie->next) { - if (parant_scope == 0) { - fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", - entrie->theName, current_scope, entrie->theType, - "Extra annotation"); - } else { - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName, - current_scope, parant_scope, entrie->theType, "Extra annotation"); - } - } - if (table->Children_Scope != NULL) { - ListOfTable* node = table->Children_Scope; - for (; node != NULL; node = node->next) { - print_symbol_table(node->table, file_ptr); - } - } - if (table->Parent_Scope == NULL) { - fprintf(file_ptr, - "-----------------:--------:--------:----------------------:-------" - "----------------------\n"); - } + for (; entrie != NULL; entrie = entrie->next) { + if (parant_scope == 0) { + fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", + entrie->theName, current_scope, entrie->theType, + "Extra annotation"); + } else { + fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName, + current_scope, parant_scope, entrie->theType, "Extra annotation"); + } + } + if (table->Children_Scope != NULL) { + ListOfTable* node = table->Children_Scope; + for (; node != NULL; node = node->next) { + print_symbol_table(node->table, file_ptr); + } + } + if (table->Parent_Scope == NULL) { + fprintf(file_ptr, + "-----------------:--------:--------:----------------------:-------" + "----------------------\n"); + } } SymbolTable* getAncestor(SymbolTable* table) { - if (table->Parent_Scope == NULL) { - // if table has no parent, return itself - return table; - } else { - // call function recursively to grab ancestor - return getAncestor(table->Parent_Scope); - } + if (table->Parent_Scope == NULL) { + // if table has no parent, return itself + return table; + } else { + // call function recursively to grab ancestor + return getAncestor(table->Parent_Scope); + } } SymbolTable* getParent(SymbolTable* st) { return st->Parent_Scope; } @@ -134,15 +134,15 @@ int getColumn(SymbolTable* st) { return st->Column_Number; } // 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); + 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; - } - */ + printf("The type of the first entry is %s\n",First_Entry->theType); + return 0; + } + */ diff --git a/src/symbol_table.h b/src/symbol_table.h index 0ef5c8a..1e1b7da 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -4,24 +4,24 @@ #include typedef struct ListOfTable { - struct SymbolTable* table; - // struct ListOfTable* prev; - struct ListOfTable* next; + struct SymbolTable* table; + // struct ListOfTable* prev; + struct ListOfTable* next; } ListOfTable; typedef struct TableNode { - char* theType; - char* theName; - struct TableNode* next; + char* theType; + char* theName; + struct TableNode* next; } TableNode; typedef struct SymbolTable { - TableNode* entries; - struct SymbolTable* Parent_Scope; - struct ListOfTable* Children_Scope; - int Line_Number; - int Column_Number; + 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); @@ -40,4 +40,4 @@ TableNode* getNextEntry(TableNode* tn); char* getType(TableNode* tn); char* getName(TableNode* tn); int getLine(SymbolTable* st); -int getColumn(SymbolTable* st); \ No newline at end of file +int getColumn(SymbolTable* st); From e8b0af1213cf08adceb7c2b310b0c8e073402acf Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Sun, 9 Mar 2025 15:19:15 -0400 Subject: [PATCH 2/2] Fixed the spacing in the symbole table files t#NONE --- src/symbol_table.c | 5 ++++- src/symbol_table.h | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index ebd06ee..05a1f6e 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -85,7 +85,10 @@ void print_symbol_table(SymbolTable* table, FILE* file_ptr) { } else { current_scope = 1001; } - + if ( entrie == NULL ) { + fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", + current_scope, parant_scope, "", "Empty Scope"); + } for (; entrie != NULL; entrie = entrie->next) { if (parant_scope == 0) { fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", diff --git a/src/symbol_table.h b/src/symbol_table.h index 1e1b7da..03028ad 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -7,7 +7,6 @@ typedef struct ListOfTable { struct SymbolTable* table; // struct ListOfTable* prev; struct ListOfTable* next; - } ListOfTable; typedef struct TableNode {