Made some changes to the files t#29

This commit is contained in:
Meyer Simon
2025-02-23 12:06:37 -05:00
parent 6c72120785
commit d790d5fdef
4 changed files with 118 additions and 98 deletions

View File

@ -1,98 +0,0 @@
#include <stdlib.h>
#include "symbol_table.h"
#include "symbol_table.c"
#include <string.h>
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");
}
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;
}
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");
} 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");
}
}
/*
int main(void){
char *prim = strdup("primitive");
char *inte = strdup("integer");
SymbolTable * parant = CreateScope(NULL, 1,1);
char *boole = strdup("Boolean");
char *chare = strdup("character");
char *str = strdup("string");
char *arg = strdup("arg");
// Value* v = calloc(1, sizeof(Value));
CreateEntry(parant, prim, boole);
CreateEntry(parant, prim, chare);
CreateEntry(parant, prim, inte);
CreateEntry(parant, &"1 -> character", str);
CreateEntry(parant, &"integer -> integer", &"int2int");
CreateEntry(parant, &"string -> integer", &"string2int");
CreateEntry(parant, &"int2int", &"square");
CreateEntry(parant, &"string2int", &"entry");
SymbolTable * child = CreateScope(parant, 14,14);
CreateEntry(child, inte, &"x");
SymbolTable * second = CreateScope(parant, 21,15);
CreateEntry(second, str, arg);
CreateEntry(second, inte, &"input");
CreateEntry(second, inte, &"expected");
CreateEntry(second, inte, &"actual");
CreateEntry(second, &"$_undefined_type", &"result");
SymbolTable * third = CreateScope(second, 33,44);
CreateEntry(third, &"BOO", arg);
CreateEntry(third, &"YAZOO", &"input");
TableNode *ret = table_lookup(third, "arg");
printf("%s == %s\n", ret->theName, "arg");
ret = table_lookup(third, "hello");
printf("This should be nil %p != %s\n", ret, "BOO");
ret = look_up(second, "input");
printf("%s == %s\n", ret->theName, "input");
ret = look_up(second, "square");
printf("%s == %s\n", ret->theName, "square");
ret = look_up(second, "spuare");
printf("This should be nil %p == %s\n", ret, "square");
print_symbol_table(parant, stderr);
free(inte);
free(boole);
free(prim);
free(str);
free(chare);
free(arg);
return 0;
}
*/

View File

@ -1,5 +1,10 @@
//Defining a symbol table //Defining a symbol table
//Using a Linked List Structure. Head of linked List points to parent scope (if one exists) //Using a Linked List Structure. Head of linked List points to parent scope (if one exists)
//Tail of Linked List points to a Linked List of all the child scopes
//T
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
//Tail of Linked List points to a List of child scopes //Tail of Linked List points to a List of child scopes
@ -66,9 +71,46 @@ TableNode * look_up(SymbolTable * table, char * x){
} }
return look_up(table->Parent_Scope, x); return look_up(table->Parent_Scope, x);
} }
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");
}
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;
}
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");
} 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");
}
}
//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation //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";
@ -79,3 +121,4 @@ int main(){
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

@ -29,3 +29,4 @@ 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 * table_lookup(SymbolTable * table, char * x);
TableNode * look_up(SymbolTable * table, char * x); TableNode * look_up(SymbolTable * table, char * x);
void print_symbol_table(SymbolTable *table, FILE *file_ptr);

74
test.c Normal file
View File

@ -0,0 +1,74 @@
#include <stdlib.h>
#include <string.h>
#include "symbol_table.h"
int main(void){
char *prim = strdup("primitive");
char *inte = strdup("integer");
SymbolTable * parant = CreateScope(NULL, 1,1);
char *boole = strdup("Boolean");
char *chare = strdup("character");
char *str = strdup("string");
char *arg = strdup("arg");
char *one_to_char = strdup("1 -> character");
char *int_to_int = strdup("integer -> integer");
char *int2int = strdup("int2int");
char *str_to_int = strdup("string -> integer");
char *str2int = strdup("string2int");
char *square = strdup("square");
char *string2int = strdup("string2int");
char *entry = strdup("entry");
char *x = strdup("x");
char *input = strdup("input");
char *expected = strdup("expected");
char *actual = strdup("actual");
char *$_und_type = strdup("$_undefined_type");
char *result = strdup("result");
char *BOO = strdup("BOO");
char *YAZOO = strdup("YAZOO");
CreateEntry(parant, prim, boole);
CreateEntry(parant, prim, chare);
CreateEntry(parant, prim, inte);
CreateEntry(parant, one_to_char, str);
CreateEntry(parant, int_to_int, int2int);
CreateEntry(parant, str_to_int, str2int);
CreateEntry(parant, int2int, square);
CreateEntry(parant, string2int, entry);
SymbolTable * child = CreateScope(parant, 14,14);
CreateEntry(child, inte, x);
SymbolTable * second = CreateScope(parant, 21,15);
CreateEntry(second, str, arg);
CreateEntry(second, inte, input);
CreateEntry(second, inte, expected);
CreateEntry(second, inte, actual);
CreateEntry(second, $_und_type, result);
SymbolTable * third = CreateScope(second, 33,44);
CreateEntry(third, BOO, arg);
CreateEntry(third, YAZOO, input);
TableNode *ret = table_lookup(third, "arg");
printf("%s == %s\n", ret->theName, "arg");
ret = table_lookup(third, "hello");
printf("This should be nil %p != %s\n", ret, "BOO");
ret = look_up(second, "input");
printf("%s == %s\n", ret->theName, "input");
ret = look_up(second, "square");
printf("%s == %s\n", ret->theName, "square");
ret = look_up(second, "spuare");
printf("This should be nil %p == %s\n", ret, "square");
print_symbol_table(parant, stderr);
free(inte);
free(boole);
free(prim);
free(str);
free(chare);
free(arg);
return 0;
}