From 7a52aa51bf025aad7d9ecc8d555c8d151ad52936 Mon Sep 17 00:00:00 2001 From: Annie Date: Tue, 18 Feb 2025 14:22:28 -0500 Subject: [PATCH 1/7] added -help functionality --- runner.c | 15 ++++++++++++++- runner.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/runner.c b/runner.c index 9a8e41c..c927d71 100644 --- a/runner.c +++ b/runner.c @@ -6,10 +6,15 @@ int main(int argc, char *argv[]) { int token; FILE *output; - if (argc == 1 || argc > 3 || (argc == 2 && is_alpha_file(argv[1], strlen(argv[1])) != 0)) { + 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 } else if (argc == 2) { + if (is_help(argv[1])) { + return 0; + } else if (is_alpha_file(argv[1], strlen(argv[1])) != 0) { + return -1; + } arg = NO_ARG; //no argument but valid input yyin = fopen(argv[1], "r"); } else { @@ -48,6 +53,14 @@ int main(int argc, char *argv[]) { 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"); + return true; + } + return false; +} + char *is_tok(int argc, char *argv[]) { if (argc == 3 && strcmp("-tok", argv[1]) == 0) { char *input_prog = argv[2]; diff --git a/runner.h b/runner.h index b7c7369..c96c3df 100644 --- a/runner.h +++ b/runner.h @@ -15,6 +15,7 @@ #include #include "flex.h" #include "typedefs.h" +#include extern int line_number, column_number; extern char *yytext; @@ -24,3 +25,4 @@ int arg; int main(int argc, char* argv[]); char *is_tok(int argc, char* argv[]); int is_alpha_file(char *file, int file_len); +bool is_help(char * input); From 4e22726d5be6849ec2281d63a4792b6060cf7528 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Tue, 18 Feb 2025 16:01:30 -0500 Subject: [PATCH 2/7] added .gitignore t#09 --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47f37f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.tok +*.o +alpha +flex.h From c2adcdf8bb121929957f9454636865f6847c284a Mon Sep 17 00:00:00 2001 From: Scarlett Date: Tue, 18 Feb 2025 16:03:52 -0500 Subject: [PATCH 3/7] Makefile updated t#09 --- Makefile | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 8164ae1..90bdaca 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 From 0aea539095ee52a937e12a70b3e02bf986361c96 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 20 Feb 2025 16:07:44 -0500 Subject: [PATCH 4/7] 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 5/7] 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 bf65f47b50cbc25d772a83891c0b0b34a24ad8d4 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Thu, 20 Feb 2025 19:20:34 -0500 Subject: [PATCH 6/7] 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 7/7] 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