Fixed -tok, spacings in -st, and validation tests

This commit is contained in:
Scarlett
2025-04-16 11:44:02 -04:00
parent b023ac0133
commit 5a23ef2756
26 changed files with 677 additions and 477 deletions

View File

@@ -111,6 +111,13 @@ int run(FILE *alpha) {
yyin = alpha;
yyparse();
if (tok_flag != NULL) {
while (0 != (token = yylex())) {
// Don't delete me 🥺
}
fclose(tok_flag);
}
if (st_flag != NULL) {
print_symbol_table(top, st_flag);
fclose(st_flag);
@@ -136,8 +143,6 @@ int run(FILE *alpha) {
fclose(cg_flag);
}
//yyparse();
if (yyin != NULL) {
fclose(yyin);
}

View File

@@ -5,11 +5,11 @@
#define TOK_LEN 3
#define ST_LEN 2
#define TC_LEN 2
#define HELP \
"HELP:\n" \
#define HELP \
"HELP:\n" \
" How to run the alpha compiler:\n" \
" ./alpha [options] program\n" \
"Valid options:\n" \
" ./alpha [options] program\n" \
"Valid options:\n" \
" -tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n" \
" -st output the symbol table for the program to the .st file\n" \
" -asc output the annotated source code for the program to the .asc file, including syntax errors\n" \

View File

@@ -92,9 +92,9 @@ int getNumArrDim(TableNode *definition) {
"function. Invalid.");
return -1;
}
if(getAdInfoType(definition) != TYPE_ARRAY_TYPE){
if (getAdInfoType(definition) != TYPE_ARRAY_TYPE) {
printdebug(
"passed an invalid node to getNumArrDim. Seeing tag %d in getNumArrDim. Invalid.",getAdInfoType(definition));
"passed an invalid node to getNumArrDim. Seeing tag %d in getNumArrDim. Invalid.", getAdInfoType(definition));
return -1;
}
return definition->additionalinfo->ArrayAdInfo->numofdimensions;
@@ -114,9 +114,9 @@ TableNode *getArrType(TableNode *definition) {
"function. Invalid.");
return undefined;
}
if(getAdInfoType(definition) != TYPE_ARRAY_TYPE){
if (getAdInfoType(definition) != TYPE_ARRAY_TYPE) {
printdebug(
"passed an invalid node to getArrType. Seeing tag %d. Invalid.",getAdInfoType(definition));
"passed an invalid node to getArrType. Seeing tag %d. Invalid.", getAdInfoType(definition));
return undefined;
}
return definition->additionalinfo->ArrayAdInfo->typeofarray;
@@ -138,25 +138,25 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) {
// Perhaps this may not be needed since we need to iterate over all elements
// anyways.
int getRecTotal(TableNode* node){
if(node == NULL){
int getRecTotal(TableNode *node) {
if (node == NULL) {
printdebug(
"passed a NULL node to getRecTotal. Invalid.");
return -1;
return -1;
}
if(getAdInfoType(node) != TYPE_RECORD_TYPE){
if (getAdInfoType(node) != TYPE_RECORD_TYPE) {
printdebug(
"passed an invalid node to getRecTotal. Invalid.");
return -1;
}
if(node->additionalinfo == NULL){
if (node->additionalinfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return -1;
}
return node->additionalinfo->RecAdInfo->total_size;
}
TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
TableNode *setRecOffsetInfo(SymbolTable *scope, TableNode *node) {
if (node == NULL) {
printdebug(
"passed a NULL node to setRecOffsetInfo. Invalid.");
@@ -167,51 +167,47 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
"passed an NULL scope to setRecOffsetInfo. Invalid.");
return undefined;
}
if(getFirstEntry(scope) == NULL){
if (getFirstEntry(scope) == NULL) {
printdebug(
"passed an empty scope to setRecOffsetInfo. Invalid.");
return undefined;
}
TableNode* this = getFirstEntry(scope);
TableNode *this = getFirstEntry(scope);
int largest = 0;
int k = getRecLength(node);
int total_size = 0;
int counter = 0;
int *offsets = (int *)calloc(2 * k, sizeof(int));
if(getAdInfoType(this) == TYPE_FUNCTION_DECLARATION){
if (getAdInfoType(this) == TYPE_FUNCTION_DECLARATION) {
offsets[counter] = 8;
total_size = total_size + offsets[counter];
largest = 8;
counter++;
}
else if((getAdInfoType(this) == TYPE_RECORD)){
} else if ((getAdInfoType(this) == TYPE_RECORD)) {
offsets[counter] = 8;
printf("hitting record and adding to largest");
total_size = total_size + offsets[counter];
largest = offsets[counter];
counter++;
}
else if(getAdInfoType(this)==TYPE_PRIMITIVE){
} else if (getAdInfoType(this) == TYPE_PRIMITIVE) {
offsets[counter] = getPrimSize(getTypeEntry(this));
total_size = total_size + offsets[counter];
largest = offsets[counter];
counter++;
}
else if(getAdInfoType(this)==TYPE_ARRAY){
} else if (getAdInfoType(this) == TYPE_ARRAY) {
offsets[counter] = 8;
total_size = total_size + offsets[counter];
largest = offsets[counter];
counter++;
}
else {
} else {
printdebug(
"[TYPE CHECK] passed an invalid (first) parameter to a function definition. seeing %d. Type of entry is %s. Name attempted to pass is %s.",getAdInfoType(this),getType(this),getName(this));
"[TYPE CHECK] passed an invalid (first) parameter to a function definition. seeing %d. Type of entry is %s. Name attempted to pass is %s.", getAdInfoType(this), getType(this), getName(this));
return undefined;
}
this = getNextEntry(this);
while(this != NULL){
if(getAdInfoType(this) == TYPE_FUNCTION_DECLARATION){
while (this != NULL) {
if (getAdInfoType(this) == TYPE_FUNCTION_DECLARATION) {
int s = 8;
if (s > largest) {
largest = s;
@@ -225,8 +221,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}
else if(getAdInfoType(this) == TYPE_ARRAY){
} else if (getAdInfoType(this) == TYPE_ARRAY) {
int s = 8;
if (s > largest) {
largest = s;
@@ -240,8 +235,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}
else if((getAdInfoType(this) == TYPE_RECORD)){
} else if ((getAdInfoType(this) == TYPE_RECORD)) {
int s = 8;
if (s > largest) {
largest = s;
@@ -257,8 +251,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}
else if(getAdInfoType(this) == TYPE_PRIMITIVE){
} else if (getAdInfoType(this) == TYPE_PRIMITIVE) {
int s = getPrimSize(getTypeEntry(this));
if (s > largest) {
largest = s;
@@ -272,9 +265,9 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}else{
} else {
printdebug(
"[TYPE CHECK] passed an invalid parameter at position %d in record.",((counter+1)/2));
"[TYPE CHECK] passed an invalid parameter at position %d in record.", ((counter + 1) / 2));
return undefined;
}
}
@@ -286,18 +279,18 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
return node;
}
int* getRecOffsets(TableNode* node){
if(node == NULL){
int *getRecOffsets(TableNode *node) {
if (node == NULL) {
printdebug(
"passed a NULL node to getRecTotal. Invalid.");
return NULL;
return NULL;
}
if(getAdInfoType(node) != TYPE_RECORD_TYPE){
if (getAdInfoType(node) != TYPE_RECORD_TYPE) {
printdebug(
"passed an invalid node to getRecTotal. Invalid.");
return NULL;
}
if(node->additionalinfo == NULL){
if (node->additionalinfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return NULL;
@@ -341,7 +334,8 @@ SymbolTable *getRecList(TableNode *definition) {
if (strcmp(getType(definition), "record") != 0) {
printdebug(
"not checking the list of types of a record -- invalid "
"op of type %s", getType(definition));
"op of type %s",
getType(definition));
return NULL;
}
return definition->additionalinfo->RecAdInfo->recordScope;
@@ -521,7 +515,7 @@ TableNode *getParameter(TableNode *definition) {
"function. Invalid.");
return undefined;
}
if(definition->additionalinfo == NULL){
if (definition->additionalinfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return undefined;
@@ -552,7 +546,7 @@ TableNode *getReturn(TableNode *definition) {
"not checking the return of a function -- invalid op");
return undefined;
}
if(definition->additionalinfo == NULL){
if (definition->additionalinfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return undefined;
@@ -605,10 +599,10 @@ SymbolTable *init(SymbolTable *start) {
chara = (TableNode *)calloc(1, sizeof(TableNode));
stri = (TableNode *)calloc(1, sizeof(TableNode));
boo = (TableNode *)calloc(1, sizeof(TableNode));
TableNode* reservetype = (TableNode *)calloc(1, sizeof(TableNode));
TableNode* reserve = (TableNode *)calloc(1, sizeof(TableNode));
TableNode* releasetype = (TableNode *)calloc(1, sizeof(TableNode));
TableNode* release = (TableNode *)calloc(1, sizeof(TableNode));
TableNode *reservetype = (TableNode *)calloc(1, sizeof(TableNode));
TableNode *reserve = (TableNode *)calloc(1, sizeof(TableNode));
TableNode *releasetype = (TableNode *)calloc(1, sizeof(TableNode));
TableNode *release = (TableNode *)calloc(1, sizeof(TableNode));
// TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable));
start->entries = integ;
integ->next = addr;
@@ -721,7 +715,7 @@ SymbolTable *init(SymbolTable *start) {
integ->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for integ
addr->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for addr
chara->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for chara
stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri
stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri
boo->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for boo
reserve->tag = TYPE_FUNCTION_DECLARATION;
reservetype->tag = TYPE_FUNCTION_TYPE;
@@ -1064,7 +1058,7 @@ TableNode *table_lookup(SymbolTable *table, char *x) {
// check current table and all parents
TableNode *look_up(SymbolTable *table, char *x) {
if (table == NULL) {
printdebug("Could not find %s in any scope using lookup",x);
printdebug("Could not find %s in any scope using lookup", x);
return undefined;
}
TableNode *ret = table_lookup(table, x);
@@ -1157,33 +1151,33 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
}
}
if (getAdInfoType(entry) == TYPE_ARRAY) {
char *arrayType = (char *)malloc(100);
//sprintf(arrayType, " %d -> %s", getNumArrDim(entry),
// getName(getArrType(entry)));
char *arrayType = (char *)malloc(sizeof(getType(entry) + 1));
sprintf(arrayType, " %s", getType(entry));
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Array Instance");
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, arrayType, " Array Instance");
} else {
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), " Array Instance");
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, arrayType, " Array Instance");
}
}
if (getAdInfoType(entry) == TYPE_RECORD_TYPE) {
char *recordAdInfo = (char *)malloc(100);
sprintf(recordAdInfo, " elements-%d size-%d bytes", getRecLength(entry), getRecTotal(entry));
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " record type", recordAdInfo);
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Record Type", recordAdInfo);
} else {
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " record type", recordAdInfo);
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " Record Type", recordAdInfo);
}
}
if (getAdInfoType(entry) == TYPE_RECORD) {
char *recordAdInfo = (char *)malloc(100);
sprintf(recordAdInfo, " elements-%d", getRecLength(entry));
char *recordType = (char *)malloc(sizeof(getType(entry) + 1));
sprintf(recordType, " %s", getType(entry));
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), "record instance");
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, recordType, " Record Instance");
} else {
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), "record instance");
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, recordType, " Record Instance");
}
}
@@ -1193,8 +1187,6 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive Type", primAdInfo);
} else {
//printdebug("%sTHIS ONE", COLOR_RED);
printTableNode(entry);
char *primType = (char *)malloc(sizeof(getType(entry) + 1));
sprintf(primType, " %s", getType(entry));
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, primAdInfo);
@@ -1206,11 +1198,9 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive", primAdInfo);
} else {
printdebug("%sTHIS ONE", COLOR_RED);
printTableNode(entry);
char *primType = (char *)malloc(sizeof(getType(entry) + 1));
sprintf(primType, " %s", getType(entry));
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), "Primitive Instance");
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, " Primitive Instance");
}
}
@@ -1226,10 +1216,12 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
}
if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) {
char *functiontype = (char *)malloc(100);
sprintf(functiontype, " %s", getName(getReturn(entry)));
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Function Definition");
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition");
} else {
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), " Function Definition");
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, " Function Definition");
}
}
@@ -1393,7 +1385,7 @@ TableNode *getNextEntry(TableNode *tn) {
// Uses pointers to the table node to print the info
TableNode *printTableNode(TableNode *tn) {
if (DEBUG == 0) {
return tn;
return tn;
}
if (tn == NULL) {

View File

@@ -7,80 +7,79 @@
#define SIZE_INT 4
#define SIZE_ADDR 8
#define SIZE_CHAR 1
#define SIZE_BOOL 4 //TODO: Ask Carl what this size should be
#define SIZE_BOOL 4 //TODO: Ask Carl what this size should be
struct TableNode;
typedef struct {
int size;
int size;
} primitive_info;
typedef struct {
int numofdimensions;
struct TableNode *typeofarray;
int numofdimensions;
struct TableNode *typeofarray;
} array_info;
typedef struct {
int numofelements;
struct SymbolTable *recordScope;
int total_size;
int* offsets;
int numofelements;
struct SymbolTable *recordScope;
int total_size;
int *offsets;
} record_info;
typedef struct {
int startlinenumber;
bool regularoras;
int startlinenumber;
bool regularoras;
} function_declaration_info;
typedef struct {
struct TableNode *parameter;
struct TableNode *returntype;
struct TableNode *parameter;
struct TableNode *returntype;
} function_type_info;
typedef union {
primitive_info *PrimAdInfo;
array_info *ArrayAdInfo;
record_info *RecAdInfo;
function_declaration_info *FunDecAdInfo;
function_type_info *FunTypeAdInfo;
primitive_info *PrimAdInfo;
array_info *ArrayAdInfo;
record_info *RecAdInfo;
function_declaration_info *FunDecAdInfo;
function_type_info *FunTypeAdInfo;
} AdInfo;
typedef struct ListOfTable {
struct SymbolTable *table;
struct ListOfTable *next;
struct SymbolTable *table;
struct ListOfTable *next;
} ListOfTable;
//Table node to store
typedef struct TableNode {
struct TableNode *theType;
int tag;
char *theName;
AdInfo *additionalinfo;
struct TableNode *next;
struct TableNode *theType;
int tag;
char *theName;
AdInfo *additionalinfo;
struct TableNode *next;
} TableNode;
typedef struct SymbolTable {
TableNode *entries;
struct SymbolTable *Parent_Scope;
struct ListOfTable *Children_Scope;
int Line_Number;
int Column_Number;
TableNode *entries;
struct SymbolTable *Parent_Scope;
struct ListOfTable *Children_Scope;
int Line_Number;
int Column_Number;
} SymbolTable;
typedef enum {
TYPE_STRING = 1,
TYPE_ARRAY_TYPE = 2,
TYPE_RECORD_TYPE = 3,
TYPE_FUNCTION_DECLARATION = 4,
TYPE_FUNCTION_TYPE = 5,
TYPE_PRIMITIVE = 6,
TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8,
TYPE_RECORD = 9,
TYPE_ARRAY = 10,
TYPE_SYSTEM_DEFINED = 11,
TYPE_PRIMITIVE_TYPE = 12
TYPE_STRING = 1,
TYPE_ARRAY_TYPE = 2,
TYPE_RECORD_TYPE = 3,
TYPE_FUNCTION_DECLARATION = 4,
TYPE_FUNCTION_TYPE = 5,
TYPE_PRIMITIVE = 6,
TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8,
TYPE_RECORD = 9,
TYPE_ARRAY = 10,
TYPE_SYSTEM_DEFINED = 11,
TYPE_PRIMITIVE_TYPE = 12
} types;
AdInfo *CreatePrimitiveInfo(int size);
@@ -128,10 +127,10 @@ ListOfTable *getRestOfChildren(ListOfTable *lt);
TableNode *getFirstEntry(SymbolTable *st);
TableNode *getNextEntry(TableNode *tn);
TableNode * printTableNode(TableNode * tn);
TableNode *printTableNode(TableNode *tn);
void printdebug_impl(char *file, int line, const char *format, ...);
#define printdebug(format, ...) \
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
#define printdebug(format, ...) \
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
extern int yylex(void);
extern char *yytext;