expr_lex.l 1.99 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
/* flex --outfile=expr_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* 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}*/[ ]*\( {
                yylval->fname = (char *) strdupx(yytext);
                return FUNCTION;
             } /* end functions */


{LPH}{LPHDGT}* {
                yylval->varnm = (char *) strdupx(yytext);
                return VARIABLE;
             }


[-()<>=+*/;{}^.] {
                return *yytext;
             }

">="            return GE;
"<="            return LE;
"=="            return EQ;
"!="            return NE;

[ \t\n]+        ;       /* ignore whitespace */

83
.               yyerror(NULL, NULL, "Unknown character");
84
%%