Website Summary Files Commits Branches @commit @diff @stats
path: root / demo / flex / JavaScript.flex
blob: 4df5ca3c978e6f23f6ce22ad4f8a96a323713048 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
}