Merge pull request #12 from UB-CSE443/Sprint1-TokenLocationLogic-NoTask

Sprint1 token location logic no task
This commit is contained in:
Moroseui
2025-02-13 20:28:29 -05:00
committed by GitHub
7 changed files with 94 additions and 21 deletions

View File

@ -1,7 +1,7 @@
CC := gcc CC := gcc
FLEX := flex FLEX := flex
LEX := lexicalStructure.lex LEX := lexicalStructure.lex
EXE := lexicalStructure EXE := runner
CFLAGS := -std=c99 -Wall CFLAGS := -std=c99 -Wall
CPPFLAGS := CPPFLAGS :=
@ -16,7 +16,10 @@ flex.o: lex.yy.c typedefs.h
lex.yy.c: lexicalStructure.lex lex.yy.c: lexicalStructure.lex
$(FLEX) -o lex.yy.c $(LEX) $(FLEX) -o lex.yy.c $(LEX)
debug: CFLAGS += -DDEBUG=1
debug: clean runner
test: test:
./$(EXE) ./tests/test_comments.alpha ./$(EXE) ./tests/test_comments.alpha
./$(EXE) ./tests/test_generalTokenTest.alpha ./$(EXE) ./tests/test_generalTokenTest.alpha
@ -25,9 +28,16 @@ test:
./$(EXE) ./tests/test_otherpunc.alpha ./$(EXE) ./tests/test_otherpunc.alpha
./$(EXE) ./tests/test_simpleIntTest.alpha ./$(EXE) ./tests/test_simpleIntTest.alpha
./$(EXE) ./tests/test_simpleLiterals.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: clean:
rm -f *.o rm -f *.o
rm -f lex.yy.c rm -f lex.yy.c
rm -f $(EXE) rm -f $(EXE)
rm -f flex.h rm -f flex.h
rm -f *.tok

43
Makefile.save Normal file
View File

@ -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

View File

@ -8,13 +8,15 @@
#include <stdbool.h> #include <stdbool.h>
#include "typedefs.h" #include "typedefs.h"
int line_number = 1, column_number = 1; int line_number = 1, column_number = 1;
bool DEBUG = true; #ifndef DEBUG
#define DEBUG 0
#endif
%} %}
COM ([^*]|\*+[^)*])* COM ([^*]|\*+[^)*])*
ID [A-Za-z_][0-9A-Za-z_]* ID [A-Za-z_][0-9A-Za-z_]*
DIGIT [0-9] 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 */ /* 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\\] 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 */ /* 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 */

View File

@ -26,8 +26,25 @@ int main(int argc, char *argv[]) {
if (arg == TOK_ARG) { if (arg == TOK_ARG) {
fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext); 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; return 0;
} }
@ -40,21 +57,20 @@ char *is_tok(int argc, char *argv[]) {
return INVALID_ARG; return INVALID_ARG;
} }
int ignore_path = 0; const char *basename = input_prog;
int count_since_slash = 0; const char *slash = strrchr(input_prog, '/');
for (int i = 0; i < file_len; i++) { if (slash != NULL) {
count_since_slash++; basename = slash + 1;
if (input_prog[i] == '/') {
ignore_path += count_since_slash;
count_since_slash = 0;
}
} }
// 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 FILE_tok;
} }
return CHECK_OTHER; return CHECK_OTHER;

View File

@ -14,6 +14,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "flex.h" #include "flex.h"
#include "typedefs.h"
extern int line_number, column_number; extern int line_number, column_number;
extern char *yytext; extern char *yytext;
@ -22,4 +23,4 @@ int arg;
int main(int argc, char* argv[]); int main(int argc, char* argv[]);
char *is_tok(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);

View File

@ -1,4 +1,5 @@
45 45
123 123
8392 8392
40 40
200 50 21783

View File

@ -4,7 +4,7 @@
12893 "this is not a string (*one valid token before this*) 12893 "this is not a string (*one valid token before this*)
(* spacey comment here (* spacey comment here
over multiple lines over multiple lines
will it work? *) will it work? *) false
" "
''' '''
'\' '\'