diff --git a/Makefile b/Makefile index a9544ee..5044506 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,19 @@ -# 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: + ./$(EXE) ./tests/test_constants_literals.txt + ./$(EXE) ./tests/test_keywards.alpha + ./$(EXE) ./tests/test_types.alpha + ./$(EXE) ./tests/test_variables.txt + +clean: + rm -f *.o + rm -f lex.yy.c + rm -f $(EXE) \ No newline at end of file diff --git a/lexicalStructure.lex b/lexicalStructure.lex index 7aabad8..bf444a4 100644 --- a/lexicalStructure.lex +++ b/lexicalStructure.lex @@ -6,31 +6,39 @@ #include "typedefs.h" %} +DIGIT [0-9] +CHAR \\\\n|\\\\t|\\\'|[^'\\\n\t] +STRINGVAL CHAR | " " %% +{DIGIT}+ {printf( "C_INTEGER: %s (%d)\n", yytext, atoi( yytext ) );} +"null" {printf( "C_NULL: %s (%d)\n", yytext, atoi( yytext ) );} +'{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 ) );} +"{STRINGVAL}+" {printf( "C_STRING: %s (%d)\n", yytext, atoi( yytext ) );} + .|\n -"integer" {printf("T_INTEGER %s, Token %d\n",yytext, T_INTEGER);} //{return T_INTEGER} -"address" {printf("T_ADDRESS %s, Token %d\n",yytext, T_ADDRESS);} //{return T_ADDRESS} -"Boolean" {printf("T_BOOLEAN %s, Token %d\n",yytext, T_BOOLEAN);} //{return T_BOOLEAN} -"character" {printf("T_CHARACTER %s, Token %d\n",yytext, T_CHARACTER);} //{return T_CHARACTER} -"string" {printf("T_STRING %s, Token %d\n",yytext, T_STRING);} //{return T_STRING} +"integer" {printf("T_INTEGER %s, Token %d\n",yytext, T_INTEGER);} // {return T_INTEGER} +"address" {printf("T_ADDRESS %s, Token %d\n",yytext, T_ADDRESS);} // {return T_ADDRESS} +"Boolean" {printf("T_BOOLEAN %s, Token %d\n",yytext, T_BOOLEAN);} // {return T_BOOLEAN} +"character" {printf("T_CHARACTER %s, Token %d\n",yytext, T_CHARACTER);} // {return T_CHARACTER} +"string" {printf("T_STRING %s, Token %d\n",yytext, T_STRING);} // {return T_STRING} /* KEYWARDS */ - - -"while" {printf("WHILE %s, Token %d\n",yytext, WHILE);} //{return WHILE} -"if" {printf("IF %s, Token %d\n",yytext, IF);} //{return IF} -"then" {printf("THEN %s, Token %d\n",yytext, THEN);} //{return THEN} -"else" {printf("ELSE %s, Token %d\n",yytext, ELSE);} //{return ELSE} -"type" {printf("TYPE %s, Token %d\n",yytext, TYPE);} //{return TYPE} -"function" {printf("FUNCTION %s, Token %d\n",yytext, FUNCTION);} //{return FUNCTION} -"return" {printf("RETURN %s, Token %d\n",yytext, RETURN);} //{return RETURN} -"external" {printf("EXTERNAL %s, Token %d\n",yytext, EXTERNAL);} //{return EXTERNAL} -"as" {printf("AS %s, Token %d\n",yytext, AS);} //{return AS} - +"while" {printf("WHILE %s, Token %d\n",yytext, WHILE);} // {return WHILE} +"if" {printf("IF %s, Token %d\n",yytext, IF);} // {return IF} +"then" {printf("THEN %s, Token %d\n",yytext, THEN);} // {return THEN} +"else" {printf("ELSE %s, Token %d\n",yytext, ELSE);} // {return ELSE} +"type" {printf("TYPE %s, Token %d\n",yytext, TYPE);} // {return TYPE} +"function" {printf("FUNCTION %s, Token %d\n",yytext, FUNCTION);} // {return FUNCTION} +"return" {printf("RETURN %s, Token %d\n",yytext, RETURN);} // {return RETURN} +"external" {printf("EXTERNAL %s, Token %d\n",yytext, EXTERNAL);} // {return EXTERNAL} +"as" {printf("AS %s, Token %d\n",yytext, AS);} // {return AS} %% + int main(int argc, char *argv[]){ argc--, argv++; if ( argc > 0 ) diff --git a/tests/test_constants_literals.txt b/tests/test_constants_literals.txt new file mode 100644 index 0000000..fa713c4 --- /dev/null +++ b/tests/test_constants_literals.txt @@ -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_variables.txt b/tests/test_variables.txt new file mode 100644 index 0000000..e69de29