%{ /* From the flex manual, Chapter 4: This is the beginnings of a simple scanner for a language like Pascal. It identifies different types of tokens and reports on what it has seen. See txt/pascal.txt for a sample input file */ extern int fileno(FILE *); /* to avoid gcc warning */ #include /* for atoi() and atof() */ %} DIGIT [0-9] ID [a-z][a-z0-9]* %% {DIGIT}+ { printf( "An integer: %s (%d)\n", yytext, atoi( yytext ) ); } {DIGIT}+"."{DIGIT}* { printf( "A float: %s (%g)\n", yytext, atof( yytext ) ); } if|then|begin|end|procedure|function { printf( "A keyword: %s\n", yytext ); } {ID} printf( "An identifier: %s\n", yytext ); "+"|"-"|"*"|"/" printf( "An operator: %s\n", yytext ); "{"[^{}\n]*"}" /* eat up one-line comments -- NOTE mistakes \^ and }} in the manual */ [ \t\n]+ /* eat up whitespace */ . printf( "Unrecognized character: %s\n", yytext ); %% void (*jj_junk)(int,char *) = yyunput; /* avoid gcc warnings */ int (*jj2_junk)(void) = input; int main( int argc, char *argv[] ) { ++argv, --argc; /* skip over program name */ if ( argc > 0 ) yyin = fopen( argv[0], "r" ); else yyin = stdin; return yylex(); }