restructured table, still have to update print table properly as well as grammar rules

This commit is contained in:
Partho Bhattacharya
2025-03-14 13:35:25 -04:00
parent f0e0d7bdbc
commit 63534d1daf
2 changed files with 47 additions and 37 deletions

View File

@ -77,10 +77,12 @@ typedef union {
}AdInfo; }AdInfo;
*/ */
primitive_info* CreatePrimitiveInfo(size){ AdInfo* CreatePrimitiveInfo(int size){
primitive_info* prim = (primitive_info*)malloc(sizeof(primitive_info));
prim.size=size; AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
return prim; info->PrimAdInfo = (primitive_info*)malloc(sizeof(primitive_info));
info->PrimAdInfo->size=size;
return info;
} }
//probably don't need the below structure since can create from an array //probably don't need the below structure since can create from an array
@ -91,35 +93,39 @@ primitive_info* CreatePrimitiveInfo(size){
return stringy; return stringy;
} }
*/ */
array_info* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){ AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
array_info* arr = (array_info*)malloc(sizeof(array_info)); AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
arr.numofdimensions=dim; info->ArrayAdInfo = (array_info*)malloc(sizeof(array_info));
arr->typeofarray=type; info->ArrayAdInfo->numofdimensions=dim;
info->ArrayAdInfo->typeofarray=type;
//avoiding storing any types like below //avoiding storing any types like below
//int* dimensionsizes = loc; //int* dimensionsizes = loc;
return arr; return info;
} }
record_info* CreateRecordInfo(int length, TableNode* typesarray){ AdInfo* CreateRecordInfo(int length, TableNode* typesarray){
record_info* reccy = (record_info*)malloc(sizeof(record_info)); AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
reccy.numofelements=length; info->RecAdInfo = (record_info*)malloc(sizeof(record_info));
reccy->listoftypes = typesarray; info->RecAdInfo->numofelements=length;
return reccy; info->RecAdInfo->listoftypes = typesarray;
return info;
} }
//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
function_declaration_info* CreateFunctionDeclarationInfo(int line, bool asorregular){ AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
function_declaration_info* fundec = (function_declaration_info*)malloc(sizeof(function_declaration_info)); AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
fundec.startlinenumber=line; info->FunDecAdInfo = (function_declaration_info*)malloc(sizeof(function_declaration_info));
fundec.regularoras = asorregular; info->FunDecAdInfo->startlinenumber=line;
return fundec; info->FunDecAdInfo->regularoras = asorregular;
return info;
} }
function_type_info* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){ AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
function_type_info* funtype = (function_type_info*)malloc(sizeof(function_type_info)); AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
funtype->parameter=parameter; info->FunTypeAdInfo = (function_type_info*)malloc(sizeof(function_type_info));
funtype->returntype = returntype; info->FunTypeAdInfo->parameter=parameter;
return funtype; info->FunTypeAdInfo->returntype = returntype;
return info;
} }
@ -205,9 +211,9 @@ SymbolTable* init(SymbolTable* start){
integ->theType=prime; integ->theType=prime;
addr->theType=prime; addr->theType=prime;
chara->theType=prime; chara->theType=prime;
stri->theType=striprim; stri->theType=arrayprim;
boo->theType=prime; boo->theType=prime;
arr->theType=arrayprim; //arr->theType=arrayprim;
//filling in all the values for the additional info for initial types //filling in all the values for the additional info for initial types
integ->additionalinfo = CreatePrimitiveInfo(4); integ->additionalinfo = CreatePrimitiveInfo(4);
@ -329,7 +335,8 @@ void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
} }
for (; entrie != NULL; entrie = entrie->next) { for (; entrie != NULL; entrie = entrie->next) {
if (parant_scope == 0) { if (parant_scope == 0) {
if(strcmp(entrie->theType->theName,"function primitive")|| strccmp(entrie->theType->theName," /*have to update*/ if(strcmp(entrie->theType->theName,"function primitive")|| strcmp(entrie->theType->theName,"array")){
}
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
entrie->theName, current_scope, entrie->theType->theName, entrie->theName, current_scope, entrie->theType->theName,
"Extra annotation"); "Extra annotation");

View File

@ -3,6 +3,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
struct TableNode;
typedef struct{ typedef struct{
int size; int size;
}primitive_info; }primitive_info;
@ -21,12 +23,13 @@ typedef struct{
//shouldn't need to store any values (like sizes of dimenions or the location //shouldn't need to store any values (like sizes of dimenions or the location
//int* sizesofdimensions; //int* sizesofdimensions;
//do have to store type of array //do have to store type of array
TableNode* typeofarray; struct TableNode* typeofarray;
}array_info; }array_info;
typedef struct{ typedef struct{
//similar to above we define a record to hold the number of elements and an array of tablenodes (types) that it contains in the > int numofelements; //similar to above we define a record to hold the number of elements and an array of tablenodes (types) that it contains in the >
TableNode* listoftypes; int numofelements;
struct TableNode* listoftypes;
}record_info; }record_info;
typedef struct{ typedef struct{
@ -35,8 +38,8 @@ typedef struct{
}function_declaration_info; }function_declaration_info;
typedef struct{ typedef struct{
TableNode* parameter; struct TableNode* parameter;
TableNode* returntype; struct TableNode* returntype;
}function_type_info; }function_type_info;
typedef union { typedef union {
@ -44,8 +47,8 @@ typedef union {
array_info* ArrayAdInfo; array_info* ArrayAdInfo;
record_info* RecAdInfo; record_info* RecAdInfo;
//string_info* StringAdInfo; //string_info* StringAdInfo;
func_dec_info* FunDecAdInfo; function_declaration_info* FunDecAdInfo;
func_type_info* FunTypeAdInfo; function_type_info* FunTypeAdInfo;
}AdInfo; }AdInfo;
typedef struct ListOfTable { typedef struct ListOfTable {
@ -56,11 +59,11 @@ typedef struct ListOfTable {
typedef struct TableNode { typedef struct TableNode {
//reference to the type entry that this is //reference to the type entry that this is
TableNode* theType; struct TableNode* theType;
char* theName; char* theName;
AdInfo* additionalinfo; AdInfo* additionalinfo;
struct TableNode* next; struct TableNode* next;
} TableNode; }TableNode;
typedef struct SymbolTable { typedef struct SymbolTable {
TableNode* entries; TableNode* entries;
@ -71,7 +74,6 @@ typedef struct SymbolTable {
} SymbolTable; } SymbolTable;
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column);
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id);
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); void print_symbol_table(SymbolTable* table, FILE* file_ptr);
@ -83,6 +85,7 @@ SymbolTable* getFirstChild(ListOfTable* lt);
ListOfTable* getRestOfChildren(ListOfTable* lt); ListOfTable* getRestOfChildren(ListOfTable* lt);
TableNode* getFirstEntry(SymbolTable* st); TableNode* getFirstEntry(SymbolTable* st);
TableNode* getNextEntry(TableNode* tn); TableNode* getNextEntry(TableNode* tn);
SymbolTable* init(SymbolTable* scope);
char* getType(TableNode* tn); char* getType(TableNode* tn);
char* getName(TableNode* tn); char* getName(TableNode* tn);
int getLine(SymbolTable* st); int getLine(SymbolTable* st);