diff --git a/Makefile b/Makefile index ff000b2..81376c3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC := gcc FLEX := flex LEX := lexicalStructure.lex -EXE := lexicalStructure +EXE := runner CFLAGS := -std=c99 -Wall CPPFLAGS := @@ -16,7 +16,10 @@ flex.o: lex.yy.c typedefs.h lex.yy.c: lexicalStructure.lex $(FLEX) -o lex.yy.c $(LEX) - + +debug: CFLAGS += -DDEBUG=1 +debug: clean runner + test: ./$(EXE) ./tests/test_comments.alpha ./$(EXE) ./tests/test_generalTokenTest.alpha @@ -25,9 +28,16 @@ test: ./$(EXE) ./tests/test_otherpunc.alpha ./$(EXE) ./tests/test_simpleIntTest.alpha ./$(EXE) ./tests/test_simpleLiterals.alpha - + ./$(EXE) -tok ./tests/test_comments.alpha + ./$(EXE) -tok ./tests/test_generalTokenTest.alpha + ./$(EXE) -tok ./tests/test_keywords.alpha + ./$(EXE) -tok ./tests/test_operators.alpha + ./$(EXE) -tok ./tests/test_otherpunc.alpha + ./$(EXE) -tok ./tests/test_simpleIntTest.alpha + ./$(EXE) -tok ./tests/test_simpleLiterals.alpha clean: rm -f *.o rm -f lex.yy.c rm -f $(EXE) rm -f flex.h + rm -f *.tok diff --git a/Makefile.save b/Makefile.save new file mode 100644 index 0000000..668af54 --- /dev/null +++ b/Makefile.save @@ -0,0 +1,43 @@ +CC := gcc +FLEX := flex +LEX := lexicalStructure.lex +EXE := runner +CFLAGS := -std=c99 -Wall +CPPFLAGS := + +runner: flex.o runner.o + $(CC) -o runner runner.o flex.o + +debug: CFLAGS += -DDEBUG=true +debug: clean runner + +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 + ./$(EXE) -tok ./tests/test_comments.alpha + ./$(EXE) -tok ./tests/test_generalTokenTest.alpha + ./$(EXE) -tok ./tests/test_keywords.alpha + ./$(EXE) -tok ./tests/test_operators.alpha + ./$(EXE) -tok ./tests/test_otherpunc.alpha + ./$(EXE) -tok ./tests/test_simpleIntTest.alpha + ./$(EXE) -tok ./tests/test_simpleLiterals.alpha +clean: + rm -f *.o + rm -f lex.yy.c + rm -f $(EXE) + rm -f flex.h + rm -f *.tok diff --git a/lexicalStructure.lex b/lexicalStructure.lex index 411d0bd..0763c99 100644 --- a/lexicalStructure.lex +++ b/lexicalStructure.lex @@ -8,13 +8,15 @@ #include #include "typedefs.h" int line_number = 1, column_number = 1; - bool DEBUG = true; + #ifndef DEBUG + #define DEBUG 0 + #endif %} COM ([^*]|\*+[^)*])* ID [A-Za-z_][0-9A-Za-z_]* DIGIT [0-9] -CHAR \\n|\\t|\\'|[^'\n\t\\] +CHAR \\n|\\t|\\'|[^'\n\t\\]|\\\\ /* char can be a newline, tab, an escaped quote, or anything but a single quote, an actual line break, an actual tab, or a backslash by itself (to prevent confusion from escaped quote */ SCHAR \\n|\\t|\\\"|[^\"\n\\] /* similar to above, a string Char (SCHAR) is the same as a CHAR except we cannot have double quotes instead of single quotes. Double quotes need to be escaped in Flex unlike single quotes based on documentation */ diff --git a/runner.c b/runner.c index 2f1045a..30f7551 100644 --- a/runner.c +++ b/runner.c @@ -26,8 +26,25 @@ int main(int argc, char *argv[]) { if (arg == TOK_ARG) { fprintf(output, "%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 (yyin != NULL) { + fclose(yyin); + } + if (output != NULL && arg == TOK_ARG) { + fclose(output); + } return 0; } @@ -40,21 +57,20 @@ char *is_tok(int argc, char *argv[]) { 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; - } + 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 + 1, sizeof(char)); + + // Copy filename and add .tok extension + strncpy(FILE_tok, basename, basename_len - ALPHA_OFFSET); + strcat(FILE_tok, ".tok"); - 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 return FILE_tok; } return CHECK_OTHER; diff --git a/runner.h b/runner.h index 07ec277..b7c7369 100644 --- a/runner.h +++ b/runner.h @@ -14,6 +14,7 @@ #include #include #include "flex.h" +#include "typedefs.h" extern int line_number, column_number; extern char *yytext; @@ -22,4 +23,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); \ No newline at end of file +int is_alpha_file(char *file, int file_len); diff --git a/tests/test_simpleIntTest.alpha b/tests/test_simpleIntTest.alpha index 507ec22..127f0a6 100644 --- a/tests/test_simpleIntTest.alpha +++ b/tests/test_simpleIntTest.alpha @@ -1,4 +1,5 @@ 45 123 8392 - +40 40 + 200 50 21783 diff --git a/tests/test_simpleLiterals.alpha b/tests/test_simpleLiterals.alpha index fd54099..4c5b0b2 100644 --- a/tests/test_simpleLiterals.alpha +++ b/tests/test_simpleLiterals.alpha @@ -4,7 +4,7 @@ 12893 "this is not a string (*one valid token before this*) (* spacey comment here over multiple lines -will it work? *) +will it work? *) false " ''' '\'