/* The Scanner for Formula Calculator */ /*============= 1. The definitions section ================*/ %{ #include #include #include #include "parser.h" /* Common definitions of parser and scanner. */ /* Includes definitions of terminals */ /* generated by bison (PLUS, MINUS, etc.) */ /* and the definition of YYSTYPE -- the type of the */ /* values associated with terminals and nonterminals. */ %} /* Macro defining the exponent in double constant */ EXP ((E|e)("+"|"-")?[0-9]+) %% /*============= 2. The rules (regular expressions) section ======*/ "+" { return PLUS; } "-" { return MINUS; } "*" { return MULT; } "/" { return DIV; } "(" { return LPAR; } ")" { return RPAR; } (" "|\t)* {} /* Skip space */ /* Integer constant */ [0-9]+ { yylval.int_value = atoi(yytext); return INT_CONST; } /* Double constant may be in one of the following 3 forms: */ /* 1) .5, .5e-20, 1.2, 123.456E+10 (mandatory point and fraction) */ /* 2) 1., 123.e+10, 0.2E-20, 35.3 (mandatory integer and point) */ /* 3) 1e+10, 2e20, 1E-12 (mandatory integer and exponent) */ /* The expression for double is the union "|" of 3 subexpressions */ ([0-9]*"."[0-9]+{EXP}?)|([0-9]+"."[0-9]*{EXP}?)|([0-9]+{EXP}) { yylval.double_value = atof(yytext); return DOUBLE_CONST; } /* Name */ [a-zA-Z_]+[a-zA-Z_0-9]* { strcpy(yylval.name, yytext); return NAME; } \n|\r\n { return ENDL; } /* End of line in Unix or MS DOS form */ . { return ILLEGAL; } /* Any other character */ %% /*============= The user programs section ===================*/ int yywrap() { return 1; } /* Stop at end of file */