From 7836f6ecd0866854848950797d652f59c1be431a Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 01:18:22 -0500 Subject: [PATCH 01/16] started symbol table file --- symbol_table.c | 50 +++++++++++++++++++++++++++++++++++ symbol_table.h | 0 test_comment_issues.tok | 0 tests/test_comment_issues.tok | 0 4 files changed, 50 insertions(+) create mode 100644 symbol_table.c create mode 100644 symbol_table.h create mode 100644 test_comment_issues.tok create mode 100644 tests/test_comment_issues.tok diff --git a/symbol_table.c b/symbol_table.c new file mode 100644 index 0000000..31d2945 --- /dev/null +++ b/symbol_table.c @@ -0,0 +1,50 @@ +//Defining a symbol table +//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 "symbol_table.h" + +typedef struct ListOfTable{ + struct SymbolTable* table; + struct ListOfTable* prev; + struct ListOfTable* next; +}ListOfTable; + +typedef union Value{ + int* value_of_int; + void* value_of_pointer; + bool* value_of_bool; + char* value_of_char; + int* size_of_char_array; + }Value; + +typedef struct TableNode{ + string* theType; + string* theName; + Value* value; + struct TableNode* next; +}TableNode; + +typedef struct SymbolTable{ + TableNode* entries; + struct SymbolTable* Parent_Scope; + struct ListOfTable* Children_Scope; +}SymbolTable; + +SymbolTable* CreateScope(SymbolTable* ParentScope){ + SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); + table.Parent_Scope = ParentScope; + table.Children_Scope = NULL; + table.entries = NULL; + return table; +} + +Entry* CreateEntry(SymbolTable* table, string typeOf, string id, Value value){ + if( + + + + + + + diff --git a/symbol_table.h b/symbol_table.h new file mode 100644 index 0000000..e69de29 diff --git a/test_comment_issues.tok b/test_comment_issues.tok new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_comment_issues.tok b/tests/test_comment_issues.tok new file mode 100644 index 0000000..e69de29 From 5f35308361a2f594c0145aa45e5b55633a501170 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 15:19:50 -0500 Subject: [PATCH 02/16] continued working on table structure --- symbol_table.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/symbol_table.c b/symbol_table.c index 31d2945..196bf06 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -8,6 +8,8 @@ typedef struct ListOfTable{ struct SymbolTable* table; struct ListOfTable* prev; struct ListOfTable* next; + int Line_Number; + int Column_Number; }ListOfTable; typedef union Value{ @@ -15,7 +17,6 @@ typedef union Value{ void* value_of_pointer; bool* value_of_bool; char* value_of_char; - int* size_of_char_array; }Value; typedef struct TableNode{ @@ -23,6 +24,8 @@ typedef struct TableNode{ string* theName; Value* value; struct TableNode* next; + //this next value is an int for string types to tell you how far to traverse a buffer for the string + int StrLength; }TableNode; typedef struct SymbolTable{ @@ -31,18 +34,23 @@ typedef struct SymbolTable{ struct ListOfTable* Children_Scope; }SymbolTable; -SymbolTable* CreateScope(SymbolTable* ParentScope){ +SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); + table.Line_Number = Line; + table.Column_Number = Column; table.Parent_Scope = ParentScope; table.Children_Scope = NULL; table.entries = NULL; return table; } -Entry* CreateEntry(SymbolTable* table, string typeOf, string id, Value value){ - if( - - +TableNode* CreateEntry(SymbolTable* table, string typeOf, string id, Value value, int StringLength){ + if(table.entries == NULL){ + TableNode* newEntry = (TableNode*)malloc(sizeof(SymbolTable)); + newEntry.theType = typeOf; + newEntry.theName = id; + newEntry.StrLength = StringLength; + } else{ From 0aea539095ee52a937e12a70b3e02bf986361c96 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 20 Feb 2025 16:07:44 -0500 Subject: [PATCH 03/16] implemented -st and -tok functionality, updated -help --- Makefile | 1 + runner.c | 175 ++++++++++++++++++++++++++++++++++++------------------- runner.h | 23 ++++---- 3 files changed, 128 insertions(+), 71 deletions(-) diff --git a/Makefile b/Makefile index 90bdaca..7d58bb2 100644 --- a/Makefile +++ b/Makefile @@ -44,3 +44,4 @@ clean: rm -f $(EXE) rm -f flex.h rm -f *.tok + rm -f *.st diff --git a/runner.c b/runner.c index c927d71..85e8214 100644 --- a/runner.c +++ b/runner.c @@ -2,96 +2,151 @@ int main(int argc, char *argv[]) { - char *check_input; - int token; - FILE *output; - - if (argc == 1 || argc > 3) { - fprintf(stderr, "invalid input with 1, >3, or non .alpha arg \n"); - return -1; //no alpha file or too many args + if (argc == 1) { + fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs \n"); + return -1; } else if (argc == 2) { + //can be either -help or .alpha file if (is_help(argv[1])) { return 0; - } else if (is_alpha_file(argv[1], strlen(argv[1])) != 0) { + } else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) { + //run flex for now + no_flag = SET_FLAG; //no argument but valid input + alpha_file = fopen(argv[1], "r"); + + } else { + fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs\n"); return -1; } - arg = NO_ARG; //no argument but valid input - yyin = fopen(argv[1], "r"); } else { - check_input = is_tok(argc, argv); - if (strcmp(CHECK_OTHER, check_input) == 0 || strcmp(INVALID_ARG, check_input) == 0) { - fprintf(stderr, "check_other or invalid_arg \n"); - return -1; //invalid argument (need to update as we add more valid arguments) + //last input must be .alpha + if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) { + fprintf(stderr, "INVALID INPUT: Include a .alpha file at end of input or use -help for more inputs \n"); + return -1; + } else { + //now check that other args are valid (flags will not be null if flag is present) + for (int i = 1; i < argc - 1; i++) { + if (check_flag(argv[i], argv[argc - 1]) != 0) { + fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n"); + return -1; + } + } + alpha_file = fopen(argv[argc - 1], "r"); } - output = fopen(check_input, "w"); - arg = TOK_ARG; //it is a -tok arg with a valid alpha file at argv[2] - yyin = fopen(argv[2], "r"); } - while (0 != (token = yylex())) { - if (arg == TOK_ARG) { - fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext); + return run(alpha_file); +} + +int check_flag(char *arg, char* alpha) { + if (strcmp("-tok", arg) == 0) { + if (tok_flag == NULL) { + return new_file(arg, alpha); } - if(token == COMMENT){ - for (int i = 0; i < yyleng; i++) { - if(yytext[i] == '\n'){ - line_number++; - column_number = 0; - } - column_number++; - } - continue; + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else if (strcmp("-st", arg) == 0) { + if (st_flag == NULL) { + return new_file(arg, alpha); + } + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else { + fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n"); + return -1; + } +} + + +int run(FILE *alpha) { + int token; + //check that file exists + if (alpha == NULL) { + fprintf(stderr, "INPUT FILE NOT FOUND\n"); + return -1; + } + yyin = alpha; + while (0 != (token = yylex())) { + if (tok_flag != NULL) { + fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext); + } + if(token == COMMENT){ + for (int i = 0; i < yyleng; i++) { + if(yytext[i] == '\n'){ + line_number++; + column_number = 0; + } + column_number++; + } + continue; } column_number += yyleng; } + if (st_flag != NULL) { + //output symbol table, file pointer is + fprintf(st_flag, "just checking that this works \n"); + } + if (yyin != NULL) { fclose(yyin); } - if (output != NULL && arg == TOK_ARG) { - fclose(output); + + if (tok_flag != NULL) { + fclose(tok_flag); } + return 0; } + + bool is_help(char * input) { if (strcmp(input, "-help") == 0) { - printf("HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid options:\n-tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n"); + printf("%s", HELP); return true; } return false; } -char *is_tok(int argc, char *argv[]) { - if (argc == 3 && strcmp("-tok", argv[1]) == 0) { - char *input_prog = argv[2]; - int file_len = strlen(input_prog); - //check that input program is a .alpha file - if (is_alpha_file(input_prog, file_len) != 0) { - return INVALID_ARG; - } - - const char *basename = input_prog; - const char *slash = strrchr(input_prog, '/'); - if (slash != NULL) { - basename = slash + 1; - } - - // Calculate lengths - int basename_len = strlen(basename); - char* FILE_tok = calloc(basename_len - ALPHA_OFFSET + TOK_LEN + 2, sizeof(char)); - - // Copy filename and add .tok extension - strncpy(FILE_tok, basename, basename_len - ALPHA_OFFSET); - //fprintf(stderr, "hello"); - strcat(FILE_tok, ".tok"); - - return FILE_tok; +int new_file(char *arg, char *alpha) { + int type_len; + const char *basename = alpha; + const char *slash = strchr(alpha, '/'); + while (slash != NULL) { + basename = slash + 1; + slash = strchr(basename, '/'); } - return CHECK_OTHER; + if (strcmp(arg, "-tok") == 0) { + type_len = TOK_LEN; + } else if (strcmp(arg, "-st") == 0) { + type_len = ST_LEN; + } else { + fprintf(stderr, "INVALID FLAG: Use -help to view valid inputs\n"); + return -1; + } + + + // calculate lengths + int basename_len = strlen(basename); + char *file_name = calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char)); + + //coy filename and add extension + strncpy(file_name, basename, basename_len - ALPHA_OFFSET); + strcat(file_name, "."); + strcat(file_name, arg + 1); + + + if (strcmp(arg, "-tok") == 0) { + tok_flag = fopen(file_name, "w"); + } else if (strcmp(arg, "-st") == 0) { + st_flag = fopen(file_name, "w"); + } + return 0; } -int is_alpha_file(char *file, int file_len) { - if (strcmp(".alpha", file + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) { + +int is_alpha_file(char *alpha, int file_len) { + if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) { return -1; //not alpha file } return 0; //is alpha file diff --git a/runner.h b/runner.h index c96c3df..4ac136d 100644 --- a/runner.h +++ b/runner.h @@ -1,14 +1,9 @@ #define ALPHA_OFFSET 6 #define TOK_LEN 3 - -//returns for is_tok -#define INVALID_ARG "invalid" -#define CHECK_OTHER "diff" - -//argument type in main -#define NO_ARG 0 -#define DIFF_ARG 1 -#define TOK_ARG 2 +#define ST_LEN 2 +#define HELP "HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid 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" +//use to set flags for arg types +#define SET_FLAG 1 #include #include @@ -21,8 +16,14 @@ extern int line_number, column_number; extern char *yytext; extern FILE *yyin; int arg; +FILE *alpha_file; +FILE *tok_flag = NULL; +FILE *st_flag = NULL; +int no_flag = 0; int main(int argc, char* argv[]); -char *is_tok(int argc, char* argv[]); -int is_alpha_file(char *file, int file_len); +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); From fdb8f493e17c7eb67294eef1437b746e0dd00684 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 20 Feb 2025 16:14:50 -0500 Subject: [PATCH 04/16] include -help description in -help --- runner.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runner.h b/runner.h index 4ac136d..a5c12fb 100644 --- a/runner.h +++ b/runner.h @@ -1,7 +1,7 @@ #define ALPHA_OFFSET 6 #define TOK_LEN 3 #define ST_LEN 2 -#define HELP "HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid 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" +#define HELP "HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid 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-help print this message and exit the alpha compiler\n" //use to set flags for arg types #define SET_FLAG 1 From b21d7039a2e2e68b86d6a49e1108f80a7c731617 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 17:50:25 -0500 Subject: [PATCH 05/16] finished base code for table setup. Still have to check grammar --- symbol_table.c | 57 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/symbol_table.c b/symbol_table.c index 196bf06..70cb359 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -6,10 +6,9 @@ typedef struct ListOfTable{ struct SymbolTable* table; - struct ListOfTable* prev; + //struct ListOfTable* prev; struct ListOfTable* next; - int Line_Number; - int Column_Number; + }ListOfTable; typedef union Value{ @@ -32,27 +31,55 @@ typedef struct SymbolTable{ TableNode* entries; struct SymbolTable* Parent_Scope; struct ListOfTable* Children_Scope; + int Line_Number; + int Column_Number; }SymbolTable; SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); - table.Line_Number = Line; - table.Column_Number = Column; - table.Parent_Scope = ParentScope; - table.Children_Scope = NULL; - table.entries = NULL; + table->Line_Number = Line; + table->Column_Number = Column; + table->Parent_Scope = ParentScope; + table->Children_Scope = NULL; + table->entries = NULL; + if(ParentScope->Children_scope == NULL){ + ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); + newEntry->next = NULL; + //newEntry->prev = NULL; + newEntry->table = table; + ParentScope->Children_scope = newEntry; + } else{ + ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); + //newEntry->prev = NULL; + newEntry->table= table; + ListOfTable* oldEntry = ParentScope->Children_scope; + ParentScope->Children_scope = newEntry; + newEntry->next = oldEntry; + } + return table; + } + return table; } TableNode* CreateEntry(SymbolTable* table, string typeOf, string id, Value value, int StringLength){ - if(table.entries == NULL){ - TableNode* newEntry = (TableNode*)malloc(sizeof(SymbolTable)); - newEntry.theType = typeOf; - newEntry.theName = id; - newEntry.StrLength = StringLength; + if(table->entries == NULL){ + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->StrLength = StringLength; + return newEntry; } else{ - + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->StrLength = StringLength; + TableNode* oldEntry = table->entries; + table->entries = newEntry; + newEntry->next = oldEntry; + return newEntry; + } + } - From f2a0fbd1bc8dc171034e99eb81ce053bddc21797 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 19:07:21 -0500 Subject: [PATCH 06/16] added a simple test of symbol table and entry creation --- symbol_table.c | 148 +++++++++++++++++++++++----------------- symbol_table.h | 34 +++++++++ test_comment_issues.tok | 0 3 files changed, 118 insertions(+), 64 deletions(-) delete mode 100644 test_comment_issues.tok diff --git a/symbol_table.c b/symbol_table.c index 70cb359..2c3f3a2 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -2,84 +2,104 @@ //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 "symbol_table.h" +//*#include "symbol_table.h" +/* +#include +#include +#include typedef struct ListOfTable{ - struct SymbolTable* table; - //struct ListOfTable* prev; - struct ListOfTable* next; + struct SymbolTable* table; + //struct ListOfTable* prev; + struct ListOfTable* next; }ListOfTable; typedef union Value{ - int* value_of_int; + int* value_of_int; void* value_of_pointer; bool* value_of_bool; char* value_of_char; }Value; - + typedef struct TableNode{ - string* theType; - string* theName; - Value* value; - struct TableNode* next; - //this next value is an int for string types to tell you how far to traverse a buffer for the string - int StrLength; -}TableNode; - + char* theType; + char* theName; + Value* value; + struct TableNode* next; + //this next value is an int for string types to tell you how far to traverse a buffer for the string + int StrLength; + }TableNode; + typedef struct SymbolTable{ - TableNode* entries; - struct SymbolTable* Parent_Scope; - struct ListOfTable* Children_Scope; - int Line_Number; + TableNode* entries; + struct SymbolTable* Parent_Scope; + struct ListOfTable* Children_Scope; + int Line_Number; int Column_Number; -}SymbolTable; + }SymbolTable; +*/ + SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ - SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); - table->Line_Number = Line; - table->Column_Number = Column; - table->Parent_Scope = ParentScope; - table->Children_Scope = NULL; - table->entries = NULL; - if(ParentScope->Children_scope == NULL){ - ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); - newEntry->next = NULL; - //newEntry->prev = NULL; - newEntry->table = table; - ParentScope->Children_scope = newEntry; - } else{ - ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); - //newEntry->prev = NULL; - newEntry->table= table; - ListOfTable* oldEntry = ParentScope->Children_scope; - ParentScope->Children_scope = newEntry; - newEntry->next = oldEntry; + SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); + table->Line_Number = Line; + table->Column_Number = Column; + table->Parent_Scope = ParentScope; + table->Children_Scope = NULL; + table->entries = NULL; + if(ParentScope != NULL){ + if(ParentScope->Children_Scope == NULL){ + ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); + newEntry->next = NULL; + //newEntry->prev = NULL; + newEntry->table = table; + ParentScope->Children_Scope = newEntry; + } else{ + ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); + //newEntry->prev = NULL; + newEntry->table= table; + ListOfTable* oldEntry = ParentScope->Children_Scope; + ParentScope->Children_Scope = newEntry; + newEntry->next = oldEntry; } - return table; - } - - return table; -} - -TableNode* CreateEntry(SymbolTable* table, string typeOf, string id, Value value, int StringLength){ - if(table->entries == NULL){ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->StrLength = StringLength; - return newEntry; - } else{ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->StrLength = StringLength; - TableNode* oldEntry = table->entries; - table->entries = newEntry; - newEntry->next = oldEntry; - return newEntry; - } - } - - + } + return table; + } + + +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ + if(table->entries == NULL){ + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->value = value; + newEntry->StrLength = StringLength; + return newEntry; + } else{ + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->value = value; + newEntry->StrLength = StringLength; + TableNode* oldEntry = table->entries; + table->entries = newEntry; + newEntry->next = oldEntry; + return newEntry; + } + } +//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"; + Value* ofX = (Value*)malloc(sizeof(Value)); + ofX->value_of_char = X; + SymbolTable* Second = CreateScope(NULL, 2,2); + printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number); + TableNode* First_Entry = CreateEntry(Second,String,X,ofX,-1); + printf("The value of the first entry is %s\n",First_Entry->value->value_of_char); + return 0; + } +*/ diff --git a/symbol_table.h b/symbol_table.h index e69de29..539f2e4 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -0,0 +1,34 @@ +#include +#include +#include + +typedef struct ListOfTable{ + struct SymbolTable* table; + //struct ListOfTable* prev; + struct ListOfTable* next; + +}ListOfTable; + +typedef union Value{ + int* value_of_int; + void* value_of_pointer; + bool* value_of_bool; + char* value_of_char; + }Value; + +typedef struct TableNode{ + char* theType; + char* theName; + Value* value; + struct TableNode* next; + //this next value is an int for string types to tell you how far to traverse a buffer for the string + int StrLength; +}TableNode; + +typedef struct SymbolTable{ + TableNode* entries; + struct SymbolTable* Parent_Scope; + struct ListOfTable* Children_Scope; + int Line_Number; + int Column_Number; +}SymbolTable; diff --git a/test_comment_issues.tok b/test_comment_issues.tok deleted file mode 100644 index e69de29..0000000 From bf65f47b50cbc25d772a83891c0b0b34a24ad8d4 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 19:20:34 -0500 Subject: [PATCH 07/16] uncommented one part of symbol_table --- symbol_table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/symbol_table.c b/symbol_table.c index 2c3f3a2..343cf66 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -2,7 +2,7 @@ //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 "symbol_table.h" +#include "symbol_table.h" /* #include #include From 0b10c50a7847e46ae593cc1b36c3b325d172c892 Mon Sep 17 00:00:00 2001 From: Annie Date: Fri, 21 Feb 2025 01:19:39 -0500 Subject: [PATCH 08/16] added tests to makefile --- Makefile | 4 ++++ tests/test_wrong_type.txt | 0 2 files changed, 4 insertions(+) create mode 100644 tests/test_wrong_type.txt diff --git a/Makefile b/Makefile index 7d58bb2..e912658 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,10 @@ test: ./$(EXE) -tok ./tests/test_simpleLiterals.alpha ./$(EXE) -tok ./tests/test_real_alpha_file.alpha ./$(EXE) -tok ./tests/test_real_alpha_2.alpha + ./$(EXE) -tok -st ./tests/test_real_alpha_2.alpha + ./$(EXE) -st -tok ./tests/test_operators.alpha + ./$(EXE) -st ./tests/test_keywords.alpha + clean: rm -f *.o rm -f lex.yy.c diff --git a/tests/test_wrong_type.txt b/tests/test_wrong_type.txt new file mode 100644 index 0000000..e69de29 From 4468dbd02f653c35d89dddc8dff747b6c1f4622e Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 21 Feb 2025 09:32:06 -0500 Subject: [PATCH 09/16] Fixed some functionality in symbol_table and added print_symbole_table t#29 --- print_symbol_table.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ symbol_table.c | 48 ++++++++++++++++------------------ 2 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 print_symbol_table.c diff --git a/print_symbol_table.c b/print_symbol_table.c new file mode 100644 index 0000000..91393e8 --- /dev/null +++ b/print_symbol_table.c @@ -0,0 +1,62 @@ +#include +#include "symbol_table.h" +#include "symbol_table.c" + +void print_symbol_table(SymbolTable *table, FILE *file_ptr){ + if(table->Parent_Scope == NULL){ + fprintf(file_ptr, "%-17s:%-8s:%-8s:%-22s:%-29s\n", "NAME", "SCOPE", "PARANT", "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){ + fprintf(file_ptr, "%-17s: %06d : %06d :%-22s:%-29s\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){ + SymbolTable * parant = CreateScope(NULL, 1,1); + // Value* v = calloc(1, sizeof(Value)); + CreateEntry(parant, &"primitive", &"Boolean", NULL, 0); + CreateEntry(parant, &"primitive", &"character", NULL, 0); + CreateEntry(parant, &"primitive", &"integer", NULL, 0); + CreateEntry(parant, &"1 -> character", &"string", NULL, 0); + CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0); + CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0); + CreateEntry(parant, &"int2int", &"square", NULL, 0); + CreateEntry(parant, &"string2int", &"entry", NULL, 0); + SymbolTable * child = CreateScope(parant, 14,14); + CreateEntry(child, &"integer", &"x", NULL, 0); + SymbolTable * second = CreateScope(parant, 14,14); + CreateEntry(second, &"string", &"arg", NULL, 0); + CreateEntry(second, &"integer", &"input", NULL, 0); + CreateEntry(second, &"integer", &"expected", NULL, 0); + CreateEntry(second, &"integer", &"actual", NULL, 0); + CreateEntry(second, &"$_undefined_type", &"result", NULL, 0); + SymbolTable * third = CreateScope(second, 33,44); + CreateEntry(third, &"BOO", &"arg", NULL, 0); + CreateEntry(third, &"YAZOO", &"input", NULL, 0); + print_symbol_table(parant, stderr); + return 0; +} +*/ diff --git a/symbol_table.c b/symbol_table.c index 2c3f3a2..0e4c3d0 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -30,7 +30,7 @@ typedef struct TableNode{ //this next value is an int for string types to tell you how far to traverse a buffer for the string int StrLength; }TableNode; - + typedef struct SymbolTable{ TableNode* entries; struct SymbolTable* Parent_Scope; @@ -40,7 +40,7 @@ typedef struct SymbolTable{ }SymbolTable; */ - + SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); table->Line_Number = Line; @@ -66,31 +66,27 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ } return table; } - - -TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ - if(table->entries == NULL){ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->value = value; - newEntry->StrLength = StringLength; - return newEntry; - } else{ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->value = value; - newEntry->StrLength = StringLength; - TableNode* oldEntry = table->entries; - table->entries = newEntry; - newEntry->next = oldEntry; - return newEntry; - } - } -//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation -/* + +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->value = value; + newEntry->StrLength = StringLength; + if(table->entries == NULL){ + table->entries = newEntry; + return newEntry; + } else{ + TableNode* oldEntry = table->entries; + table->entries = newEntry; + newEntry->next = oldEntry; + return newEntry; + } +} + +//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"; From dbcdafb1a0e6515e048baaffe652fa9a0dda4b5c Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 21 Feb 2025 10:09:26 -0500 Subject: [PATCH 10/16] fixed some of the spacing in the table t#29 --- print_symbol_table.c | 6 +++--- symbol_table.h | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/print_symbol_table.c b/print_symbol_table.c index 91393e8..0121538 100644 --- a/print_symbol_table.c +++ b/print_symbol_table.c @@ -4,7 +4,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){ if(table->Parent_Scope == NULL){ - fprintf(file_ptr, "%-17s:%-8s:%-8s:%-22s:%-29s\n", "NAME", "SCOPE", "PARANT", "TYPE", "Extra annotation"); + fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARANT", "TYPE", "Extra annotation"); } TableNode * entrie = table->entries; fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n"); @@ -18,7 +18,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){ } for(; entrie != NULL; entrie = entrie->next){ - fprintf(file_ptr, "%-17s: %06d : %06d :%-22s:%-29s\n", + fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName, current_scope, parant_scope, entrie->theType, "Extra annotation"); } @@ -34,6 +34,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){ } /* +*/ int main(void){ SymbolTable * parant = CreateScope(NULL, 1,1); // Value* v = calloc(1, sizeof(Value)); @@ -59,4 +60,3 @@ int main(void){ print_symbol_table(parant, stderr); return 0; } -*/ diff --git a/symbol_table.h b/symbol_table.h index 539f2e4..1e002d0 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -32,3 +32,7 @@ typedef struct SymbolTable{ int Line_Number; int Column_Number; }SymbolTable; + + +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength); +SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); From ad5da9685799ae3e80cdb36e3e68f5f1d34e80eb Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 21 Feb 2025 11:47:20 -0500 Subject: [PATCH 11/16] Made spelling errors t#29 --- print_symbol_table.c | 49 ++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/print_symbol_table.c b/print_symbol_table.c index 0121538..0ae90f6 100644 --- a/print_symbol_table.c +++ b/print_symbol_table.c @@ -1,10 +1,11 @@ #include #include "symbol_table.h" #include "symbol_table.c" +#include 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", "PARANT", "TYPE", "Extra annotation"); + fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARENT", "TYPE", "Extra annotation"); } TableNode * entrie = table->entries; fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n"); @@ -18,9 +19,15 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){ } for(; entrie != NULL; entrie = entrie->next){ - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", - entrie->theName, current_scope, parant_scope, - entrie->theType, "Extra annotation"); + 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; @@ -36,27 +43,39 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){ */ 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, &"primitive", &"Boolean", NULL, 0); - CreateEntry(parant, &"primitive", &"character", NULL, 0); - CreateEntry(parant, &"primitive", &"integer", NULL, 0); - CreateEntry(parant, &"1 -> character", &"string", NULL, 0); + CreateEntry(parant, prim, boole, NULL, 0); + CreateEntry(parant, prim, chare, NULL, 0); + CreateEntry(parant, prim, inte, NULL, 0); + CreateEntry(parant, &"1 -> character", str, NULL, 0); CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0); CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0); CreateEntry(parant, &"int2int", &"square", NULL, 0); CreateEntry(parant, &"string2int", &"entry", NULL, 0); SymbolTable * child = CreateScope(parant, 14,14); - CreateEntry(child, &"integer", &"x", NULL, 0); - SymbolTable * second = CreateScope(parant, 14,14); - CreateEntry(second, &"string", &"arg", NULL, 0); - CreateEntry(second, &"integer", &"input", NULL, 0); - CreateEntry(second, &"integer", &"expected", NULL, 0); - CreateEntry(second, &"integer", &"actual", NULL, 0); + CreateEntry(child, inte, &"x", NULL, 0); + SymbolTable * second = CreateScope(parant, 21,15); + CreateEntry(second, str, arg, NULL, 0); + CreateEntry(second, inte, &"input", NULL, 0); + CreateEntry(second, inte, &"expected", NULL, 0); + CreateEntry(second, inte, &"actual", NULL, 0); CreateEntry(second, &"$_undefined_type", &"result", NULL, 0); SymbolTable * third = CreateScope(second, 33,44); - CreateEntry(third, &"BOO", &"arg", NULL, 0); + CreateEntry(third, &"BOO", arg, NULL, 0); CreateEntry(third, &"YAZOO", &"input", NULL, 0); print_symbol_table(parant, stderr); + free(inte); + free(boole); + free(prim); + free(str); + free(chare); + free(arg); return 0; } From e0ab5540da8dd3f0b2cb0d15ee30581b82c47ec3 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Fri, 21 Feb 2025 12:52:24 -0500 Subject: [PATCH 12/16] edited symbol table node structure to remove strlength and value as they are not needed --- symbol_table.c | 54 ++++++------------------------------------------- symbol_table.h | 12 +---------- test | Bin 0 -> 16088 bytes 3 files changed, 7 insertions(+), 59 deletions(-) create mode 100755 test diff --git a/symbol_table.c b/symbol_table.c index 0e4c3d0..9cbdf90 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -1,45 +1,8 @@ //Defining a symbol table //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 "symbol_table.h" -/* -#include -#include -#include +//Tail of Linked List points to a List of child scopes -typedef struct ListOfTable{ - struct SymbolTable* table; - //struct ListOfTable* prev; - struct ListOfTable* next; - -}ListOfTable; - -typedef union Value{ - int* value_of_int; - void* value_of_pointer; - bool* value_of_bool; - char* value_of_char; - }Value; - -typedef struct TableNode{ - char* theType; - char* theName; - Value* value; - struct TableNode* next; - //this next value is an int for string types to tell you how far to traverse a buffer for the string - int StrLength; - }TableNode; - -typedef struct SymbolTable{ - TableNode* entries; - struct SymbolTable* Parent_Scope; - struct ListOfTable* Children_Scope; - int Line_Number; - int Column_Number; - }SymbolTable; - -*/ +#include "symbol_table.h" SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); @@ -68,12 +31,10 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ } -TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id){ TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); newEntry->theType = typeOf; newEntry->theName = id; - newEntry->value = value; - newEntry->StrLength = StringLength; if(table->entries == NULL){ table->entries = newEntry; return newEntry; @@ -86,16 +47,13 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, } //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"; - Value* ofX = (Value*)malloc(sizeof(Value)); - ofX->value_of_char = X; SymbolTable* Second = CreateScope(NULL, 2,2); printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number); - TableNode* First_Entry = CreateEntry(Second,String,X,ofX,-1); - printf("The value of the first entry is %s\n",First_Entry->value->value_of_char); + TableNode* First_Entry = CreateEntry(Second,String,X); + printf("The type of the first entry is %s\n",First_Entry->theType); return 0; } -*/ diff --git a/symbol_table.h b/symbol_table.h index 1e002d0..d510a3b 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -9,20 +9,10 @@ typedef struct ListOfTable{ }ListOfTable; -typedef union Value{ - int* value_of_int; - void* value_of_pointer; - bool* value_of_bool; - char* value_of_char; - }Value; - typedef struct TableNode{ char* theType; char* theName; - Value* value; struct TableNode* next; - //this next value is an int for string types to tell you how far to traverse a buffer for the string - int StrLength; }TableNode; typedef struct SymbolTable{ @@ -34,5 +24,5 @@ typedef struct SymbolTable{ }SymbolTable; -TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength); +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); diff --git a/test b/test new file mode 100755 index 0000000000000000000000000000000000000000..4941bf240c8f2f1ec1cf50ca112b79be646ae11c GIT binary patch literal 16088 zcmeHOZ)_Y#6(2iI>d+*RbH+whpg^sB8Z9&Wm?6-bGFe+m>sY=3lyD7HsX}iyVkJ}}BucpX%3%r!MX2I_5N;tU zm=DsU9EVWRm)o-ljAH4}GW`J=pa}KjD3tL=tybeZE9qg*%OF$15lgrU#!WCz=nS_f zRPsqVp=&$$BS@Q|qarkj8)RINwi}odN^MX%blyw6pZ2;L=ULi~3Q}oDsI+${a6GgI zc$H5X=7ZeceB;o~`4`IRRhv`EzHP0WQ;Eh@GE*3C9NxaIaa(INpN+QYmMJa^7tN_X z`+F7KOpuJzsA@+IFaAiklJ#W&O6T*xIrZB52e!YsvE_Js;pB&7Z~TmGC~m@#4Hfby zQ-pY`UyF-uOg_GdnvoGb=f0FCI$yaFitDQ=?}W^%BHsjAHG68Lnmxr|&HjOT?5Q6c zaJ{EEPXBP+8AxX0spS2xO2<>FY`=0Gj7z_h_u@IvNyn2JHIz$cya7dG8l7!w&)%-x zZB9$HCAzg--_oj_uAY5P!p*ryl6lX~_3Ud)WixJ1yf5X#>ydOe!(JU@TGf#9UxP7` zi^ic=-%~Q*3Tu*!Y1DS0LnM3p>yKiXYE>t9P@V&F{~ca#6esyU7rDjqx|q?6T=>a5 zjGX2G^)PGbuAZxtUK!GwYMne3SA#%Sk44opfh?a{ZhYKuE(3gc%4k z5N06EK$w9r17QaKr!w%@`n7+zk6oy<#}>Y_Stb7j%|7;r zneLvB=85JP>=Qeiv3@PB+5*eJ4MgkLJ*4A-{4PlB6AKR#`NXWZ3ZdUXb%egSG*!QD zlnhTZh5aUNzjXtVO;_yVjQz~TJM3p>7uYpt>~mMVm2hxAY8=#+rUvTQA#TA_|3-I` zS`}{VwU6!mB~|U>d2fk5w(|^_>BZ>xbOIk|7XBQ3&Aq@@;9tKFN^;Z-9FpB+7mrRB z3ojRY&z`d2ZTe4FN+rA4F=dZ+OpW!vTj$puMpQ|n0-mM{Zh2Eq)483;2FW+2Q!n1L_@VFvy`Ga&l~ zWq+XT{nPI>NXZgO3%|VJPcL1DRv5aPdD(|}6Z5jaa1HaaACcZ~sEGc}E2S*)}h;zv6FV(|1u7l#qz0ud<)Cv{s5~>7d~s$ z8Wo8dUdE4hS=Xo=l+1UdRq6p$s9m|=ME+H6zpA{SU*wOYQZ4@Z`t>92XQleydhswv ztND3W+ux{y04VJr6m>AGCiNa;wsX<}O0Mp^AKQE#(>C0)hKN9qP|{&Cx9Oy2tyHju$_o zAjo@#fq`hhn%fuecxk7fcE;ynnaDavQrW(E%1LF;DQAnpp0P%4SEWHZT{7AYln?iWlmXEj`r}N;heH~86{&olZ z^4ss)ziVGt8^kys;dI!nwc8IWXV2aPyLat%4s>=N>gaKLcJ1EV0Yvb)Kuk^tT;)VS zTh5JpZU_BrsT?;E_u@+Ti;o+W-OwQeNU@4yUZ*i#tK3rWOGN7`+@k#J`g_m0$+}9MldZpe z2n?-da69WV-+3-pm)B>)Z$rh851#9y-^2Yyo{J)*?@K>kzORK^V#qXz|1{)pVJ#%_ z%X(3$v@d5)Wc?*>!6gt6%6eHzM@UpH;LPkpr!OQwosH{g}qDwIUpM#Xv zdV-hroltqaO8jCc^g-Zh?I^sgAB8rVW_6DJ@v~T+K!tKDcv;U1onS+0KiL0oF@87O z$@;iVeR#@Q5dVG1C@%3|{yyQ3%XCqb-9_x1Nd$>5GwZb z;l~5`!we8=36QDy6Z(??ev}nLFUr6K@Zy&C7W(@y>mH%~iO z3ta@TAFpC;+GMD-BMt;7JpIQ>GHGAdg%QRF?Wrw)Sn x*x?f#*axAcrL4pw&tH1ali2 Date: Fri, 21 Feb 2025 14:30:14 -0500 Subject: [PATCH 13/16] it looks like this should work. t#29 --- Makefile | 20 ++++++++++------- grammar.y | 46 +++++++++++++++++++++++++++++++++++++++ print_symbol_table.c | 51 +++++++++++++++++++++++++++++--------------- runner.c | 17 ++++++++++++++- runner.h | 4 ++++ symbol_table.c | 29 +++++++++++++++++++------ symbol_table.h | 3 ++- 7 files changed, 136 insertions(+), 34 deletions(-) create mode 100644 grammar.y diff --git a/Makefile b/Makefile index 8164ae1..e912658 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,20 @@ CC := gcc FLEX := flex LEX := lexicalStructure.lex -EXE := runner +EXE := alpha CFLAGS := -std=c99 -Wall CPPFLAGS := -runner: flex.o runner.o - $(CC) -o runner runner.o flex.o +build: lex.yy.c runner.o runner + +lex.yy.c: lexicalStructure.lex + $(FLEX) -o lex.yy.c $(LEX) runner.o: runner.c runner.h flex.h $(CC) $(CFLAGS) -o runner.o -c runner.c -flex.o: lex.yy.c typedefs.h - $(CC) $(CFLAGS) -o flex.o -c lex.yy.c - -lex.yy.c: lexicalStructure.lex - $(FLEX) -o lex.yy.c $(LEX) +runner: lex.yy.c runner.o + $(CC) -o $(EXE) runner.o lex.yy.c debug: CFLAGS += -DDEBUG=1 debug: clean runner @@ -39,9 +38,14 @@ test: ./$(EXE) -tok ./tests/test_simpleLiterals.alpha ./$(EXE) -tok ./tests/test_real_alpha_file.alpha ./$(EXE) -tok ./tests/test_real_alpha_2.alpha + ./$(EXE) -tok -st ./tests/test_real_alpha_2.alpha + ./$(EXE) -st -tok ./tests/test_operators.alpha + ./$(EXE) -st ./tests/test_keywords.alpha + clean: rm -f *.o rm -f lex.yy.c rm -f $(EXE) rm -f flex.h rm -f *.tok + rm -f *.st diff --git a/grammar.y b/grammar.y new file mode 100644 index 0000000..19c6ea2 --- /dev/null +++ b/grammar.y @@ -0,0 +1,46 @@ +%token ID 101 +%token T_INTEGER 201 +%token T_ADDRESS 202 +%token T_BOOLEAN 203 +%token T_CHARACTER 204 +%token T_STRING 205 +%token C_INTEGER 301 +%token C_NULL 302 +%token C_CHARACTER 303 +%token C_STRING 304 +%token C_TRUE 305 +%token C_FALSE 306 +%token WHILE 401 +%token IF 402 +%token THEN 403 +%token ELSE 404 +%token TYPE 405 +%token FUNCTION 406 +%token RETURN 407 +%token EXTERNAL 408 +%token AS 409 +%token L_PAREN 501 +%token R_PAREN 502 +%token L_BRACKET 503 +%token R_BRACKET 504 +%token L_BRACE 505 +%token R_BRACE 506 +%token SEMI_COLON 507 +%token COLON 508 +%token COMMA 509 +%token ARROW 510 +%token ADD 601 +%token SUB_OR_NEG 602 +%token MUL 603 +%token DIV 604 +%token REM 605 +%token LESS_THAN 606 +%token EQUAL_TO 607 +%token ASSIGN 608 +%token NOT 609 +%token AND 610 +%token OR 611 +%token DOT 612 +%token RESERVE 613 +%token RELEASE 614 +%token COMMENT 700 diff --git a/print_symbol_table.c b/print_symbol_table.c index 0ae90f6..c5e4208 100644 --- a/print_symbol_table.c +++ b/print_symbol_table.c @@ -41,7 +41,6 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){ } /* -*/ int main(void){ char *prim = strdup("primitive"); char *inte = strdup("integer"); @@ -51,25 +50,41 @@ int main(void){ char *str = strdup("string"); char *arg = strdup("arg"); // Value* v = calloc(1, sizeof(Value)); - CreateEntry(parant, prim, boole, NULL, 0); - CreateEntry(parant, prim, chare, NULL, 0); - CreateEntry(parant, prim, inte, NULL, 0); - CreateEntry(parant, &"1 -> character", str, NULL, 0); - CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0); - CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0); - CreateEntry(parant, &"int2int", &"square", NULL, 0); - CreateEntry(parant, &"string2int", &"entry", NULL, 0); + 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", NULL, 0); + CreateEntry(child, inte, &"x"); SymbolTable * second = CreateScope(parant, 21,15); - CreateEntry(second, str, arg, NULL, 0); - CreateEntry(second, inte, &"input", NULL, 0); - CreateEntry(second, inte, &"expected", NULL, 0); - CreateEntry(second, inte, &"actual", NULL, 0); - CreateEntry(second, &"$_undefined_type", &"result", NULL, 0); + 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, NULL, 0); - CreateEntry(third, &"YAZOO", &"input", NULL, 0); + 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); @@ -79,3 +94,5 @@ int main(void){ free(arg); return 0; } + +*/ diff --git a/runner.c b/runner.c index 9a8e41c..a0223c8 100644 --- a/runner.c +++ b/runner.c @@ -1,6 +1,7 @@ +#include "symbol_table.h" +#include "symbol_table.c" #include "runner.h" - int main(int argc, char *argv[]) { char *check_input; int token; @@ -84,3 +85,17 @@ int is_alpha_file(char *file, int file_len) { return 0; //is alpha file } +void enter_scope(int line, int column){ + curr = CreateScope(curr, line, column); +} +void exit_scope() { + if(curr->Parent_Scope == NULL){ + printf("Can't close top"); + return; + } + curr = curr->Parent_Scope; +} + + + + diff --git a/runner.h b/runner.h index b7c7369..968eada 100644 --- a/runner.h +++ b/runner.h @@ -20,7 +20,11 @@ extern int line_number, column_number; extern char *yytext; extern FILE *yyin; int arg; +SymbolTable * top; +SymbolTable * curr; int main(int argc, char* argv[]); char *is_tok(int argc, char* argv[]); int is_alpha_file(char *file, int file_len); +void enter_scope(int, int); +void exit_scope(void); diff --git a/symbol_table.c b/symbol_table.c index 0e4c3d0..3695a32 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -68,12 +68,10 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ } -TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id){ TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); newEntry->theType = typeOf; newEntry->theName = id; - newEntry->value = value; - newEntry->StrLength = StringLength; if(table->entries == NULL){ table->entries = newEntry; return newEntry; @@ -84,18 +82,35 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, 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); +} //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"; - Value* ofX = (Value*)malloc(sizeof(Value)); - ofX->value_of_char = X; SymbolTable* Second = CreateScope(NULL, 2,2); printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number); - TableNode* First_Entry = CreateEntry(Second,String,X,ofX,-1); - printf("The value of the first entry is %s\n",First_Entry->value->value_of_char); + TableNode* First_Entry = CreateEntry(Second,String,X); + printf("The value of the first entry is %s\n",First_Entry->theType); return 0; } */ diff --git a/symbol_table.h b/symbol_table.h index 1e002d0..11f828f 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -1,6 +1,7 @@ #include #include #include +#include typedef struct ListOfTable{ struct SymbolTable* table; @@ -34,5 +35,5 @@ typedef struct SymbolTable{ }SymbolTable; -TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength); +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); From eb3452fec9afd4a71c97adfd83ecc34903a7ed7a Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Fri, 21 Feb 2025 15:25:21 -0500 Subject: [PATCH 14/16] added code to print out positions of incorrect tokens --- lexicalStructure.lex | 4 +++- runner.c | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lexicalStructure.lex b/lexicalStructure.lex index 7857730..64b2863 100644 --- a/lexicalStructure.lex +++ b/lexicalStructure.lex @@ -81,6 +81,8 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\] {ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {return ID;}} \n {line_number++; column_number = 1;} -. {column_number++;} +\t {column_number++;} +" " {column_number++;} +. {column_number++; return 1999;} %% diff --git a/runner.c b/runner.c index 85e8214..a87df49 100644 --- a/runner.c +++ b/runner.c @@ -79,6 +79,9 @@ int run(FILE *alpha) { } continue; } + if(token == 1999){ + printf("On line number %d and column number %d we have an invalid character:%s\n",line_number,column_number,yytext); + } column_number += yyleng; } From e86edb324b6ead099ee1b7074ee2665ce6e8e480 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 21 Feb 2025 16:26:37 -0500 Subject: [PATCH 15/16] Fix the conflicts t#29 --- runner.c | 6 +++--- runner.h | 4 ++-- symbol_table.c | 2 ++ test | Bin 16088 -> 0 bytes 4 files changed, 7 insertions(+), 5 deletions(-) delete mode 100755 test diff --git a/runner.c b/runner.c index dd096aa..d183b52 100644 --- a/runner.c +++ b/runner.c @@ -1,4 +1,4 @@ -#include "symbol_table.h" +//#include "symbol_table.h" #include "symbol_table.c" #include "runner.h" @@ -94,7 +94,7 @@ int run(FILE *alpha) { if (yyin != NULL) { fclose(yyin); } - + if (tok_flag != NULL) { fclose(tok_flag); } @@ -139,7 +139,7 @@ int new_file(char *arg, char *alpha) { strcat(file_name, "."); strcat(file_name, arg + 1); - + if (strcmp(arg, "-tok") == 0) { tok_flag = fopen(file_name, "w"); } else if (strcmp(arg, "-st") == 0) { diff --git a/runner.h b/runner.h index e7a8194..73c94f2 100644 --- a/runner.h +++ b/runner.h @@ -20,9 +20,9 @@ int arg; SymbolTable * top; SymbolTable * curr; -int main(int argc, char* argv[]); +// int main(int argc, char* argv[]); char *is_tok(int argc, char* argv[]); -int is_alpha_file(char *file, int file_len); +// int is_alpha_file(char *file, int file_len); void enter_scope(int, int); void exit_scope(void); diff --git a/symbol_table.c b/symbol_table.c index e5d1366..a8592eb 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -69,6 +69,7 @@ TableNode * look_up(SymbolTable * table, char * x){ //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"; @@ -79,3 +80,4 @@ int main(){ printf("The type of the first entry is %s\n",First_Entry->theType); return 0; } + */ diff --git a/test b/test deleted file mode 100755 index 4941bf240c8f2f1ec1cf50ca112b79be646ae11c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16088 zcmeHOZ)_Y#6(2iI>d+*RbH+whpg^sB8Z9&Wm?6-bGFe+m>sY=3lyD7HsX}iyVkJ}}BucpX%3%r!MX2I_5N;tU zm=DsU9EVWRm)o-ljAH4}GW`J=pa}KjD3tL=tybeZE9qg*%OF$15lgrU#!WCz=nS_f zRPsqVp=&$$BS@Q|qarkj8)RINwi}odN^MX%blyw6pZ2;L=ULi~3Q}oDsI+${a6GgI zc$H5X=7ZeceB;o~`4`IRRhv`EzHP0WQ;Eh@GE*3C9NxaIaa(INpN+QYmMJa^7tN_X z`+F7KOpuJzsA@+IFaAiklJ#W&O6T*xIrZB52e!YsvE_Js;pB&7Z~TmGC~m@#4Hfby zQ-pY`UyF-uOg_GdnvoGb=f0FCI$yaFitDQ=?}W^%BHsjAHG68Lnmxr|&HjOT?5Q6c zaJ{EEPXBP+8AxX0spS2xO2<>FY`=0Gj7z_h_u@IvNyn2JHIz$cya7dG8l7!w&)%-x zZB9$HCAzg--_oj_uAY5P!p*ryl6lX~_3Ud)WixJ1yf5X#>ydOe!(JU@TGf#9UxP7` zi^ic=-%~Q*3Tu*!Y1DS0LnM3p>yKiXYE>t9P@V&F{~ca#6esyU7rDjqx|q?6T=>a5 zjGX2G^)PGbuAZxtUK!GwYMne3SA#%Sk44opfh?a{ZhYKuE(3gc%4k z5N06EK$w9r17QaKr!w%@`n7+zk6oy<#}>Y_Stb7j%|7;r zneLvB=85JP>=Qeiv3@PB+5*eJ4MgkLJ*4A-{4PlB6AKR#`NXWZ3ZdUXb%egSG*!QD zlnhTZh5aUNzjXtVO;_yVjQz~TJM3p>7uYpt>~mMVm2hxAY8=#+rUvTQA#TA_|3-I` zS`}{VwU6!mB~|U>d2fk5w(|^_>BZ>xbOIk|7XBQ3&Aq@@;9tKFN^;Z-9FpB+7mrRB z3ojRY&z`d2ZTe4FN+rA4F=dZ+OpW!vTj$puMpQ|n0-mM{Zh2Eq)483;2FW+2Q!n1L_@VFvy`Ga&l~ zWq+XT{nPI>NXZgO3%|VJPcL1DRv5aPdD(|}6Z5jaa1HaaACcZ~sEGc}E2S*)}h;zv6FV(|1u7l#qz0ud<)Cv{s5~>7d~s$ z8Wo8dUdE4hS=Xo=l+1UdRq6p$s9m|=ME+H6zpA{SU*wOYQZ4@Z`t>92XQleydhswv ztND3W+ux{y04VJr6m>AGCiNa;wsX<}O0Mp^AKQE#(>C0)hKN9qP|{&Cx9Oy2tyHju$_o zAjo@#fq`hhn%fuecxk7fcE;ynnaDavQrW(E%1LF;DQAnpp0P%4SEWHZT{7AYln?iWlmXEj`r}N;heH~86{&olZ z^4ss)ziVGt8^kys;dI!nwc8IWXV2aPyLat%4s>=N>gaKLcJ1EV0Yvb)Kuk^tT;)VS zTh5JpZU_BrsT?;E_u@+Ti;o+W-OwQeNU@4yUZ*i#tK3rWOGN7`+@k#J`g_m0$+}9MldZpe z2n?-da69WV-+3-pm)B>)Z$rh851#9y-^2Yyo{J)*?@K>kzORK^V#qXz|1{)pVJ#%_ z%X(3$v@d5)Wc?*>!6gt6%6eHzM@UpH;LPkpr!OQwosH{g}qDwIUpM#Xv zdV-hroltqaO8jCc^g-Zh?I^sgAB8rVW_6DJ@v~T+K!tKDcv;U1onS+0KiL0oF@87O z$@;iVeR#@Q5dVG1C@%3|{yyQ3%XCqb-9_x1Nd$>5GwZb z;l~5`!we8=36QDy6Z(??ev}nLFUr6K@Zy&C7W(@y>mH%~iO z3ta@TAFpC;+GMD-BMt;7JpIQ>GHGAdg%QRF?Wrw)Sn x*x?f#*axAcrL4pw&tH1ali2 Date: Fri, 21 Feb 2025 16:28:53 -0500 Subject: [PATCH 16/16] switched -st to call print_symbol_table --- runner.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runner.c b/runner.c index 85e8214..4560da4 100644 --- a/runner.c +++ b/runner.c @@ -84,7 +84,7 @@ int run(FILE *alpha) { if (st_flag != NULL) { //output symbol table, file pointer is - fprintf(st_flag, "just checking that this works \n"); + print_symbol_table(curr,st_flag); } if (yyin != NULL) {