Merge pull request #12 from UB-CSE443/Sprint1-TokenLocationLogic-NoTask
Sprint1 token location logic no task
This commit is contained in:
16
Makefile
16
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
|
||||
|
43
Makefile.save
Normal file
43
Makefile.save
Normal 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
|
@ -8,13 +8,15 @@
|
||||
#include <stdbool.h>
|
||||
#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 */
|
||||
|
42
runner.c
42
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;
|
||||
|
3
runner.h
3
runner.h
@ -14,6 +14,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
int is_alpha_file(char *file, int file_len);
|
||||
|
@ -1,4 +1,5 @@
|
||||
45
|
||||
123
|
||||
8392
|
||||
|
||||
40 40
|
||||
200 50 21783
|
||||
|
@ -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
|
||||
"
|
||||
'''
|
||||
'\'
|
||||
|
Reference in New Issue
Block a user