From 392a4b3ba57cfc2e2f3fe90f4a7acad13c8371b0 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Fri, 14 Mar 2025 05:32:24 -0400 Subject: [PATCH] wrote up symbol table structure. Have to correct code and then add type checking grammar rules --- src/runner.c | 2 +- src/symbol_table.c | 124 ++++++++++++++++++++++++++++++++++++++------- src/symbol_table.h | 2 + 3 files changed, 109 insertions(+), 19 deletions(-) diff --git a/src/runner.c b/src/runner.c index 2110d23..f02e2cf 100644 --- a/src/runner.c +++ b/src/runner.c @@ -75,7 +75,7 @@ fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,tok, yytext); } int run(FILE *alpha) { int token; - top = cur = CreateScope(NULL, 1, 1); + top = cur = init(CreateScope(NULL, 1, 1)); // If file is not found if (alpha == NULL) { diff --git a/src/symbol_table.c b/src/symbol_table.c index 08de0de..c50eac8 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -44,8 +44,9 @@ typedef struct{ typedef struct{ int numofdimensions; - //the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define a multidimensional array of that size - int* arr; + //the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define > + int* sizesofdimensions; + TableNode* typeofarray; }array_info; typedef struct{ @@ -73,6 +74,53 @@ typedef union { FunTypeAdInfo* func_type_info; }AdInfo; */ + +primitive_info* CreatePrimitiveInfo(size){ + primitive_info* prim = (primitive_info*)malloc(sizeof(primitive_info)); + prim.size=size; + return prim; +} + +string_info* CreateStringInfo(int length, char* loc){ + string_info* stringy = (string_info*)malloc(sizeof(string_info)); + stringy.length=length; + char* location = loc; + return stringy; +} + +array_info* CreateArrayInfo(int dim, int* sizes, TableNode* type){ + array_info* arr = (array_info*)malloc(sizeof(array_info)); + arr.numofdimensions=dim; + arr.typeofarray=type; + int* dimensionsizes = loc; + return arr; +} + +record_info* CreateRecordInfo(int length, TableNode* typesarray){ + record_info* reccy = (record_info*)malloc(sizeof(record_info)); + reccy.numofelements=length; + reccy->listoftypes = typesarray; + return reccy; +} + +function_declaration_info* CreateFunctionDeclarationInfo(int line, bool asorregular){ + function_declaration_info* fundec = (function_declaration_info*)malloc(sizeof(function_declaration_info)); + fundec.startlinenumber=line; + fundec.regularoras = asorregular; + return fundec; +} + +function_type_info* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){ + function_type_info* funtype = (function_type_info*)malloc(sizeof(function_type_info)); + funtype->parameter=parameter; + funtype->returntype = returntype; + return funtype; +} + + + + + SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) { SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); table->Line_Number = Line; @@ -105,28 +153,68 @@ SymbolTable* init(SymbolTable* start){ printf("Cannot initialize a scope that is not the parent scope\n"); return NULL; } - TableNode* table1 = (TableNode*)malloc(sizeof(SymbolTable)); - table->Line_Number = Line; - table->Column_Number = Column; - table->Parent_Scope = ParentScope; - table->Children_Scope = NULL; - table->entries = NULL; + TableNode* integ = (TableNode*)malloc(sizeof(SymbolTable)); + TableNode* addr = (TableNode*)malloc(sizeof(SymbolTable)); + TableNode* chara = (TableNode*)malloc(sizeof(SymbolTable)); + TableNode* stri = (TableNode*)malloc(sizeof(SymbolTable)); + TableNode* boo = (TableNode*)malloc(sizeof(SymbolTable)); + TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); + start->entries = integ; + integ->next = addr; + addr->next = chara; + chara->next = stri; + stri->next = boo; + boo->next = arr; + integ->theName= "integer"; + addr->theName= "address"; + chara->theName= "character"; + boo->theName= "Boolean"; + stri->theName= "string"; + arr->theName= "array" -TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) { + //root TableNode that all are pointing to but not in table + TableNode* prime = (TableNode*)malloc(sizeof(SymbolTable)); + prime->theName= "primitive"; + prime->theType=NULL; + prime->additionalinfo = NULL; + prime->next = NULL; + + TableNode* striprim = (TableNode*)malloc(sizeof(SymbolTable)); + prime->theName= "1->character"; + prime->theType=NULL; + prime->additionalinfo = NULL; + prime->next = NULL; + + integ->theType=prime; + addr->theType=prime; + chara->theType=prime; + stri->theType=striprim; + boo->theType=prime; + arr->theType=prime; + + start->Line_Number = 1; + start->Column_Number = 1; + start->Parent_Scope = NULL; + start->Children_Scope = NULL; + + return start; +} + +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, AdInfo* ad) { if(table ==NULL){ printf("Null reference to table"); return NULL; } -/*TableNode* topDef = (table_lookup(getAncestor(table),typeOf)); +TableNode* topDef = (table_lookup(getAncestor(table),typeOf)); if(topDef == NULL){ printf("This type is not defined at the top level\n"); return NULL; -}*/ +} TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - //newEntry->theType = topDef->theName; - newEntry->theType=typeOf; + newEntry->theType = topDef; newEntry->theName = id; + newEntry->additionalinfo = ad; if (table->entries == NULL) { table->entries = newEntry; return newEntry; @@ -137,7 +225,7 @@ if(topDef == NULL){ return newEntry; } } - +/* //we use false for type defs and true for functions for parameter of typeOf TableNode* Define(SymbolTable* table, bool typeOf, char* id) { if(table ==NULL || table->Parent_Scope != NULL){ @@ -171,7 +259,7 @@ if(table_lookup(table,id) != NULL){ } } - +*/ TableNode* table_lookup(SymbolTable* table, char* x) { TableNode* entrie = table->entries; for (; entrie != NULL; entrie = entrie->next) { @@ -217,11 +305,11 @@ void print_symbol_table(SymbolTable* table, FILE* file_ptr) { for (; entrie != NULL; entrie = entrie->next) { if (parant_scope == 0) { fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", - entrie->theName, current_scope, entrie->theType, + entrie->theName, current_scope, entrie->theType->theName, "Extra annotation"); } else { fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName, - current_scope, parant_scope, entrie->theType, "Extra annotation"); + current_scope, parant_scope, entrie->theType->theName, "Extra annotation"); } } if (table->Children_Scope != NULL) { @@ -254,7 +342,7 @@ SymbolTable* getFirstChild(ListOfTable* lt) { return lt->table; } ListOfTable* getRestOfChildren(ListOfTable* lt) { return lt->next; } TableNode* getFirstEntry(SymbolTable* st) { return st->entries; } TableNode* getNextEntry(TableNode* tn) { return tn->next; } -char* getType(TableNode* tn) { return tn->theType; } +char* getType(TableNode* tn) { return tn->theType->theName; } char* getName(TableNode* tn) { return tn->theName; } int getLine(SymbolTable* st) { return st->Line_Number; } int getColumn(SymbolTable* st) { return st->Column_Number; } diff --git a/src/symbol_table.h b/src/symbol_table.h index 46caa02..497cf3f 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -15,6 +15,8 @@ typedef struct{ typedef struct{ int numofdimensions; //the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define > int* arr; + int* sizesofdimensions; + TableNode* typeofarray; }array_info; typedef struct{