/* scanner for a toy Pascal-like language */ %{ #include /* needed for call to atof() */ %} DIG [0-9] ID [a-z][a-z0-9]* %% {DIG}+ printf("Integer: %s (%d)\n", yytext, atoi(yytext)); {DIG}+"."{DIG}* printf("Float: %s (%g)\n", yytext, atof(yytext)); if|then|begin|end printf("Keyword: %s\n",yytext); "(" printf("Open paren\n"); ")" printf("Open paren\n"); {ID} printf("Identifier: %s\n",yytext); "+"|"-"|"*"|"/" printf("Operator: %s\n",yytext); ">"|"<" printf("Relational operator: %s\n",yytext); "{"[^}\n]*"}" /* skip one-line comments */ [ \t\n]+ /* skip whitespace */ . printf("UNRECOGNIZED: %s\n",yytext); %% main(){ yylex(); }