-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_parser.trison
215 lines (184 loc) · 5.11 KB
/
calculator_parser.trison
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// 2007.11.10 - Copyright Victor Dods - Licensed under Apache 2.0
%targets cpp
%target.cpp.header_filename "calculator_parser.hpp"
%target.cpp.implementation_filename "calculator_parser.cpp"
%target.cpp.top_of_header_file %{
#if !defined(CALCULATOR_PARSER_HPP_)
#define CALCULATOR_PARSER_HPP_
#include "calculator.hpp"
namespace Calculator {
class Scanner;
%}
%target.cpp.class_name Parser
%target.cpp.bottom_of_class {
bool ShouldPrintResult () const { return m_should_print_result; }
void SetInputString (string const &input_string);
private:
Scanner *m_scanner;
double m_last_result;
double m_modulus;
bool m_should_print_result;
}
%target.cpp.bottom_of_header_file %{
} // end of namespace Calculator
#endif // !defined(CALCULATOR_PARSER_HPP_)
%}
%target.cpp.top_of_implementation_file %{
#include "calculator_scanner.hpp"
#define MODULO(x) (m_modulus == 0 ? x : fmod(x, m_modulus))
namespace Calculator {
%}
%target.cpp.constructor_actions {
m_scanner = NULL;
m_last_result = 0.0;
m_modulus = 0.0;
m_should_print_result = true;
}
%target.cpp.destructor_actions {
delete m_scanner;
m_scanner = NULL;
}
%target.cpp.top_of_parse_method_actions %{
m_should_print_result = true;
%}
%target.cpp.bottom_of_implementation_file %{
void Parser::SetInputString (string const &input_string)
{
if (m_scanner != NULL)
delete m_scanner;
m_scanner = new Scanner(input_string);
ResetForNewInput();
}
} // end of namespace Calculator
%}
%target.cpp.token_data_type "double"
%target.cpp.token_data_default "0.0"
%target.cpp.scan_actions {
assert(m_scanner != NULL);
return m_scanner->Scan();
}
%target.cpp.generate_debug_spew_code
%terminal BAD_TOKEN
%terminal NEWLINE
%terminal NUMBER
%terminal RESULT
%terminal HELP MOD
%terminal LOG
%terminal '+' '-' '*' '/' '^' '(' ')' '\\'
%prec.left %default
%prec.left ADDITION
%prec.left MULTIPLICATION
%prec.right UNARY
%prec.right EXPONENTIATION
%default_parse_nonterminal root
%%
%nonterminal root
:
%empty
%target.cpp {
m_should_print_result = false;
return 0.0;
}
|
expression:result
%target.cpp {
result = MODULO(result);
if (m_should_print_result)
m_last_result = result;
return result;
}
|
command
%target.cpp {
m_should_print_result = false;
return 0.0;
}
;
%nonterminal expression
:
expression:lhs '+' expression:rhs %prec ADDITION %target.cpp { return MODULO(lhs + rhs); }
|
expression:lhs '-' expression:rhs %prec ADDITION %target.cpp { return MODULO(lhs - rhs); }
|
expression:lhs '*' expression:rhs %prec MULTIPLICATION %target.cpp { return MODULO(lhs * rhs); }
|
expression:lhs '/' expression:rhs %prec MULTIPLICATION
%target.cpp {
if (rhs == 0.0)
{
cerr << "error: divide by zero" << endl;
m_should_print_result = false;
return 1.0; // arbitrary non-zero return value
}
else
return lhs / rhs;
}
|
'+' expression:ex %prec UNARY %target.cpp { return MODULO(ex); }
|
'-' expression:ex %prec UNARY %target.cpp { return MODULO(-ex); }
|
LOG '(' expression:ex ')' %prec UNARY %target.cpp { return log(ex); }
|
expression:base '^' expression:power %prec EXPONENTIATION
%target.cpp {
if (base == 0.0 && power == 0.0)
{
cerr << "error: taking 0 to the 0 power" << endl;
m_should_print_result = false;
return 1.0; // arbitrary non-zero return value
}
else
return MODULO(pow(base, power));
}
|
'(' expression:ex ')' %target.cpp { return ex; }
|
NUMBER:number %target.cpp { return MODULO(number); }
|
RESULT %target.cpp { return m_last_result; }
;
%nonterminal command
:
'\\' HELP
%target.cpp {
cout << "Operators\n"
" + - * / ^ ( )\n"
"Symbols\n"
" r - The value of the previous result.\n"
" pi - The ratio of a circle's circumference to its diameter.\n"
" e - The base of the natural logarithm.\n"
"Functions\n"
" log - The natural logarithm (base e).\n"
"Commands\n"
" \\help - This help screen.\n"
" \\mod [arg] - Sets the modulus (i.e. modulo arithmetic). Giving no\n"
" argument will print the current modulus. A modulus\n"
" of 0 indicates no result truncation." << endl;
return 0.0;
}
|
'\\' MOD NUMBER:number
%target.cpp {
if (number >= 0.0)
{
m_modulus = number;
cout << "current modulus set to: " << m_modulus << endl;
}
else
cerr << "error: invalid modulus (must be non-negative)" << endl;
return 0.0;
}
|
'\\' MOD
%target.cpp {
cout << "current modulus: " << m_modulus << endl;
return 0.0;
}
;
%nonterminal at_least_zero_newlines
:
at_least_zero_newlines NEWLINE %target.cpp { return 0.0; }
|
%empty %target.cpp { return 0.0; }
;