fixed most things in the symbol table structure. Seeing core dumps and have to fix the print symbol table function

This commit is contained in:
Partho Bhattacharya
2025-03-26 15:21:42 -04:00
parent 5aefd319ae
commit 0ff894d571
2 changed files with 56 additions and 16 deletions

View File

@ -27,23 +27,23 @@ typedef enum {
// TYPE_BOOLEAN, // TYPE_BOOLEAN,
// TYPE_ADDRESS, // TYPE_ADDRESS,
// Type String is an array of char enclosed in double quotes per lexer // Type String is an array of char enclosed in double quotes per lexer
TYPE_STRING, TYPE_STRING = 1,
// Array can be multidimensional. Information should be stored here // Array can be multidimensional. Information should be stored here
TYPE_ARRAY, TYPE_ARRAY = 2,
// Record is user defined types // Record is user defined types
TYPE_RECORD, TYPE_RECORD = 3,
// Declaring what type a particular function is without as // Declaring what type a particular function is without as
TYPE_FUNCTION_DECLARATION, TYPE_FUNCTION_DECLARATION = 4,
// Declaring what type a particular function is with as // Declaring what type a particular function is with as
// TYPE_AS_FUNCTION_DECLARATION, // TYPE_AS_FUNCTION_DECLARATION,
// Declaring what type a function is (what the parameters and output // Declaring what type a function is (what the parameters and output
// are) // are)
TYPE_FUNCTION_TYPE, TYPE_FUNCTION_TYPE = 5,
// The Type being pointed to by the first 4 above that only stores the // The Type being pointed to by the first 4 above that only stores the
// size // size
TYPE_PRIMITIVE, TYPE_PRIMITIVE = 6,
//likely NULL //likely NULL
TYPE_ALL_ELSE TYPE_ALL_ELSE = 7
} types; } types;
@ -153,12 +153,12 @@ TableNode *getArrType(TableNode *definition) {
// order, of what make up that type in an array. Unfortunately this second part // order, of what make up that type in an array. Unfortunately this second part
// should probably instead be replaced by a reference to a scope in which those // should probably instead be replaced by a reference to a scope in which those
// elements are found. // elements are found.
AdInfo *CreateRecordInfo(int length, TableNode *typesarray) { AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) {
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
info->RecAdInfo = (record_info *)malloc(sizeof(record_info)); info->RecAdInfo = (record_info *)malloc(sizeof(record_info));
info->RecAdInfo->numofelements = length; info->RecAdInfo->numofelements = length;
// replace below with reference to a scope, not an array // replace below with reference to a scope, not an array
info->RecAdInfo->listoftypes = typesarray; info->RecAdInfo->recordScope = recordScope;
return info; return info;
} }
// This gets the number of elements that make up a record. // This gets the number of elements that make up a record.
@ -172,13 +172,13 @@ int getRecLength(TableNode *definition) {
return definition->additionalinfo->RecAdInfo->numofelements; return definition->additionalinfo->RecAdInfo->numofelements;
} }
// This gets the array. Needs to up be updated to get the scope instead // This gets the array. Needs to up be updated to get the scope instead
TableNode *getRecList(TableNode *definition) { SymbolTable *getRecList(TableNode *definition) {
if (strcmp(getType(definition), "record") != 0) { if (strcmp(getType(definition), "record") != 0) {
printf("not checking the list of types of a record -- invalid " printf("not checking the list of types of a record -- invalid "
"op\n"); "op\n");
return NULL; return NULL;
} }
return definition->additionalinfo->RecAdInfo->listoftypes; return definition->additionalinfo->RecAdInfo->recordScope;
} }
// below function takes a bool to see if parameter should be decomposed or not // below function takes a bool to see if parameter should be decomposed or not
@ -379,8 +379,34 @@ TableNode* boo;
TableNode* recprime; TableNode* recprime;
TableNode* funtypeprime; TableNode* funtypeprime;
*/ */
TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){
if(tn == NULL){
printf("passed in an invalid table node to modify (NULL).\n");
return NULL;
}
if(type == NULL){
printf("passed in a NULL type reference to populate a table node. Invalid.\n");
return NULL;
}
if(info == NULL){
printf("passed in a NULL info reference to populate a table node. Invalid.\n");
return NULL;
}
tn->theType = type;
tn->additionalinfo = info;
//returning reference to modified table node
return tn;
}
int getAdInfoType(TableNode* tn){ int getAdInfoType(TableNode* tn){
if(tn == NULL){
printf("passing in NULL table entry. Invalid\n");
return -1;
}
if(tn->theType == NULL){
printf("Entry being passed in has a null reference for theType. Invalid.\n");
return -1;
}
if(strcmp(getType(tn),getName(integ))==0){ if(strcmp(getType(tn),getName(integ))==0){
return TYPE_PRIMITIVE; return TYPE_PRIMITIVE;
} }
@ -443,7 +469,16 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
} }
} }
char *getType(TableNode *tn) { return tn->theType->theName; } char *getType(TableNode *tn) {
if(tn == NULL){
printf("passed a NULL table entry to getType\n");
return NULL;
}
if(tn->theType == NULL){
printf("type of entry is currently NULL, undefined type \n");
return NULL;
}
return tn->theType->theName; }
char *getName(TableNode *tn) { return tn->theName; } char *getName(TableNode *tn) { return tn->theName; }
int getLine(SymbolTable *st) { return st->Line_Number; } int getLine(SymbolTable *st) { return st->Line_Number; }
int getColumn(SymbolTable *st) { return st->Column_Number; } int getColumn(SymbolTable *st) { return st->Column_Number; }
@ -483,6 +518,7 @@ names\n"); return NULL;
} }
*/ */
//only check table that is given
TableNode *table_lookup(SymbolTable *table, char *x) { TableNode *table_lookup(SymbolTable *table, char *x) {
TableNode *entrie = table->entries; TableNode *entrie = table->entries;
for (; entrie != NULL; entrie = entrie->next) { for (; entrie != NULL; entrie = entrie->next) {
@ -492,6 +528,7 @@ TableNode *table_lookup(SymbolTable *table, char *x) {
} }
return NULL; return NULL;
} }
//check current table and all parents
TableNode *look_up(SymbolTable *table, char *x) { TableNode *look_up(SymbolTable *table, char *x) {
if (table == NULL) { if (table == NULL) {
return NULL; return NULL;
@ -504,6 +541,8 @@ TableNode *look_up(SymbolTable *table, char *x) {
} }
void print_symbol_table(SymbolTable *table, FILE *file_ptr) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
return;
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation"); "SCOPE", "PARENT", "TYPE", "Extra annotation");
@ -526,7 +565,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "",
current_scope, parant_scope, "", "Empty Scope"); current_scope, parant_scope, "", "Empty Scope");
} }
for (; entrie != NULL; entrie = entrie->next) { for (entrie != NULL; entrie = entrie->next;) {
if (parant_scope == 0) { if (parant_scope == 0) {
/*have to update*/ if (strcmp(entrie->theType->theName, /*have to update*/ if (strcmp(entrie->theType->theName,
"function primitive") || "function primitive") ||
@ -555,7 +594,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
"----------------------\n"); "----------------------\n");
} }
} }
//get top most symbol table
SymbolTable *getAncestor(SymbolTable *table) { SymbolTable *getAncestor(SymbolTable *table) {
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
// if table has no parent, return itself // if table has no parent, return itself
@ -594,6 +633,7 @@ SymbolTable *removeEntry(SymbolTable *scope, char *search) {
return scope; return scope;
} }
//almost certainly don't need to use the below function since type checking happens by passing types up the grammar
bool typeCheck(char *firstID, char *secondID) { bool typeCheck(char *firstID, char *secondID) {
TableNode *entry1 = look_up(cur, firstID); TableNode *entry1 = look_up(cur, firstID);

View File

@ -30,7 +30,7 @@ typedef struct {
// similar to above we define a record to hold the number of elements // similar to above we define a record to hold the number of elements
// and an array of tablenodes (types) that it contains in the > // and an array of tablenodes (types) that it contains in the >
int numofelements; int numofelements;
struct TableNode *listoftypes; struct SymbolTable *recordScope;
} record_info; } record_info;
typedef struct { typedef struct {
@ -91,7 +91,7 @@ int getPrimSize(TableNode *definition);
int getNumArrDim(TableNode *definition); int getNumArrDim(TableNode *definition);
TableNode *getArrType(TableNode *definition); TableNode *getArrType(TableNode *definition);
int getRecLength(TableNode *definition); int getRecLength(TableNode *definition);
TableNode *getRecList(TableNode *definition); SymbolTable *getRecList(TableNode *definition);
int getStartLine(TableNode *definition); int getStartLine(TableNode *definition);
bool getAsKeyword(TableNode *definition); bool getAsKeyword(TableNode *definition);
TableNode *getParameter(TableNode *definition); TableNode *getParameter(TableNode *definition);