diff --git a/Makefile b/Makefile index a9544ee..3a7cffc 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,16 @@ -# Basic Makefile example from flex documentation -- provides explicit rules -# Creates "myprogram" from "scan.l" and "myprogram.c" -# -#LEX=flex -#myprogram: scan.o myprogram.o - #$(CC) -o $@ $(LDFLAGS) $^ -#myprogram.o: myprogram.c - #$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^ -#scan.o: scan.c - #$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^ -#scan.c: scan.l - #$(LEX) $(LFLAGS) -o $@ $^ -#clean: - #$(RM) *.o scan.c +CC := gcc +FLEX := flex +LEX := lexicalStructure.lex +EXE := lexicalStructure + +lexicalStructure: + $(FLEX) $(LEX) + $(CC) lex.yy.c -o $(EXE) + +test_operators: + ./$(EXE) ./tests/test_operators.alpha + +clean: + rm -f *.o + rm -f lex.yy.c + rm -f $(EXE) diff --git a/lexicalStructure.lex b/lexicalStructure.lex index baadc20..e658e4d 100644 --- a/lexicalStructure.lex +++ b/lexicalStructure.lex @@ -1,11 +1,13 @@ - /* so we placed the citation here. */ - /* definitions */ +/* Lexical Analysis with Flex (1.6.0) We used some of the code from this manual */ +/* so we placed the citation here. */ +/* definitions */ + %option noyywrap %{ #include "typedefs.h" %} - + int line_number = 1, column_number = 1; DIGIT [0-9] CHAR \\n|\\t|\\'|[^'\n\t\\] @@ -18,33 +20,61 @@ COMMENTCHAR [^\*]|\*[^\)] /*Making the contents of a comment anything that is either not a * or not a * followed by ) to terminate comments at the first ENDCOM */ %% + +\n line_number++ column_number = 1; +. column_number++; + +"integer" {return T_INTEGER} +"address" {return T_ADDRESS} +"Boolean" {return T_BOOLEAN} +"character" {return T_CHARACTER} + + /* rules */ {DIGIT}+ {printf( "C_INTEGER: %s (%d)\n", yytext, atoi( yytext ) );} "null" {printf( "C_NULL: %s (%d)\n", yytext, atoi( yytext ) );} +"while" {return WHILE} +"if" {return IF} +"then" {return THEN} +"else" {return ELSE} +"type" {return TYPE} +"function" {return FUNCTION} +"return" {return RETURN} +"external" {return EXTERNAL} +"as" {return AS} + '{CHAR}' {printf( "C_CHARACTER: %s (%d)\n", yytext, atoi( yytext ) );} /*using double \ per documentation to show escaped chars*/ "true" {printf( "C_TRUE: %s (%d)\n", yytext, atoi( yytext ) );} + "false" {printf( "C_FALSE: %s (%d)\n", yytext, atoi( yytext ) );} + /* OPERATORS */ + +"+" {return ADD;} +"-" {return SUB_OR_NEG;} +"*" {return MUL;} +"/" {return DIV;} +"%" {return REM;} +"<" {return LESS_THAN;} +"=" {return EQUAL_TO;} +":=" {return ASSIGN;} +"!" {return NOT;} +"&" {return AND;} +"|" {return OR;} +"." {return DOT;} +"reserve" {return RESERVE;} +"release" {return RELEASE;} + + \"{SCHAR}*\" {printf( "C_STRING: %s (%d)\n", yytext, atoi( yytext ) );} \(\*{COMMENTCHAR}*\*\) {printf( "COMMENT: %s (%d)\n", yytext, atoi( yytext ) );} .|\n + %% - /* user code */ - -int main( int argc, char **argv ) -{ - argc--, argv++; /* skip over program name */ - if ( argc > 0 ) - yyin = fopen( argv[0], "r" ); - else - yyin = stdin; - yylex(); - -} diff --git a/runner.c b/runner.c index e69de29..3d467b8 100644 --- a/runner.c +++ b/runner.c @@ -0,0 +1,38 @@ +#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"); + + if (check_input == INVALID_ARG) { + return -1; + } + + while (0 != (token = yylex())) { + if (check_input != DIFF_ARG) { + fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext); + } + } + + return 0; +} + +char *is_tok(int argc, char *argv[]) { + if (argc == 3 && strcmp("-tok", argv[1])) { + char *input_prog = argv[2]; + int file_len = strlen(input); + //check that input program is a .alpha file + if (strcmp(".alpha", input_prog[file_len - ALPHA_OFFSET]) != 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 + return FILE_tok; + } + return DIFF_ARG; +} diff --git a/runner.h b/runner.h new file mode 100644 index 0000000..080d5c1 --- /dev/null +++ b/runner.h @@ -0,0 +1,13 @@ +#define ALPHA_OFFSET 5 +#define TOK_LEN 3 +#define INVALID_ARG "invalid" +#define DIFF_ARG "diff" +#include +#include +#include "lex.yy.c" + +extern int line_number, column_number; +extern char *yytext; + +int main(int argc, char* argv); +char *is_tok(int argc, char* argv);#define ALPHA_OFFSET 5 diff --git a/tests/test_operators.alpha b/tests/test_operators.alpha new file mode 100644 index 0000000..b985387 --- /dev/null +++ b/tests/test_operators.alpha @@ -0,0 +1,23 @@ ++ +- +* +/ +\ +% +< +> += +:= +=: +: += +! +& +| +. +relEASE +release +RELEASE +reserve +RESERVE +reSERVe \ No newline at end of file