%{ /* * toy.l (an example from various parts of the Flex manual) * * To try out this example, do the following on the instructional machines: * % flex -otoy.c toy.l * % gcc -o toy toy.c -lfl -L/usr/sww/lib * % ./toy * [type stuff here] */ /* We need math.h for the atof() call below. */ #include %} %x COMMENT DIGIT [0-9] ID [a-z][a-z0-9]* %% [^*]* /* skip */ "*"+[^*/]* /* skip */ "*"+"/" BEGIN(INITIAL); "/*" BEGIN(COMMENT); {DIGIT}+ printf("int: %d\n", atoi(yytext)); {DIGIT}+"."{DIGIT}* printf("float: %g\n", atof(yytext)); if|then|else|fi printf("keyword: %s\n", yytext); {ID} printf("identifier: %s\n", yytext); "+"|"-"|"*"|"/" printf("operator: %s\n", yytext); [ \t\n]+ /* eat up whitespace */ . printf("unrecognized character: %s\n", yytext); %% int main(int argc, char **argv) { ++argv; --argc; /* skip over program name */ if (argc > 0) { yyin = fopen(argv[0], "r"); } else { yyin = stdin; } yylex(); }