Updated header

This commit is contained in:
Scarlett
2025-02-28 17:42:47 -05:00
parent 2695334db9
commit 5147d9d41c
2 changed files with 157 additions and 159 deletions

View File

@ -1,9 +1,9 @@
#include "symbol_table.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "symbol_table.h"
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
table->Line_Number = Line;
@ -30,7 +30,6 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){
return table;
}
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf;
@ -65,25 +64,20 @@ TableNode * look_up(SymbolTable * table, char * x){
return look_up(table->Parent_Scope, x);
}
SymbolTable * getAncestor(SymbolTable * table){
if(table->Parent_Scope == NULL){
//if table has no parent, return itself
return table;
} else {
//call function recursively to grab ancestor
return getAncestor(table->Parent_Scope);
}
}
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");
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE",
"PARENT", "TYPE", "Extra annotation");
}
TableNode* entrie = table->entries;
fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n");
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;
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;
@ -92,12 +86,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, "Extra annotation");
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");
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName,
current_scope, parant_scope, entrie->theType, "Extra annotation");
}
}
if (table->Children_Scope != NULL) {
@ -107,50 +100,44 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){
}
}
if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n");
fprintf(file_ptr,
"-----------------:--------:--------:----------------------:-------"
"----------------------\n");
}
}
SymbolTable * getParent(SymbolTable* st){
return st->Parent_Scope;
SymbolTable* getAncestor(SymbolTable* table) {
if (table->Parent_Scope == NULL) {
// if table has no parent, return itself
return table;
} else {
// call function recursively to grab ancestor
return getAncestor(table->Parent_Scope);
}
}
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;
}
char * getType(TableNode * tn){
return tn->theType;
}
char * getName(TableNode * tn){
return tn->theName;
}
int getLine(SymbolTable * st){
return st->Line_Number;
}
int getColumn(SymbolTable *st){
return st->Column_Number;
}
//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation
SymbolTable* getParent(SymbolTable* st) { return st->Parent_Scope; }
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; }
char* getType(TableNode* tn) { return tn->theType; }
char* getName(TableNode* tn) { return tn->theName; }
int getLine(SymbolTable* st) { return st->Line_Number; }
int getColumn(SymbolTable* st) { return st->Column_Number; }
// 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";
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);
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);
printf("The type of the first entry is %s\n",First_Entry->theType);
return 0;

View File

@ -24,9 +24,20 @@ typedef struct SymbolTable{
int Column_Number;
} SymbolTable;
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id);
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column);
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id);
TableNode* table_lookup(SymbolTable* table, char* x);
TableNode* look_up(SymbolTable* table, char* x);
void print_symbol_table(SymbolTable* table, FILE* file_ptr);
SymbolTable* getAncestor(SymbolTable* table);
SymbolTable* getParent(SymbolTable* st);
ListOfTable* getChildren(SymbolTable* st);
SymbolTable* getFirstChild(ListOfTable* lt);
ListOfTable* getRestOfChildren(ListOfTable* lt);
TableNode* getFirstEntry(SymbolTable* st);
TableNode* getNextEntry(TableNode* tn);
char* getType(TableNode* tn);
char* getName(TableNode* tn);
int getLine(SymbolTable* st);
int getColumn(SymbolTable* st);