-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathcalc.l
50 lines (40 loc) · 1.14 KB
/
calc.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
// An interactive calculator with Unicode identifier variables
// Builds with bison-bridge to pass Lexer object 'lexer' to bison parser
// $ reflex calc.l
// $ bison -y -d calc.y
// $ c++ -o calc y.tab.c lex.yy.cpp -lreflex
// Example:
// $ ./calc
// π = 3.14
// => 3.14
// π/2
// => 1.57
%top{
#include <cstdlib>
#include <string>
#include <map>
// fixes problem with Bison 2.7 that needs the Lexer class type:
class REFLEX_OPTION_lexer; // = %option lexer (which is 'Lexer' by default)
#include "calc.tab.h"
}
%{
extern void yyerror(Lexer *lexer, const char *msg);
%}
%class{
public:
std::map<std::wstring,double> map;
private:
std::wstring var;
}
%option fast bison-bridge header-file interactive unicode freespace
var \p{UnicodeIdentifierStart} \p{UnicodeIdentifierPart}*
exp [Ee] [-+]? \d+
num \d* (\d | \.\d | \d\.) \d* {exp}?
%%
{var} { var = wstr(); yylval.var = &var; return 'V'; }
{num} { yylval.num = strtod(text(), NULL); return 'N'; }
[-+*/=()] { return *text(); }
\n { return '\n'; }
\s // ignore space
. { yyerror(this, "mystery character"); }
%%