From c7345482e39982e53a2aae1525632f8ebebb0529 Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Fri, 28 Sep 2018 01:35:30 +0300 Subject: Flex-based lexer for JavaScript --- demo/flex/JavaScript.flex | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 demo/flex/JavaScript.flex (limited to 'demo/flex/JavaScript.flex') diff --git a/demo/flex/JavaScript.flex b/demo/flex/JavaScript.flex new file mode 100644 index 0000000..4df5ca3 --- /dev/null +++ b/demo/flex/JavaScript.flex @@ -0,0 +1,67 @@ +/* scanner for a toy JavaScript-like language */ + +%{ +/* need this for the call to atof() below */ +#include +%} + +DIGIT [0-9] +ID [a-z_$][a-z0-9_$]* +STRQ '([^\']|(\\\'))*' +STRDQ "([^\"]|(\\\"))*" + +%% + +{DIGIT}+ { + printf( "An integer: %s (%d)\n", yytext, + atoi( yytext ) ); + } + +{DIGIT}+"."{DIGIT}* { + printf( "A float: %s (%g)\n", yytext, + atof( yytext ) ); + } + +if|else|for|while|do|break|switch|case|function|return|new|delete|try|throw|catch|class|public|private { + printf( "A keyword: %s\n", yytext ); +} + +{ID} printf( "An identifier: %s\n", yytext ); +{STRQ}|{STRDQ} printf( "A string: %s\n", yytext ); + +"."|"+"|"-"|"*"|"/"|"="|"!"|">"|"<"|"!=="|"==="|">="|"<="|"++"|"--"|"=>" printf( "An operator: %s\n", yytext ); + +";" printf( "A semicolumn\n" ); +"(" printf( "A left parenthesis\n" ); +")" printf( "A right parenthesis\n" ); +"{" printf( "A left bracket\n" ); +"}" printf( "A right bracket\n" ); +":" printf( "A colon\n" ); +"?" printf( "A question mark\n" ); + +[ \t\n]+ /* eat up whitespace */ + +. printf( "Unrecognized character: %s\n", yytext ); + +%% + +int yywrap() +{ + return 1; +} + + +int main(int argc, const char* argv[]) +{ + ++argv, --argc; /* skip over program name */ + if ( argc > 0 ) + yyin = fopen( argv[0], "r" ); + else + yyin = stdin; + + /* YY_BUFFER_STATE hello = yy_scan_string("(x)=>{ return x !== 1; }"); */ + /* yypush_buffer_state(hello); */ + yylex(); + /* yy_delete_buffer(hello); */ + return 0; +} -- cgit v1.3.1-23-gd82d