%{ /* This file is part of exprparser, a lex/yacc based expression parser Copyright (C) 2002-6 Toby Thain, toby@telegraphics.com.au This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "node.h" #include "y.tab.h" #define yywrap() 1 int tokpos; extern YYSTYPE yylval; #define OP(x) \ tokpos += yyleng; \ yylval = newnode(x); \ return x; %} DIGIT [0-9] %% [ \t]+ tokpos += yyleng; /* ignore whitespace */ [\n\r] tokpos = 0; {DIGIT}*(\.{DIGIT}+)?([eE][\+\-]?{DIGIT}+)? { /* a constant; make new tree node */ tokpos += yyleng; yylval = newnode(TOK_NUM); yylval->v.value = atof(yytext); return TOK_NUM; } "<=" { OP(LE); } ">=" { OP(GE); } "==" { OP(EQ); } "!=" { OP(NE); } "&&" { OP(LOGAND); } "||" { OP(LOGOR); } [!+\-*/<>^?] { OP(yytext[0]); } /* an operator; make new tree node */ [(),:] { /* these tokens are just sugar; they are not tree nodes */ ++tokpos; return yytext[0]; } [a-zA-Z0-9_]+ { /* an identifier; look it up */ struct sym_rec *s = lookup(yytext); tokpos += yyleng; if(s){ /* a known function or variable; make a tree node for it */ yylval = newnode(s->token); yylval->v.sym = s; return s->token; }else return TOK_UNKNOWN; } . { ++tokpos; return TOK_BADCHAR; } %%