-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_scanner.reflex
169 lines (149 loc) · 4.55 KB
/
calculator_scanner.reflex
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 2006.11.10 - Copyright Victor Dods - Licensed under Apache 2.0
%targets cpp
// ///////////////////////////////////////////////////////////////////////////
// cpp scanner header-file-related directives
// ///////////////////////////////////////////////////////////////////////////
%target.cpp.header_filename "calculator_scanner.hpp"
%target.cpp.implementation_filename "calculator_scanner.cpp"
%target.cpp.top_of_header_file %{
#if !defined(CALCULATOR_SCANNER_HPP_)
#define CALCULATOR_SCANNER_HPP_
#include "calculator.hpp"
#include <sstream>
#include <stdlib.h>
#include "calculator_parser.hpp"
namespace Calculator {
%}
%target.cpp.constructor_parameters { string const &input_string }
%target.cpp.class_name Scanner
%target.cpp.bottom_of_class {
istringstream m_input;
}
%target.cpp.bottom_of_header_file %{
} // end of namespace Calculator
#endif // !defined(CALCULATOR_SCANNER_HPP_)
%}
// ///////////////////////////////////////////////////////////////////////////
// cpp scanner implementation-file-related directives
// ///////////////////////////////////////////////////////////////////////////
%target.cpp.top_of_implementation_file %{
namespace Calculator {
%}
%target.cpp.constructor_actions {
m_input.str(input_string);
}
%target.cpp.bottom_of_scan_method_actions %{
return Parser::Token(Parser::Terminal::BAD_TOKEN);
%}
%target.cpp.bottom_of_implementation_file %{
} // end of namespace Calculator
%}
// ///////////////////////////////////////////////////////////////////////////
// cpp scanner I/O parameters
// ///////////////////////////////////////////////////////////////////////////
%target.cpp.return_type "Parser::Token"
// we definitely want interactive, because the calculator will almost always
// be used from the commandline.
%target.cpp.generate_interactive_scanner
%target.cpp.return_true_iff_input_is_at_end {
return m_input.peek() == char_traits<char>::eof();
}
%target.cpp.return_next_input_char {
return m_input.get();
}
%target.cpp.rejection_actions {
assert(false && "we should have handled this in the catch-all rule");
}
%target.cpp.reset_for_new_input_actions {
}
// ///////////////////////////////////////////////////////////////////////////
// cpp scanner misc directives
// ///////////////////////////////////////////////////////////////////////////
%target.cpp.generate_debug_spew_code
// ///////////////////////////////////////////////////////////////////////////
// target-independent scanner directives
// ///////////////////////////////////////////////////////////////////////////
// an unsigned integer literal
%macro INTEGER (0|[1-9][0-9]*)
// an unsigned floating-point literal with optional base 10 exponent
%macro FLOAT ({INTEGER}\.[0-9]+([eE][\-+]?{INTEGER})?)
// arithmetic operators and brackets
%macro OPERATOR ([+\-*/\^()\\])
// non-newline whitespace
%macro WHITESPACE ([ \t])
// newline
%macro NEWLINE (\n)
// end-of-file condition
%macro END_OF_FILE (\z)
%start_with_state_machine MAIN
%%
// ///////////////////////////////////////////////////////////////////////////
// state machines and constituent regex rules
// ///////////////////////////////////////////////////////////////////////////
%state_machine MAIN
:
({INTEGER})
%target.cpp {
return Parser::Token(Parser::Terminal::NUMBER, strtod(accepted_string.c_str(), NULL));
}
|
({FLOAT})
%target.cpp {
return Parser::Token(Parser::Terminal::NUMBER, strtod(accepted_string.c_str(), NULL));
}
|
(pi)
%target.cpp {
return Parser::Token(Parser::Terminal::NUMBER, M_PI);
}
|
(e)
%target.cpp {
return Parser::Token(Parser::Terminal::NUMBER, M_E);
}
|
(r)
%target.cpp {
return Parser::Token(Parser::Terminal::RESULT);
}
|
(log)
%target.cpp {
return Parser::Token(Parser::Terminal::LOG);
}
|
(help)
%target.cpp {
return Parser::Token(Parser::Terminal::HELP);
}
|
(mod)
%target.cpp {
return Parser::Token(Parser::Terminal::MOD);
}
|
({OPERATOR})
%target.cpp {
return Parser::Token(Parser::Token::Id(accepted_string[0]));
}
|
({WHITESPACE})
%target.cpp {
// ignore all non-newline whitespace
}
|
({NEWLINE})
%target.cpp {
return Parser::Token(Parser::Terminal::NEWLINE);
}
|
({END_OF_FILE})
%target.cpp {
return Parser::Token(Parser::Terminal::END_);
}
|
(.) // catch-all rule -- any other char is unrecognized
%target.cpp {
return Parser::Token(Parser::Terminal::BAD_TOKEN);
}
;