expr_lex.l 2.35 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
/* 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 */

59
"remove("    { return REMOVE; }
60

Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
"print("    { return PRINT; }

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

68
69
70
71
72
{LPH}{LPHDGT}*\.{LPH}* {
                /* Recognize coordinates, e.g., var_nm.x (lon of var_nm) */
                yylval->varnm = strdup(yytext);
                return VARIABLE;
             }
73
74

{LPH}{LPHDGT}* {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
                yylval->varnm = strdup(yytext);
76
77
78
79
                return VARIABLE;
             }


80
[-()=+*/;,{}^.?:] {
81
82
83
                return *yytext;
             }

84
"<=>"           return LEG;
85
86
">="            return GE;
"<="            return LE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
88
">"             return GT;
"<"             return LT;
89
90
"=="            return EQ;
"!="            return NE;
91
92
"&&"            return AND;
"||"            return OR;
93

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

96
.               yyerror(NULL, NULL, "Unknown character");
97
%%