diff --git a/src/grammar.y b/src/grammar.y index b3f2334..63f2034 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -11,35 +11,16 @@ // 4️⃣ Rule end-markers (;, |) should always be 4 spaces in. // 5️⃣ C-blocks should always be clearly defined and follow clang formatting rules. // 6️⃣ 1-line if/for/while statements must be wrapped in curly braces. -// 7️⃣ DO NOT USE TABS. EVER. +// 7️⃣ Comments should always be above rules +// 8️⃣ DO NOT USE TABS. EVER. // Please ask Scarlett if you are unsure of how to format something. Thanks! 😀 %{ - #include #include "../src/symbol_table.c" - #include - extern int yylex(void); + void yyerror(const char *err); - extern char* yytext; - extern int yyleng; - extern int yychar; - extern SymbolTable * cur; - //char* cur_value; - //char* cur_type; int token_tracker; - extern int line_number; - extern int column_number; - extern FILE * yyin; - extern TableNode* funprime; - extern TableNode* arrayprim; - extern TableNode* recprime; - extern TableNode* funtypeprime; - extern TableNode* integ; - extern TableNode* addr; - extern TableNode* chara; - extern TableNode* stri; - extern TableNode* boo; TableNode * tn; %} @@ -144,7 +125,7 @@ definition: printdebug("Currently see a record definition for %s", $2); tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); if (table_lookup(getAncestor(cur), $2) == undefined) { - printdebug("rec not found "); + printdebug("rec not found"); } } dblock @@ -196,7 +177,7 @@ definition: } if (node == undefined) { printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); - } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { + } else if (getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); } else { setStartLine(node, @1.first_line); diff --git a/src/lexicalStructure.lex b/src/lexicalStructure.lex index 920cdc4..00b5a4d 100644 --- a/src/lexicalStructure.lex +++ b/src/lexicalStructure.lex @@ -4,8 +4,8 @@ %option noyywrap %option header-file="flex.h" %option yylineno + %{ - #include #include "../tmp/grammar.tab.h" #include "../src/symbol_table.h" #ifndef DEBUG diff --git a/src/runner.c b/src/runner.c index c6f94a3..5f903c3 100644 --- a/src/runner.c +++ b/src/runner.c @@ -2,17 +2,6 @@ /* The Translators - Spring 2025 */ #include "runner.h" -extern TableNode *funprime; -extern TableNode *arrayprim; -extern TableNode *integ; -extern TableNode *addr; -extern TableNode *chara; -extern TableNode *stri; -extern TableNode *boo; -extern bool DEBUG; -extern char *COLOR_YELLOW; -extern char *COLOR_BLUE; -extern char *COLOR_WHITE; int main(int argc, char *argv[]) { // if last argument is debug then set to true and ignore it for the rest @@ -91,10 +80,12 @@ void incr(int lnum, int cnum, int tok) { // column_number += yyleng; // } } + void print_tok(int tok) { fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok, yytext); } + int run(FILE *alpha) { int token; top = cur = init(CreateScope(NULL, 1, 1)); diff --git a/src/runner.h b/src/runner.h index 9678609..cc573c5 100644 --- a/src/runner.h +++ b/src/runner.h @@ -25,30 +25,56 @@ #include #include "../tmp/flex.h" -#include "symbol_table.h" -// #include "typedefs.h" #include "../tmp/grammar.tab.h" +#include "symbol_table.h" extern int line_number, column_number; extern char *yytext; extern FILE *yyin; -int arg; +extern bool DEBUG; SymbolTable *top; SymbolTable *cur; -// int main(int argc, char* argv[]); -char *is_tok(int argc, char *argv[]); -// int is_alpha_file(char *file, int file_len); - FILE *alpha_file; FILE *tok_flag = NULL; FILE *st_flag = NULL; +bool DEBUG = false; int no_flag = 0; +int arg; + +TableNode *funprime; +TableNode *arrayprim; +TableNode *integ; +TableNode *addr; +TableNode *chara; +TableNode *stri; +TableNode *boo; +TableNode *recprime; +TableNode *funtypeprime; +TableNode *undefined; int main(int argc, char *argv[]); +int check_flag(char *arg, char *alpha); +void incr(int lnum, int cnum, int tok); +void print_tok(int tok); +int run(FILE *alpha); +bool is_help(char *input); int new_file(char *arg, char *alpha); int is_alpha_file(char *alpha, int file_len); -bool is_help(char *input); -int run(FILE *alpha); -int check_flag(char *arg, char *alpha); + +char *COLOR_RED = "\033[0;31m"; +char *COLOR_GREEN = "\033[0;32m"; +char *COLOR_ORANGE = "\033[0;33m"; +char *COLOR_BLUE = "\033[0;34m"; +char *COLOR_PURPLE = "\033[0;35m"; +char *COLOR_CYAN = "\033[0;36m"; +char *COLOR_LIGHTGRAY = "\033[0;37m"; +char *COLOR_DARKGRAY = "\033[1;30m"; +char *COLOR_LIGHTRED = "\033[1;31m"; +char *COLOR_LIGHTGREEN = "\033[1;32m"; +char *COLOR_YELLOW = "\033[1;33m"; +char *COLOR_LIGHTBLUE = "\033[1;34m"; +char *COLOR_LIGHTPURPLE = "\033[1;35m"; +char *COLOR_LIGHTCYAN = "\033[1;36m"; +char *COLOR_WHITE = "\033[1;37m"; \ No newline at end of file diff --git a/src/symbol_table.c b/src/symbol_table.c index 97b4eac..f458769 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -3,44 +3,6 @@ #include "symbol_table.h" -#include -#include -#include -#include -char *typey = "type"; -char *funy = "function"; -TableNode *funprime; -TableNode *arrayprim; -extern SymbolTable *cur; -TableNode *integ; -TableNode *addr; -TableNode *chara; -TableNode *stri; -TableNode *boo; -TableNode *recprime; -TableNode *funtypeprime; -TableNode *undefined; - -char *COLOR_RED = "\033[0;31m"; -char *COLOR_GREEN = "\033[0;32m"; -char *COLOR_ORANGE = "\033[0;33m"; -char *COLOR_BLUE = "\033[0;34m"; -char *COLOR_PURPLE = "\033[0;35m"; -char *COLOR_CYAN = "\033[0;36m"; -char *COLOR_LIGHTGRAY = "\033[0;37m"; -char *COLOR_DARKGRAY = "\033[1;30m"; -char *COLOR_LIGHTRED = "\033[1;31m"; -char *COLOR_LIGHTGREEN = "\033[1;32m"; -char *COLOR_YELLOW = "\033[1;33m"; -char *COLOR_LIGHTBLUE = "\033[1;34m"; -char *COLOR_LIGHTPURPLE = "\033[1;35m"; -char *COLOR_LIGHTCYAN = "\033[1;36m"; -char *COLOR_WHITE = "\033[1;37m"; - -bool DEBUG = false; - -void printdebug_impl(char *file, int line, const char *format, ...); - void printdebug_impl(char *file, int line, const char *format, ...) { if (DEBUG) { printf("%s<%s> [%d]%s ", COLOR_DARKGRAY, file, line, @@ -53,87 +15,6 @@ void printdebug_impl(char *file, int line, const char *format, ...) { } } -#define printdebug(format, ...) \ - printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) - -// AdInfo *Undefined_function_type_info; -typedef enum { - // First 4 below are primitive types that are all encapsulated in - // primitive type - // TYPE_INTEGER, - // TYPE_CHARACTER, - // TYPE_BOOLEAN, - // TYPE_ADDRESS, - // Type String is an array of char enclosed in double quotes per lexer - TYPE_STRING = 1, - // Array can be multidimensional. Information should be stored here. - // This is the type of the array - TYPE_ARRAY_TYPE = 2, - // Record is user defined types - TYPE_RECORD_TYPE = 3, - // Declaring what type a particular function is without as - TYPE_FUNCTION_DECLARATION = 4, - // Declaring what type a particular function is with as - // TYPE_AS_FUNCTION_DECLARATION, - // Declaring what type a function is (what the parameters and output - // are) - TYPE_FUNCTION_TYPE = 5, - // The Type being pointed to by the first 4 above that only stores the - // size - TYPE_PRIMITIVE = 6, - // likely NULL - TYPE_ALL_ELSE = 7, - TYPE_UNDEFINED = 8, - TYPE_RECORD = 9, - TYPE_ARRAY = 10 - -} types; - -/* put in symbol_table.h -typedef struct{ - int size; if(strcmp(getType(tn),getName(integ))==0){ - return - } -}primitive_info; - -typedef struct{ - int length; - char* location; -}string_info; - -typedef struct{ - int numofdimensions; - //the above value tells you how long the below array is. For example if num -of dimensions is 5, I can store 1,3,2,5,9 to define > int* sizesofdimensions; - TableNode* typeofarray; -}array_info; - -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 order specified by the user - int numofelements; - TableNode* listoftypes; -}record_info; - -typedef struct{ - int startlinenumber; - bool regularoras; -}function_declaration_info; - -typedef struct{ - TableNode* parameter; - TableNode* returntype; -}function_type_info; - -typedef union { - PrimAdInfo* primitive_info; - ArrayAdInfo* array_info; - RecAdInfo* record_info; - StringAdInfo* string_info; - FunDecAdInfo* func_dec_info; - FunTypeAdInfo* func_type_info; - }AdInfo; -*/ // primitive additional info only stores the size of that type AdInfo *CreatePrimitiveInfo(int size) { @@ -167,19 +48,10 @@ int getPrimSize(TableNode *definition) { return definition->additionalinfo->PrimAdInfo->size; } -// probably don't need the below structure since can create from an array -/*string_info* CreateStringInfo(int length, char* loc){ - string_info* stringy = (string_info*)malloc(sizeof(string_info)); - stringy.length=length; - char* location = loc; - return stringy; -} -*/ - // Only information stored in array info is the number of dimensions and the // type stored in the array per professor, the actual size of the array is // calculated at runtime so bounds checking only needs to be done then -AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) { +AdInfo *CreateArrayInfo(int dim, TableNode *type) { if (type == NULL) { printdebug("passed a NULL reference to " "CreateArrayInfo. Invalid."); @@ -866,41 +738,7 @@ SymbolTable *setColumnNumber(SymbolTable *st, int column) { st->Line_Number = column; return st; } -/* -//we use false for type defs and true for functions for parameter of typeOf -TableNode* Define(SymbolTable* table, bool typeOf, char* id) { -if(table ==NULL || table->Parent_Scope != NULL){ - printdebug("No valid table given for header defs"); - return NULL; -} - - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - -//possible issues with referencing text instead of heap -if(typeOf == 0){ - newEntry->theType = typey; -} -if (typeOf == 1){ - newEntry->theType = funy; -} -if(table_lookup(table,id) != NULL){ - printdebug("already defined at the top level, can't define duplicate -names"); return NULL; -} - 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; - } - -} -*/ // only check table that is given TableNode *table_lookup(SymbolTable *table, char *x) { if (table == NULL) { @@ -915,6 +753,7 @@ TableNode *table_lookup(SymbolTable *table, char *x) { } return undefined; } + // check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { if (table == NULL) { @@ -931,60 +770,7 @@ TableNode *look_up(SymbolTable *table, char *x) { x, getLine(table), getColumn(table)); 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; - } - if (entrie == NULL) { - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", - current_scope, parant_scope, "", "Empty Scope"); - } - for (; entrie != NULL; entrie = entrie->next) { - if (parant_scope == 0) { - /*have to update if (strcmp(entrie->theType->theName, - "function primitive") || - strcmp(entrie->theType->theName, - "array")) { - } - fprintf(file_ptr, - "%-17s: %06d : : %-21s: %-28s\n", - entrie->theName, current_scope, - entrie->theType->theName, "Extra annotation"); - } else { - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", - entrie->theName, current_scope, parant_scope, - entrie->theType->theName, "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"); - } -}*/ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (table == NULL) { @@ -1286,20 +1072,3 @@ TableNode *getNextEntry(TableNode *tn) { } return tn->next; } - -// 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); - printdebug("Line number is %d, Column number of scope is - %d",Second->Line_Number,Second->Column_Number); TableNode* First_Entry = - CreateEntry(Second,String,X); - - printdebug("The type of the first entry is %s",First_Entry->theType); - return 0; - } - */ \ No newline at end of file diff --git a/src/symbol_table.h b/src/symbol_table.h index de37098..5f87b5b 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -1,3 +1,4 @@ +#include #include #include #include @@ -9,26 +10,12 @@ typedef struct { int size; } primitive_info; -/*This structure can be subsumed into the structure below (1-d array of chars) -typedef struct{ - //shouldn't need to store any values since would be compiled at runtime - //int length; - //char* location; -}string_info; -*/ - typedef struct { int numofdimensions; - // the above value tells you how long the below array is. For example if - // num of dimensions is 5, I can store 1,3,2,5,9 to define > int* - // arr; shouldn't need to store any values (like sizes of dimenions or - // the location int* sizesofdimensions; do have to store type of array struct TableNode *typeofarray; } array_info; 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; struct SymbolTable *recordScope; } record_info; @@ -47,19 +34,16 @@ typedef union { primitive_info *PrimAdInfo; array_info *ArrayAdInfo; record_info *RecAdInfo; - // string_info* StringAdInfo; function_declaration_info *FunDecAdInfo; function_type_info *FunTypeAdInfo; } AdInfo; typedef struct ListOfTable { struct SymbolTable *table; - // struct ListOfTable* prev; struct ListOfTable *next; } ListOfTable; typedef struct TableNode { - // reference to the type entry that this is struct TableNode *theType; char *theName; AdInfo *additionalinfo; @@ -74,30 +58,100 @@ typedef struct SymbolTable { 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 +} types; + +AdInfo *CreatePrimitiveInfo(int size); +int getPrimSize(TableNode *definition); +AdInfo *CreateArrayInfo(int dim, TableNode *type); +int getNumArrDim(TableNode *definition); +TableNode *getArrType(TableNode *definition); +AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope); +int getRecLength(TableNode *definition); +SymbolTable *getRecList(TableNode *definition); +TableNode *setRecSize(TableNode *tn, int n); +int getRecSize(SymbolTable *tn); +AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular); +int getStartLine(TableNode *definition); +TableNode *setStartLine(TableNode *tn, int start); +bool getAsKeyword(TableNode *definition); +TableNode *setAsKeyword(TableNode *tn, bool as); +AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype); +TableNode *getParameter(TableNode *definition); +TableNode *getReturn(TableNode *definition); SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column); +SymbolTable *init(SymbolTable *start); +TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info); +int getAdInfoType(TableNode *tn); +TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, + AdInfo *ad); +char *getType(TableNode *tn); +char *getName(TableNode *tn); +int getLine(SymbolTable *st); +int getColumn(SymbolTable *st); +TableNode *addName(TableNode *tn, char *str); +SymbolTable *setLineNumber(SymbolTable *st, int line); +SymbolTable *setColumnNumber(SymbolTable *st, int column); 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 *removeEntry(SymbolTable *scope, char *search); +bool typeCheck(char *firstID, char *secondID); SymbolTable *getParent(SymbolTable *st); ListOfTable *getChildren(SymbolTable *st); SymbolTable *getFirstChild(ListOfTable *lt); ListOfTable *getRestOfChildren(ListOfTable *lt); TableNode *getFirstEntry(SymbolTable *st); TableNode *getNextEntry(TableNode *tn); -SymbolTable *init(SymbolTable *scope); -int getPrimSize(TableNode *definition); -int getNumArrDim(TableNode *definition); -TableNode *getArrType(TableNode *definition); -int getRecLength(TableNode *definition); -SymbolTable *getRecList(TableNode *definition); -int getStartLine(TableNode *definition); -bool getAsKeyword(TableNode *definition); -TableNode *getParameter(TableNode *definition); -TableNode *getReturn(TableNode *definition); -char *getType(TableNode *tn); -char *getName(TableNode *tn); -int getLine(SymbolTable *st); -int getColumn(SymbolTable *st); +void printdebug_impl(char *file, int line, const char *format, ...); +#define printdebug(format, ...) \ + printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) + +extern int yylex(void); +extern char *yytext; +extern int yyleng; +extern int yychar; +extern SymbolTable *cur; +extern int line_number; +extern int column_number; +extern FILE *yyin; +extern bool DEBUG; + +extern TableNode *funprime; +extern TableNode *arrayprim; +extern TableNode *integ; +extern TableNode *addr; +extern TableNode *chara; +extern TableNode *stri; +extern TableNode *boo; +extern TableNode *recprime; +extern TableNode *funtypeprime; +extern TableNode *undefined; + +extern char *COLOR_RED; +extern char *COLOR_GREEN; +extern char *COLOR_ORANGE; +extern char *COLOR_BLUE; +extern char *COLOR_PURPLE; +extern char *COLOR_CYAN; +extern char *COLOR_LIGHTGRAY; +extern char *COLOR_DARKGRAY; +extern char *COLOR_LIGHTRED; +extern char *COLOR_LIGHTGREEN; +extern char *COLOR_YELLOW; +extern char *COLOR_LIGHTBLUE; +extern char *COLOR_LIGHTPURPLE; +extern char *COLOR_LIGHTCYAN; +extern char *COLOR_WHITE; \ No newline at end of file