diff --git a/lexicalStructure.lex b/lexicalStructure.lex index 5426637..22632ba 100644 --- a/lexicalStructure.lex +++ b/lexicalStructure.lex @@ -1,41 +1,50 @@ -/* Lexical Analysis with Flex (2.6.0) We used some of the code from this manual */ -/* so we placed the citation here. */ -/* definitions */ + /* so we placed the citation here. */ + /* definitions */ %option noyywrap -%{ -#include "typedefs.h" + +%{ +#include "typedefs.h" %} +DIGIT [0-9] +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 */ +STARTCOM \(\* +ENDCOM \*\) +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 -"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} + /* rules */ +{DIGIT}+ {printf( "C_INTEGER: %s (%d)\n", yytext, atoi( yytext ) );} - /* KEYWARDS */ +"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*/ -"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} +"true" {printf( "C_TRUE: %s (%d)\n", yytext, atoi( yytext ) );} +"false" {printf( "C_FALSE: %s (%d)\n", yytext, atoi( yytext ) );} + +\"{SCHAR}*\" {printf( "C_STRING: %s (%d)\n", yytext, atoi( yytext ) );} + +{STARTCOM}{COMMENTCHAR}*{ENDCOM} {printf( "COMMENT: %s (%d)\n", yytext, atoi( yytext ) );} + +.|\n %% -int main(int argc, char *argv[]){ - argc--, argv++; - if ( argc > 0 ) - yyin = fopen( argv[0], "r" ); - else - yyin = stdin; - yylex(); + /* 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/simpleIntTest.txt b/simpleIntTest.txt new file mode 100644 index 0000000..507ec22 --- /dev/null +++ b/simpleIntTest.txt @@ -0,0 +1,4 @@ +45 +123 +8392 + diff --git a/simpleLiteralTest.a b/simpleLiteralTest.a new file mode 100644 index 0000000..f97cf52 --- /dev/null +++ b/simpleLiteralTest.a @@ -0,0 +1,46 @@ +"this is a string" 721398 'g' '/n' (* should print 3 tokens before this *) +' +' +12893 "this is not a string (*one valid token before this*) +(* spacey comment here +over multiple lines +will it work? *) +" +''' +'\' +false +(**) +''' +nullfalse +"nulltrue +null +'7' +true +'189 +'\t' +'"' +'/' +'\n' +'\'' +'\t' +'\\' +'n' +'\' +'fdsf' +(*/jnewjno2893u86^ Lots of random characters /n /t '") *) +' +' +' ' +''' +"STRINGwithnotSPaces" +' ' +'\ ' +"J" +"" +" " +\"\" +"{SCHAR}" +"SCHAR" +"[SCHAR]" +"FINAL: I'd think this is a legal \"string\" that contains \n \t several escaped characters, isn't it?" +"I'd think this is a legal \"string\" that contains several \\n \t escaped characters, isn't it?"