2
/*============================================================================
3
* Define the scanner for the mathematical expression
4
*============================================================================*/
7
This file is part of the "Mathematical Expression Interpreter" library.
9
Copyright (C) 2008-2010 EDF
11
This library is free software; you can redistribute it and/or
12
modify it under the terms of the GNU Lesser General Public
13
License as published by the Free Software Foundation; either
14
version 2.1 of the License, or (at your option) any later version.
16
This library is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
Lesser General Public License for more details.
21
You should have received a copy of the GNU Lesser General Public
22
License along with this library; if not, write to the Free Software
23
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
/*----------------------------------------------------------------------------
27
* Standard C library headers
28
*----------------------------------------------------------------------------*/
34
/*----------------------------------------------------------------------------
36
*----------------------------------------------------------------------------*/
39
#include <bft_error.h>
41
/*----------------------------------------------------------------------------
43
*----------------------------------------------------------------------------*/
45
#include "mei_hash_table.h"
46
#include "mei_evaluate.h"
48
#include "mei_parser.h"
49
#include "mei_parser_glob.h"
51
/* Redefine macro to redirect scanner input from string instead of standard input. */
54
#define YY_INPUT(b, r, ms) ( r = my_yyinput(b, ms) )
56
int my_yyinput(char *buffer, int max_size);
64
" " {mei_glob_column++; } /* skip whitespace */
65
"\t" {mei_glob_column+=8; } /* skip whitespace */
66
"\n" {mei_glob_line++; mei_glob_column=0; } /* skip whitespace */
67
"#".*\n {mei_glob_line++; mei_glob_column=0; } /* discard commentary (other possibility: "#"[^\n]*"\n" ) */
70
([0-9]+|[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+\-]?[0-9]+)? {
71
yylval.iValue = strtod(yytext, NULL);
72
mei_glob_column += yyleng;
78
strncpy(yylval.sIndex, yytext, yyleng+1);
79
mei_glob_column += yyleng;
84
"min"|"max"|"atan2"|"mod" {
85
strncpy(yylval.sIndex, yytext, yyleng+1);
86
mei_glob_column += yyleng;
90
"exp"|"log"|"sqrt"|"sin"|"cos"|"tan"|"asin"|"acos"|"atan"|"sinh"|"cosh"|"tanh"|"abs"|"int" {
91
strncpy(yylval.sIndex, yytext, yyleng+1);
92
mei_glob_column += yyleng;
96
"while" {mei_glob_column+=5; return WHILE; }
97
"if" {mei_glob_column+=2; return IF; }
98
"else" {mei_glob_column+=4; return ELSE; }
99
"print" {mei_glob_column+=5; return PRINT; }
102
[_a-zA-Z][_a-zA-Z0-9]* {
103
strncpy(yylval.sIndex, yytext, yyleng+1);
105
bft_error(__FILE__, __LINE__, 0, "Error: identifier is to long\n");
106
mei_glob_column += yyleng;
111
">=" {mei_glob_column+=2; return GE; }
112
"<=" {mei_glob_column+=2; return LE; }
113
"==" {mei_glob_column+=2; return EQ; }
114
"!=" {mei_glob_column+=2; return NE; }
115
"||" {mei_glob_column+=2; return OR; }
116
"&&" {mei_glob_column+=2; return AND; }
117
"!" {mei_glob_column++; return *yytext;}
120
[-()<>=+^*/;{},] {mei_glob_column++; return *yytext;}
125
len = strlen("unknown character: ") +1;
126
BFT_MALLOC(buff, len, char);
127
strncpy(buff, "unknown character: ", len);
128
BFT_REALLOC(buff, len+yyleng, char);
129
strncat(buff, yytext, yyleng);
142
/* The calling sequence for 'YY_INPUT()' is 'YY_INPUT(buff,result,max_size)'.
143
Its action is to place up to 'max_size' characters in the character
144
array 'buff' and return in the integer variable 'buffer_length' either
145
the number of characters read or the constant 'YY_NULL' to indicate 'EOF'.
146
The default 'YY_INPUT' reads from the global file-pointer 'yyin'. */
148
/* max_sise = 8192 bytes. One can change this default
149
value with YY_BUF_SIZE macro */
153
my_yyinput(char *buffer, int max_size)
157
if (max_size < mei_glob_string_end - mei_glob_string_begin)
159
buffer_length = max_size;
163
buffer_length = mei_glob_string_end - mei_glob_string_begin;
166
if (buffer_length > 0)
168
memcpy(buffer, mei_glob_string_begin, buffer_length);
169
mei_glob_string_begin +=buffer_length;
172
return buffer_length;