%option interactive %{ /* * * * * * * * * * * * * * * DEFINITIONS * * * * * * * * * * * * * * */ %} %{ // Ray Byler, CS560, 23 April 96 // Expression Interpreter, Lexer Portion // y.tab.h contains the token number values produced by the parser #include #include "exp.h" #include "y.tab.h" extern int line_num; %} DIGIT [0-9] LETTER [a-zA-Z] %{ /* * * * * * * * * * * * * STATES * * * * * * * * * * * * */ %} %x ERROR %% %{ /* * * * * * * * * * * * RULES * * * * * * * * * * * */ %} print { return PRINT; } {DIGIT}+ { yylval.num = atof(yytext); return NUMBER; } {LETTER}[0-9a-zA-Z]* { yylval.id = strdup(yytext); return ID; } [ \t\f\r] ; // ignore white space \n { line_num++; return NEWLINE; } "-" { return MINUS; } "+" { return PLUS; } "*" { return TIMES; } "/" { return DIVIDE; } "(" { return LPAREN; } ")" { return RPAREN; } "=" { return EQUALS; } . { BEGIN(ERROR); yymore(); } [^{DIGIT}{LETTER}+\-/*()= \t\n\f\r] { yymore(); } (.|\n) { yyless(yyleng-1); printf("error token: %s on line %d\n", yytext, line_num); BEGIN(INITIAL); } %%