From 6c72120785505bf4d6270d96d8c7745a70161e4b Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Sun, 23 Feb 2025 11:38:55 -0500 Subject: [PATCH 1/3] added the prototypes for lookups to the h file t#29 --- symbol_table.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/symbol_table.h b/symbol_table.h index 4830345..e2372a0 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -27,3 +27,5 @@ typedef struct SymbolTable{ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); +TableNode * table_lookup(SymbolTable * table, char * x); +TableNode * look_up(SymbolTable * table, char * x); From d790d5fdefb5ea277aee6b397c5f0bbad11808d1 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Sun, 23 Feb 2025 12:06:37 -0500 Subject: [PATCH 2/3] Made some changes to the files t#29 --- print_symbol_table.c | 98 -------------------------------------------- symbol_table.c | 43 +++++++++++++++++++ symbol_table.h | 1 + test.c | 74 +++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 98 deletions(-) delete mode 100644 print_symbol_table.c create mode 100644 test.c diff --git a/print_symbol_table.c b/print_symbol_table.c deleted file mode 100644 index c5e4208..0000000 --- a/print_symbol_table.c +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include "symbol_table.h" -#include "symbol_table.c" -#include - -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; - } - - 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"); - } -} -/* - -int main(void){ - char *prim = strdup("primitive"); - char *inte = strdup("integer"); - SymbolTable * parant = CreateScope(NULL, 1,1); - char *boole = strdup("Boolean"); - char *chare = strdup("character"); - char *str = strdup("string"); - char *arg = strdup("arg"); - // Value* v = calloc(1, sizeof(Value)); - CreateEntry(parant, prim, boole); - CreateEntry(parant, prim, chare); - CreateEntry(parant, prim, inte); - CreateEntry(parant, &"1 -> character", str); - CreateEntry(parant, &"integer -> integer", &"int2int"); - CreateEntry(parant, &"string -> integer", &"string2int"); - CreateEntry(parant, &"int2int", &"square"); - CreateEntry(parant, &"string2int", &"entry"); - SymbolTable * child = CreateScope(parant, 14,14); - CreateEntry(child, inte, &"x"); - SymbolTable * second = CreateScope(parant, 21,15); - CreateEntry(second, str, arg); - CreateEntry(second, inte, &"input"); - CreateEntry(second, inte, &"expected"); - CreateEntry(second, inte, &"actual"); - CreateEntry(second, &"$_undefined_type", &"result"); - SymbolTable * third = CreateScope(second, 33,44); - CreateEntry(third, &"BOO", arg); - CreateEntry(third, &"YAZOO", &"input"); - - TableNode *ret = table_lookup(third, "arg"); - printf("%s == %s\n", ret->theName, "arg"); - - ret = table_lookup(third, "hello"); - printf("This should be nil %p != %s\n", ret, "BOO"); - - ret = look_up(second, "input"); - printf("%s == %s\n", ret->theName, "input"); - - ret = look_up(second, "square"); - printf("%s == %s\n", ret->theName, "square"); - - ret = look_up(second, "spuare"); - printf("This should be nil %p == %s\n", ret, "square"); - - print_symbol_table(parant, stderr); - free(inte); - free(boole); - free(prim); - free(str); - free(chare); - free(arg); - return 0; -} - -*/ diff --git a/symbol_table.c b/symbol_table.c index e5d1366..52ecf2b 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -1,5 +1,10 @@ //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 +#include +#include //Tail of Linked List points to a List of child scopes @@ -66,9 +71,46 @@ TableNode * look_up(SymbolTable * table, char * x){ } 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; + } + + 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"); + } +} //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"; @@ -79,3 +121,4 @@ int main(){ printf("The type of the first entry is %s\n",First_Entry->theType); return 0; } + */ diff --git a/symbol_table.h b/symbol_table.h index e2372a0..57ae1bc 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -29,3 +29,4 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); TableNode * table_lookup(SymbolTable * table, char * x); TableNode * look_up(SymbolTable * table, char * x); +void print_symbol_table(SymbolTable *table, FILE *file_ptr); diff --git a/test.c b/test.c new file mode 100644 index 0000000..0d9b343 --- /dev/null +++ b/test.c @@ -0,0 +1,74 @@ +#include +#include +#include "symbol_table.h" + +int main(void){ + char *prim = strdup("primitive"); + char *inte = strdup("integer"); + SymbolTable * parant = CreateScope(NULL, 1,1); + char *boole = strdup("Boolean"); + char *chare = strdup("character"); + char *str = strdup("string"); + char *arg = strdup("arg"); + char *one_to_char = strdup("1 -> character"); + char *int_to_int = strdup("integer -> integer"); + char *int2int = strdup("int2int"); + char *str_to_int = strdup("string -> integer"); + char *str2int = strdup("string2int"); + char *square = strdup("square"); + char *string2int = strdup("string2int"); + char *entry = strdup("entry"); + char *x = strdup("x"); + char *input = strdup("input"); + char *expected = strdup("expected"); + char *actual = strdup("actual"); + char *$_und_type = strdup("$_undefined_type"); + char *result = strdup("result"); + char *BOO = strdup("BOO"); + char *YAZOO = strdup("YAZOO"); + + CreateEntry(parant, prim, boole); + CreateEntry(parant, prim, chare); + CreateEntry(parant, prim, inte); + CreateEntry(parant, one_to_char, str); + CreateEntry(parant, int_to_int, int2int); + CreateEntry(parant, str_to_int, str2int); + CreateEntry(parant, int2int, square); + CreateEntry(parant, string2int, entry); + SymbolTable * child = CreateScope(parant, 14,14); + CreateEntry(child, inte, x); + SymbolTable * second = CreateScope(parant, 21,15); + CreateEntry(second, str, arg); + CreateEntry(second, inte, input); + CreateEntry(second, inte, expected); + CreateEntry(second, inte, actual); + CreateEntry(second, $_und_type, result); + SymbolTable * third = CreateScope(second, 33,44); + CreateEntry(third, BOO, arg); + CreateEntry(third, YAZOO, input); + + TableNode *ret = table_lookup(third, "arg"); + printf("%s == %s\n", ret->theName, "arg"); + + ret = table_lookup(third, "hello"); + printf("This should be nil %p != %s\n", ret, "BOO"); + + ret = look_up(second, "input"); + printf("%s == %s\n", ret->theName, "input"); + + ret = look_up(second, "square"); + printf("%s == %s\n", ret->theName, "square"); + + ret = look_up(second, "spuare"); + printf("This should be nil %p == %s\n", ret, "square"); + + print_symbol_table(parant, stderr); + free(inte); + free(boole); + free(prim); + free(str); + free(chare); + free(arg); + return 0; +} + From d6033e9168ea4de0dea1d3f0fbafcc51179f9df6 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Sun, 23 Feb 2025 12:21:28 -0500 Subject: [PATCH 3/3] Removed comments to make symbole table easyer to read. --- symbol_table.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/symbol_table.c b/symbol_table.c index 52ecf2b..e87ebd8 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -1,14 +1,7 @@ -//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 #include #include -//Tail of Linked List points to a List of child scopes - - #include "symbol_table.h" SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){