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,156 +1,143 @@
#include "symbol_table.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "symbol_table.h" SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ table->Line_Number = Line;
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); table->Column_Number = Column;
table->Line_Number = Line; table->Parent_Scope = ParentScope;
table->Column_Number = Column; table->Children_Scope = NULL;
table->Parent_Scope = ParentScope; table->entries = NULL;
table->Children_Scope = NULL; if (ParentScope != NULL) {
table->entries = NULL; if (ParentScope->Children_Scope == NULL) {
if(ParentScope != NULL){ ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
if(ParentScope->Children_Scope == NULL){ newEntry->next = NULL;
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); // newEntry->prev = NULL;
newEntry->next = NULL; newEntry->table = table;
//newEntry->prev = NULL; ParentScope->Children_Scope = newEntry;
newEntry->table = table; } else {
ParentScope->Children_Scope = newEntry; ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
} else{ // newEntry->prev = NULL;
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); newEntry->table = table;
//newEntry->prev = NULL; ListOfTable* oldEntry = ParentScope->Children_Scope;
newEntry->table= table; ParentScope->Children_Scope = newEntry;
ListOfTable* oldEntry = ParentScope->Children_Scope; newEntry->next = oldEntry;
ParentScope->Children_Scope = newEntry; }
newEntry->next = oldEntry; }
} return table;
}
return table;
}
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;
}
}
TableNode * table_lookup(SymbolTable * table, char * x){
TableNode * entrie = table->entries;
for(; entrie != NULL; entrie = entrie->next){
if (!strcmp(entrie->theName, x)){
return entrie;
}
}
return NULL;
}
TableNode * look_up(SymbolTable * table, char * x){
if(table == NULL){
return NULL;
}
TableNode * ret = table_lookup(table, x);
if (ret != NULL){
return ret;
}
return look_up(table->Parent_Scope, x);
} }
SymbolTable * getAncestor(SymbolTable * table){ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
if(table->Parent_Scope == NULL){ TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
//if table has no parent, return itself newEntry->theType = typeOf;
return table; newEntry->theName = id;
} else { if (table->entries == NULL) {
//call function recursively to grab ancestor table->entries = newEntry;
return getAncestor(table->Parent_Scope); return newEntry;
} } else {
TableNode* oldEntry = table->entries;
table->entries = newEntry;
newEntry->next = oldEntry;
return newEntry;
}
} }
void print_symbol_table(SymbolTable *table, FILE *file_ptr){ TableNode* table_lookup(SymbolTable* table, char* x) {
if(table->Parent_Scope == NULL){ TableNode* entrie = table->entries;
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARENT", "TYPE", "Extra annotation"); for (; entrie != NULL; entrie = entrie->next) {
} if (!strcmp(entrie->theName, x)) {
TableNode * entrie = table->entries; return entrie;
fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n"); }
int parant_scope = 0; }
int current_scope = 0; return NULL;
if(table->Parent_Scope != NULL){ }
parant_scope = table->Parent_Scope->Line_Number*1000 + table->Parent_Scope->Column_Number; TableNode* look_up(SymbolTable* table, char* x) {
current_scope = table->Line_Number*1000 + table->Column_Number; if (table == NULL) {
} else { return NULL;
current_scope = 1001; }
} TableNode* ret = table_lookup(table, x);
if (ret != NULL) {
for(; entrie != NULL; entrie = entrie->next){ return ret;
if (parant_scope == 0){ }
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", return look_up(table->Parent_Scope, x);
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");
}
} }
SymbolTable * getParent(SymbolTable* st){ void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
return st->Parent_Scope; 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;
}
ListOfTable * getChildren(SymbolTable* st){ for (; entrie != NULL; entrie = entrie->next) {
return st->Children_Scope; if (parant_scope == 0) {
} fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
SymbolTable * getFirstChild(ListOfTable * lt){ entrie->theName, current_scope, entrie->theType,
return lt->table; "Extra annotation");
} } else {
ListOfTable * getRestOfChildren(ListOfTable * lt){ fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName,
return lt->next; current_scope, parant_scope, entrie->theType, "Extra annotation");
} }
TableNode * getFirstEntry(SymbolTable * st){ }
return st->entries; if (table->Children_Scope != NULL) {
} ListOfTable* node = table->Children_Scope;
TableNode * getNextEntry(TableNode * tn){ for (; node != NULL; node = node->next) {
return tn->next; print_symbol_table(node->table, file_ptr);
} }
char * getType(TableNode * tn){ }
return tn->theType; if (table->Parent_Scope == NULL) {
} fprintf(file_ptr,
char * getName(TableNode * tn){ "-----------------:--------:--------:----------------------:-------"
return tn->theName; "----------------------\n");
} }
int getLine(SymbolTable * st){ }
return st->Line_Number;
} SymbolTable* getAncestor(SymbolTable* table) {
int getColumn(SymbolTable *st){ if (table->Parent_Scope == NULL) {
return st->Column_Number; // if table has no parent, return itself
} return table;
//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation } else {
// call function recursively to grab ancestor
return getAncestor(table->Parent_Scope);
}
}
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(){ int main(){
char* String = "STRING"; char* String = "STRING";
char* X = "X"; char* X = "X";
SymbolTable* Second = CreateScope(NULL, 2,2); SymbolTable* Second = CreateScope(NULL, 2,2);
printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number); printf("Line number is %d, Column number of scope is
TableNode* First_Entry = CreateEntry(Second,String,X); %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); printf("The type of the first entry is %s\n",First_Entry->theType);
return 0; return 0;

View File

@ -3,30 +3,41 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
typedef struct ListOfTable{ typedef struct ListOfTable {
struct SymbolTable* table; struct SymbolTable* table;
//struct ListOfTable* prev; // struct ListOfTable* prev;
struct ListOfTable* next; struct ListOfTable* next;
}ListOfTable; } ListOfTable;
typedef struct TableNode{ typedef struct TableNode {
char* theType; char* theType;
char* theName; char* theName;
struct TableNode* next; struct TableNode* next;
}TableNode; } TableNode;
typedef struct SymbolTable{ typedef struct SymbolTable {
TableNode* entries; TableNode* entries;
struct SymbolTable* Parent_Scope; struct SymbolTable* Parent_Scope;
struct ListOfTable* Children_Scope; struct ListOfTable* Children_Scope;
int Line_Number; int Line_Number;
int Column_Number; int Column_Number;
}SymbolTable; } SymbolTable;
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id);
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column);
TableNode * table_lookup(SymbolTable * table, char * x); TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id);
TableNode * look_up(SymbolTable * table, char * x); TableNode* table_lookup(SymbolTable* table, char* x);
void print_symbol_table(SymbolTable *table, FILE *file_ptr); 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);