From e91eb1815b0462ea7a27b4de9c95c85f0353565a Mon Sep 17 00:00:00 2001 From: Annie Date: Wed, 12 Feb 2025 23:47:12 -0500 Subject: [PATCH 1/2] Added option to generate header file for yy.lex.c, fixed errors in runner.c, and updated Makefile --- Makefile | 25 ++++++++++++++++---- lexicalStructure.lex | 4 +++- runner.c | 55 ++++++++++++++++++++++++++++++-------------- runner.h | 24 ++++++++++++++----- 4 files changed, 80 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 3a7cffc..906c76c 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,32 @@ CC := gcc FLEX := flex LEX := lexicalStructure.lex EXE := lexicalStructure +CFLAGS := -std=c99 -Wall +CPPFLAGS := -lexicalStructure: - $(FLEX) $(LEX) - $(CC) lex.yy.c -o $(EXE) +runner: flex.o runner.o + $(CC) -o runner runner.o flex.o -test_operators: +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) + +test: + ./$(EXE) ./tests/test_comments.alpha + ./$(EXE) ./tests/test_generalTokenTest.alpha + ./$(EXE) ./tests/test_keywords.alpha ./$(EXE) ./tests/test_operators.alpha + ./$(EXE) ./tests/test_otherpunc.alpha + ./$(EXE) ./tests/test_simpleIntTest.alpha + ./$(EXE) ./tests/test_simpleLiterals.alpha clean: rm -f *.o rm -f lex.yy.c rm -f $(EXE) + rm -f flex.h diff --git a/lexicalStructure.lex b/lexicalStructure.lex index 14b8616..332b9ac 100644 --- a/lexicalStructure.lex +++ b/lexicalStructure.lex @@ -3,6 +3,7 @@ /* definitions */ %option noyywrap +%option header-file="flex.h" %{ #include "typedefs.h" int line_number = 1, column_number = 1; @@ -69,4 +70,5 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\] \n {line_number++; column_number = 1;} . {column_number++;} -%% \ No newline at end of file +%% + diff --git a/runner.c b/runner.c index 3d467b8..8e9474c 100644 --- a/runner.c +++ b/runner.c @@ -1,38 +1,59 @@ #include "runner.h" - int main(int argc, char *argv[]) { char *check_input; int token; - //check_input can be compared to INVALID_ARG and DIFF_ARG to determine if -tok and holds the generated file name if it is - check_input = is_tok(argc, argv); - FILE * output = fopen(check_input, "w"); + FILE *output; - if (check_input == INVALID_ARG) { - return -1; + if (argc == 1 || argc > 3 || (argc == 2 && is_alpha_file(argv[1], strlen(argv[1])) != 0)) { + 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) { + 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) + } + 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 (check_input != DIFF_ARG) { + token = yylex(); + fprintf(stderr, "made it here"); + while (token != 0) { + if (arg == TOK_ARG) { fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext); - } + } + token = yylex(); + fprintf(stderr, "made it past yylex"); } + fprintf(stderr, "returned 0\n"); return 0; } char *is_tok(int argc, char *argv[]) { - if (argc == 3 && strcmp("-tok", argv[1])) { + if (argc == 3 && strcmp("-tok", argv[1]) == 0) { char *input_prog = argv[2]; - int file_len = strlen(input); + int file_len = strlen(input_prog); //check that input program is a .alpha file - if (strcmp(".alpha", input_prog[file_len - ALPHA_OFFSET]) != 0) { + if (is_alpha_file(input_prog, file_len) != 0) { return INVALID_ARG; } - char *FILE_tok[file_len - ALPHA_OFFSET + TOK_LEN]; - strncpy(input, FILE_tok, file_len - ALPHA_OFFSET); //copy name of prog before .alpha - strcpy(".tok", FILE_tok[file_len - ALPHA_OFFSET]); //add .tok to end of file name + char* FILE_tok = calloc(sizeof(char), file_len - ALPHA_OFFSET + TOK_LEN); + strncpy(FILE_tok, input_prog, file_len - ALPHA_OFFSET); //copy name of prog before .alpha + strcpy(FILE_tok + sizeof(char) * (file_len - ALPHA_OFFSET), ".tok"); //add .tok to end of file name return FILE_tok; } - return DIFF_ARG; + return CHECK_OTHER; } + +int is_alpha_file(char *file, int file_len) { + if (strcmp(".alpha", file + 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 080d5c1..be45c9a 100644 --- a/runner.h +++ b/runner.h @@ -1,13 +1,25 @@ -#define ALPHA_OFFSET 5 +#define ALPHA_OFFSET 6 #define TOK_LEN 3 + +//returns for is_tok #define INVALID_ARG "invalid" -#define DIFF_ARG "diff" +#define CHECK_OTHER "diff" + +//argument type in main +#define NO_ARG 0 +#define DIFF_ARG 1 +#define TOK_ARG 2 + #include -#include -#include "lex.yy.c" +#include +#include +#include "flex.h" extern int line_number, column_number; extern char *yytext; +extern FILE *yyin; +int arg; -int main(int argc, char* argv); -char *is_tok(int argc, char* argv);#define ALPHA_OFFSET 5 +int main(int argc, char* argv[]); +char *is_tok(int argc, char* argv[]); +int is_alpha_file(char *file, int file_len); From 228f66a385c578ff206a5cb59db51864c5dfa569 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 13 Feb 2025 01:44:19 -0500 Subject: [PATCH 2/2] Resolved issue of including path to .alpha file and tested with multiple directories --- runner.c | 20 ++++++++++++++------ tests/testPath/testPath.alpha | 23 +++++++++++++++++++++++ tests/test_operators.alpha~ | 17 +++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/testPath/testPath.alpha create mode 100644 tests/test_operators.alpha~ diff --git a/runner.c b/runner.c index 8e9474c..d2e0890 100644 --- a/runner.c +++ b/runner.c @@ -21,17 +21,12 @@ int main(int argc, char *argv[]) { arg = TOK_ARG; //it is a -tok arg with a valid alpha file at argv[2] yyin = fopen(argv[2], "r"); } - token = yylex(); - fprintf(stderr, "made it here"); - while (token != 0) { + while (0 != (token = yylex())) { if (arg == TOK_ARG) { fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext); } - token = yylex(); - fprintf(stderr, "made it past yylex"); } - fprintf(stderr, "returned 0\n"); return 0; } @@ -43,6 +38,19 @@ char *is_tok(int argc, char *argv[]) { if (is_alpha_file(input_prog, file_len) != 0) { return INVALID_ARG; } + + int ignore_path = 0; + int count_since_slash = 0; + for (int i = 0; i < file_len; i++) { + count_since_slash++; + if (input_prog[i] == '/') { + ignore_path += count_since_slash; + count_since_slash = 0; + } + } + + file_len -= ignore_path; + input_prog += ignore_path; char* FILE_tok = calloc(sizeof(char), file_len - ALPHA_OFFSET + TOK_LEN); strncpy(FILE_tok, input_prog, file_len - ALPHA_OFFSET); //copy name of prog before .alpha strcpy(FILE_tok + sizeof(char) * (file_len - ALPHA_OFFSET), ".tok"); //add .tok to end of file name diff --git a/tests/testPath/testPath.alpha b/tests/testPath/testPath.alpha new file mode 100644 index 0000000..fa713c4 --- /dev/null +++ b/tests/testPath/testPath.alpha @@ -0,0 +1,23 @@ +This is a test +9combined 7okens +12345 +893247892 +combined'DueToUnknownChar _validtoken __validtoken1 _valid_token2 validToken3_ +true false +null while !wrong if when +else type function +return external as +string _NOte_that_was_not_reserved +([)]{}:;,->"\ ++-*/% +<= +:= +"This is not a valid +String" +"This is a valid String" +!| +.. +(* this is a comment *) +(*Not a comment +$^& +> \ No newline at end of file diff --git a/tests/test_operators.alpha~ b/tests/test_operators.alpha~ new file mode 100644 index 0000000..84d3edd --- /dev/null +++ b/tests/test_operators.alpha~ @@ -0,0 +1,17 @@ ++ +- +* +/ +\ +% +< +> += +:= +=: +: += +! +& +| +.