This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathequation_scanner.ll
125 lines (109 loc) · 3.4 KB
/
equation_scanner.ll
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
// Copyright 2015-2018 RWTH Aachen University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
%{ /* -*- C++ -*- */
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <string>
#include "equation_driver.hh"
#include "equation_parser.tab.hh"
//The location of the current token.
static yy::location loc;
%}
%option noyywrap nounput batch debug noinput
blank [ \t]+
string \"[^\n"]+\"
idPart [[:alpha:]][[:alnum:]]*
id {idPart}(.{idPart}+)*
integer 0|[+-]?[1-9][0-9]*
float [+-]?[0-9]+.[0-9]+
boolean true|false
function {id}\([^\)]*\)
%{
//Code run each time a pattern is matched.
#define YY_USER_ACTION loc.columns(yyleng);
%}
%%
%{
//Code run each time yylex is called.
loc.step();
%}
{blank}+ { loc.step(); } /* ignore white spaces but use them for better error messages */
[\n]+ { loc.lines(yyleng); loc.step(); } /* newlines but use them for better error messages */
{function} {
string value = string(yytext);
return yy::EquationParser::make_FUNCTION(value, loc);
}
"&&" |
"&" { return yy::EquationParser::make_AND(loc); }
"||" |
"|" { return yy::EquationParser::make_OR(loc); }
"!" { return yy::EquationParser::make_NOT(loc); }
"==" |
"=" { return yy::EquationParser::make_EQ(loc); }
"!=" { return yy::EquationParser::make_NEQ(loc); }
"<" { return yy::EquationParser::make_LESS(loc); }
"<=" { return yy::EquationParser::make_LEQ(loc); }
">" { return yy::EquationParser::make_GREATER(loc); }
">=" { return yy::EquationParser::make_GEQ(loc); }
"(" { return yy::EquationParser::make_LPAREN(loc); }
")" { return yy::EquationParser::make_RPAREN(loc); }
{string} {
string value = string(yytext);
//remove the quotes from the value
value = value.substr(1, value.size() - 2);
return yy::EquationParser::make_STRING(value, loc);
}
{boolean} {
bool value;
if(string(yytext) == "true") {
value = true;
} else {
value = false;
}
return yy::EquationParser::make_BOOLEAN(value, loc);
}
{integer} {
errno = 0;
int n = strtol(yytext, NULL, 10);
if(!(INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
driver.error(loc, "integer is out of range");
return yy::EquationParser::make_INTEGER(n, loc);
}
{float} {
errno = 0;
double f = atof(yytext);
if(errno == ERANGE)
driver.error(loc, "could not be converted to double");
return yy::EquationParser::make_FLOAT(f, loc);
}
{id} { return yy::EquationParser::make_ID(yytext, loc); }
. { driver.error (loc, "invalid character"); }
<<EOF>> { return yy::EquationParser::make_END(loc); }
%%
void EquationDriver::scan_begin () {
yy_flex_debug = trace_scanning;
if (file.empty () || file == "-")
yyin = stdin;
else if (!(yyin = fopen (file.c_str (), "r")))
{
error ("cannot open " + file + ": " + strerror(errno));
exit (EXIT_FAILURE);
}
}
void EquationDriver::scan_end () {
fclose (yyin);
}