/* scanner for a toy Pascal-like language */
%{
/* need this for the call to atof() below */
#include
int exit_state;
%}
DIGIT [0-9]
ID [A-Za-z_][A-Za-z0-9_]*
%x IN_COMMENT
%%
{DIGIT}+ {
printf( "num %x\n", atoi(yytext));
}
func|end|if|then|else|exit|start|repeat|not|and {
printf( "%s\n", yytext );
}
{ID} printf( "id %s\n", yytext );
";"|"("|")"|","|"*"|"+"|"->"|"-"|">"|"=" printf( "%s\n", yytext );
{
"/*" {
BEGIN(IN_COMMENT);
exit_state=1;
}
}
{
"*/" {
BEGIN(INITIAL);
exit_state = 0;
}
[^*\n]+ // eat comment in chunks
"*" // eat the lone star
\n yylineno++;
}
[ \t\n]+ /* eat up whitespace */
. {
printf( "Unrecognized character: %s\n", yytext );
exit_state = 1;
}
%%
main( argc, argv )
int argc;
char **argv;
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
exit (exit_state);
}