Website Summary Files Commits Branches @commit @diff @stats
path: root / demo / flex / JavaScript.flex
diff options
context:
space:
mode:
1 files changed, 67 insertions, 0 deletions
-rw-r--r--demo/flex/JavaScript.flex67
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 <math.h>
+%}
+
+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;
+}