diff --git a/print_symbol_table.c b/print_symbol_table.c new file mode 100644 index 0000000..0ae90f6 --- /dev/null +++ b/print_symbol_table.c @@ -0,0 +1,81 @@ +#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, NULL, 0); + CreateEntry(parant, prim, chare, NULL, 0); + CreateEntry(parant, prim, inte, NULL, 0); + CreateEntry(parant, &"1 -> character", str, NULL, 0); + CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0); + CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0); + CreateEntry(parant, &"int2int", &"square", NULL, 0); + CreateEntry(parant, &"string2int", &"entry", NULL, 0); + SymbolTable * child = CreateScope(parant, 14,14); + CreateEntry(child, inte, &"x", NULL, 0); + SymbolTable * second = CreateScope(parant, 21,15); + CreateEntry(second, str, arg, NULL, 0); + CreateEntry(second, inte, &"input", NULL, 0); + CreateEntry(second, inte, &"expected", NULL, 0); + CreateEntry(second, inte, &"actual", NULL, 0); + CreateEntry(second, &"$_undefined_type", &"result", NULL, 0); + SymbolTable * third = CreateScope(second, 33,44); + CreateEntry(third, &"BOO", arg, NULL, 0); + CreateEntry(third, &"YAZOO", &"input", NULL, 0); + 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 343cf66..9512b8f 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -1,46 +1,11 @@ //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 + +//Tail of Linked List points to a List of child scopes + + #include "symbol_table.h" -/* -#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; - -*/ - SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); table->Line_Number = Line; @@ -66,40 +31,31 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ } 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 -/* + +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"; - 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); + TableNode* First_Entry = CreateEntry(Second,String,X); + 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 539f2e4..d510a3b 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -9,20 +9,10 @@ typedef struct ListOfTable{ }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{ @@ -32,3 +22,7 @@ typedef struct SymbolTable{ int Line_Number; int Column_Number; }SymbolTable; + + +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id); +SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); diff --git a/test b/test new file mode 100755 index 0000000..4941bf2 Binary files /dev/null and b/test differ