expr_lex.l 2.1 KB
Newer Older
1
/* flex -oexpr_lex.c expr_lex.l */
2
3
4
%{
#include <string.h>
#include <stdlib.h>
5
#include <math.h>
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#ifndef M_E
#define M_E		2.7182818284590452354	/* e */
#endif

#ifndef M_PI
#define M_PI		3.14159265358979323846	/* pi */
#endif

#include "expr.h"
#include "expr_yacc.h"

%}

20
21
22
23
%option reentrant bison-bridge
%option noyywrap
%option nounput

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Definitions:  LMB92 p. 153
   Definitions are named regular expressions, e.g., DGT [0-9]
   Definitions enclosed in braces in rules section, e.g. {DGT}, are interpreted literally
   DGT [0-9] Digit
   LPH [A-Za-z_] Alphabetic character
   LPHDGT [A-Za-z0-9_] Alphanumeric character
   XPN [eE][+-]?[0-9]+ Real number Exponent */
DGT [0-9]
LPH [A-Za-z_]
LPHDGT [A-Za-z0-9_]
XPN [eE][+-]?[0-9]+

%%

#.*          ; /* ignore comments */

M_PI         {
                yylval->cvalue = M_PI;
                return CONSTANT;
             }

M_E          {
                yylval->cvalue = M_E;
                return CONSTANT;
             }

{DGT}*\.?{DGT}*({XPN})?[LlDd]?|{DGT}*({XPN})[LlDd]? {
               /* constant */
               /* NB: Tempted to prepend lexer expressions for floats and doubles with [+-]? 
                so that unary plus/minus is handled in lexer rather than parser.
                However, this has unintended side effects so let parser handle it for now */
                yylval->cvalue = strtod(yytext, (char **)NULL);
                return CONSTANT;
             } /* end constant */


{LPH}{LPHDGT}*/[ ]*\( {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
                yylval->fname = strdup(yytext);
62
63
64
65
66
                return FUNCTION;
             } /* end functions */


{LPH}{LPHDGT}* {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
                yylval->varnm = strdup(yytext);
68
69
70
71
                return VARIABLE;
             }


Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
[-()=+*/;{}^.?:] {
73
74
75
                return *yytext;
             }

76
"<=>"           return LEG;
77
78
">="            return GE;
"<="            return LE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
">"             return GT;
"<"             return LT;
81
82
"=="            return EQ;
"!="            return NE;
83
84
"&&"            return AND;
"||"            return OR;
85

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
[ \t\n\r]+      ;       /* ignore whitespace */
87

88
.               yyerror(NULL, NULL, "Unknown character");
89
%%