From 188c7344655ea4d9495004e9eb848a1e8e6eaca6 Mon Sep 17 00:00:00 2001 From: Scarlett Date: Mon, 31 Mar 2025 17:38:37 -0400 Subject: [PATCH] small changes, segfault issue found --- src/grammar.y | 4 +-- src/symbol_table.c | 63 ++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index b44f502..94fd0a9 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -121,7 +121,7 @@ prototype: L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID; definition: -TYPE ID COLON { + TYPE ID COLON { printdebug("Currently see a record definition for %s", $2); tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); if (table_lookup(getAncestor(cur), $2) == undefined) { @@ -238,7 +238,7 @@ declaration: ; id_or_types: - ID {printdebug("string of id is %s in ID pattern of id_or_type rule."); $$ = $1;} + ID {printdebug("string of id is %s in ID pattern of id_or_type rule.", $1); $$ = $1;} //{printdebug("string of id is %s in ID pattern of id_or_type rule. Type passed up the tree is %s.",$1,getType(look_up(cur,$1))); $$ = getType(look_up(cur,$1));} | types {printdebug("string of type is %s in types pattern of id_or_type rule.",$1);} {$$ = $1;} //{printdebug("string of type is %s in types pattern of id_or_type rule. That is passed up the tree.",$1);} {$$ = $1;} diff --git a/src/symbol_table.c b/src/symbol_table.c index e1efdfb..6421002 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -148,8 +148,8 @@ int getPrimSize(TableNode *definition) { return -1; } if (definition == undefined) { - printdebug( - "passed an undefined entry to getPrimSize function. Invalid."); + printdebug("passed an undefined entry to getPrimSize function. " + "Invalid."); return -1; } if (definition->additionalinfo == NULL) { @@ -360,15 +360,13 @@ int getStartLine(TableNode *definition) { TableNode *setStartLine(TableNode *tn, int start) { if (tn == NULL) { - printdebug( - "passing in a NULL entry to setStartLine. " - "invalid"); + printdebug("passing in a NULL entry to setStartLine. " + "invalid"); return undefined; } if (tn == undefined) { - printdebug( - "passing in an undefined entry to setStartLine. " - "invalid"); + printdebug("passing in an undefined entry to setStartLine. " + "invalid"); return undefined; } tn->additionalinfo->FunDecAdInfo->startlinenumber = start; @@ -398,15 +396,13 @@ bool getAsKeyword(TableNode *definition) { TableNode *setAsKeyword(TableNode *tn, bool as) { if (tn == NULL) { - printdebug( - "passing in a NULL entry to setAsKeyword. " - "invalid"); + printdebug("passing in a NULL entry to setAsKeyword. " + "invalid"); return undefined; } if (tn == undefined) { - printdebug( - "passing in an undefined entry to setAsKeyword. " - "invalid"); + printdebug("passing in an undefined entry to setAsKeyword. " + "invalid"); return undefined; } tn->additionalinfo->FunDecAdInfo->regularoras = as; @@ -630,11 +626,13 @@ TableNode* funtypeprime; */ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) { if (tn == NULL) { - printdebug("passed in an NULL table node to populateTypeAndInfo."); + printdebug( + "passed in an NULL table node to populateTypeAndInfo."); return undefined; } if (tn == undefined) { - printdebug("passed in an undefined table node to populateTypeAndInfo"); + printdebug( + "passed in an undefined table node to populateTypeAndInfo"); return undefined; } if (type == NULL) { @@ -661,11 +659,13 @@ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) { int getAdInfoType(TableNode *tn) { if (tn == NULL) { - printdebug("passing in NULL table entry to getAdInfoType. Invalid"); + printdebug( + "passing in NULL table entry to getAdInfoType. Invalid"); return -1; } if (tn == undefined) { - printdebug("passing in undefined table entry to getAdInfoType. Invalid"); + printdebug("passing in undefined table entry to getAdInfoType. " + "Invalid"); return -1; } if (tn->theType == NULL) { @@ -725,26 +725,27 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, } */ if (typeOf == NULL) { - printdebug( - "Passing an NULL Type Node to Create Entry"); + printdebug("Passing an NULL Type Node to Create Entry"); return undefined; } if (typeOf == undefined) { - printdebug( - "Passing an undefined Type Node to Create Entry"); + printdebug("Passing an undefined Type Node to Create Entry"); return undefined; } + TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); newEntry->theType = typeOf /*topDef*/; newEntry->theName = id; newEntry->additionalinfo = ad; if (table->entries == NULL) { table->entries = newEntry; + printdebug("[CreateEntry] Adding %s to the symbol table", id); return newEntry; } else { TableNode *oldEntry = table->entries; table->entries = newEntry; newEntry->next = oldEntry; + printdebug("[CreateEntry] Adding %s to the symbol table", id); return newEntry; } } @@ -779,7 +780,7 @@ char *getName(TableNode *tn) { return undefined->theName; } if (tn->theName == NULL) { - // printdebug("name of entry is currently NULL, undefined"); + printdebug("name of entry is currently NULL, undefined"); return undefined->theName; } return tn->theName; @@ -803,15 +804,13 @@ int getColumn(SymbolTable *st) { } TableNode *addName(TableNode *tn, char *str) { if (tn == NULL) { - printdebug( - "passed a Null table node to the addName " - "function. Invalid."); + printdebug("passed a Null table node to the addName " + "function. Invalid."); return undefined; } if (tn == undefined) { - printdebug( - "passed an undefined table node to the addName " - "function. Invalid."); + printdebug("passed an undefined table node to the addName " + "function. Invalid."); return undefined; } if (tn->theName != NULL) { @@ -1220,7 +1219,11 @@ ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; } 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; } + +// Segfaults when passed an invalid table node! +TableNode *getNextEntry(TableNode *tn) { return tn; } + + // uncomment the below main function along with the headers above for a simple // standalone test of table and entry creation