-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer2.l
53 lines (45 loc) · 1.81 KB
/
lexer2.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
51
52
53
%{
#include "Parser2.hpp"
#include "Scanner2.hpp"
#define YY_DECL int calc::Scanner::lex(calc::Parser::semantic_type *yylval)
// workaround for bug in flex 2.6.4
#define yypanic(X) (void)(X)
%}
%option c++ interactive noyywrap noyylineno nodefault outfile="Scanner2.cpp"
dseq ([[:digit:]]+)
dseq_opt ({dseq}?)
frac (({dseq_opt}"."{dseq})|{dseq}".")
exp ([eE][+-]?{dseq})
exp_opt ({exp}?)
integer ({dseq})
float (({frac}{exp_opt})|({dseq}{exp}))
intvar ([[:upper:]])
fltvar ([[:lower:]])
%%
{integer} yylval->emplace<long long>(strtoll(YYText(), nullptr, 10)); return Parser::token::INT;
{float} yylval->emplace<double>(strtod(YYText(), nullptr)); return Parser::token::FLT;
{intvar} yylval->emplace<char>(YYText()[0]); return Parser::token::INTVAR;
{fltvar} yylval->emplace<char>(YYText()[0]); return Parser::token::FLTVAR;
"+" return Parser::token::PLUS;
"-" return Parser::token::MINUS;
"*" return Parser::token::MULTIPLY;
"/" return Parser::token::DIVIDE;
"%" return Parser::token::MODULO;
"!" return Parser::token::FACTORIAL;
"^" return Parser::token::EXPONENT;
"(" return Parser::token::LPAREN;
")" return Parser::token::RPAREN;
"=" return Parser::token::ASSIGN;
\n return Parser::token::EOL;
<<EOF>> return Parser::token::YYEOF;
. /* no action on unmatched input */
%%
int yyFlexLexer::yylex() {
throw std::runtime_error("Invalid call to yyFlexLexer::yylex()");
}
int main() {
calc::Scanner scanner{ std::cin, std::cerr };
calc::Parser parser{ &scanner };
std::cout.precision(10);
parser.parse();
}