-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParser.cpp
251 lines (219 loc) · 6.4 KB
/
Parser.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "Parser.h"
#include "Parsenode.h"
#include <stdexcept>
using std::logic_error;
using std::runtime_error;
using std::vector;
using std::string;
using std::move;
using std::make_unique;
Parser::Parser(const std::string& _w) :
input(_w) {
}
ParsenodePtr Parser::parse() {
auto val = value();
if (input.get() == std::char_traits<char>::eof())
return val;
error("Expected end of input"); //throws
}
void Parser::match(char x) { //should never be called unless you KNOW it's going to succeed
auto y = char(input.get()); //!!!Check for EOF? (probably not given precondition) CMH 4/8/18
if (x != y)
throw logic_error(string("Called match(") + x + ") but got " + y + "!");
}
ParsenodePtr Parser::value() {
ParsenodePtr pred = bexpr();
if (nextchar() == '?') {
match('?');
ParsenodePtr trueval = value();
if (nextchar() != ':') error("Expected : in ternary operator");
match(':');
ParsenodePtr falseval = value();
return make_unique<Ternopnode>(move(pred), move(trueval), move(falseval));
}
return pred;
}
ParsenodePtr Parser::morebool(ParsenodePtr lhs) //clean up??!!!
{
char op1 = nextchar();
if (op1 == '<') {
match(op1);
char op2 = nextchar();
if (op2 == '=') {
match(op2);
ParsenodePtr rhs = bexpr();
return make_unique<Binopnode>('l', move(lhs), move(rhs)); // l means <=
}
ParsenodePtr rhs = bexpr();
return make_unique<Binopnode>('<', move(lhs), move(rhs));
}
if (op1 == '>') {
match(op1);
char op2 = nextchar();
if (op2 == '=') {
match(op2);
ParsenodePtr rhs = bexpr();
return make_unique<Binopnode>('g', move(lhs), move(rhs)); // g means >=
}
ParsenodePtr rhs = bexpr();
return make_unique<Binopnode>('>', move(lhs), move(rhs));
}
if (op1 == '=') {
match(op1);
if (nextchar() != '=') error("Expected = (for == operator)");
match('=');
ParsenodePtr rhs = bexpr();
return make_unique<Binopnode>('=', move(lhs), move(rhs));
}
if (op1 == '!') {
match(op1);
if (nextchar() != '=') error("Expected = (for != operator)");
match('=');
ParsenodePtr rhs = bexpr();
return make_unique<Binopnode>('n', move(lhs), move(rhs)); // n means !=
}
return lhs;
}
ParsenodePtr Parser::bexpr() {
ParsenodePtr lhs = bterm();
while (nextchar() == '|') {
match('|');
if (nextchar() != '|') error("Expected | (for || operator)");
match('|');
ParsenodePtr rhs = bterm();
lhs = make_unique<Binopnode>('|', move(lhs), move(rhs));
}
return morebool(move(lhs));
}
ParsenodePtr Parser::bterm() {
ParsenodePtr lhs = expr();
while (nextchar() == '&') {
match('&');
if (nextchar() != '&') error("Expected & (for && operator)");
match('&');
ParsenodePtr rhs = expr();
lhs = make_unique<Binopnode>('&', move(lhs), move(rhs));
}
return lhs;
}
ParsenodePtr Parser::factor() {
char next = nextchar();
if (next == '-' || next == '!' || next == '+') {
match(next);
return make_unique<Unopnode>(next, factor());
}
if (next == '(') {
match('(');
ParsenodePtr val = value();
if (nextchar() != ')') error(") expected");
match(')');
return val;
}
if (isalpha(nextchar()) || nextchar() == '$')
return idexpr();
if (isdigit(nextchar()) || nextchar() == '.')
return make_unique<Numnode>(number());
error("Expected expression");
}
ParsenodePtr Parser::expr() {
ParsenodePtr lhs = term();
while (nextchar() == '+' ||
nextchar() == '-') {
char addop = nextchar();
match(addop);
ParsenodePtr rhs = term();
lhs = make_unique<Binopnode>(addop, move(lhs), move(rhs));
}
return lhs;
}
ParsenodePtr Parser::term() {
ParsenodePtr lhs = factor();
while (nextchar() == '*' ||
nextchar() == '/' ||
nextchar() == '%') {
char mulop = nextchar();
match(mulop);
ParsenodePtr rhs = factor();
lhs = make_unique<Binopnode>(mulop, move(lhs), move(rhs));
}
return lhs;
}
ParsenodePtr Parser::idexpr() {
std::string idname = getidname();
vector<ParsenodePtr > params;
if (nextchar() == '(') {
match('(');
while (true) {
params.push_back(value());
if (nextchar() == ',')
match(',');
else if (nextchar() == ')')
break;
else
error("invalid character in parameter list");
}
match(')');
}
return make_unique<Idnode>(idname, move(params));
}
std::string Parser::getidname() {
std::string name;
while (isalnum(nextchar()) || nextchar() == '$') {
name.push_back(nextchar());
match(nextchar());
}
return name;
}
char Parser::nextchar() {
return char(input.peek());
} //!!!check for eof?
double Parser::number() {
double val;
input >> val;
return val;
}
void Parser::error(const std::string &text) {
throw runtime_error(input.str() + "\n" +
string(input.eof() ? input.str().length() : static_cast<unsigned int>(input.tellg()), '-') + "^\n" + text);
}
ParsenodePtr parse(const std::string &s) {
return Parser(s).parse();
}
ParsenodePtr parse(const Token &t) {
return Parser(t.getdata()).parse();
}
/*
value -> bexpr ternop
ternop -> ? value : value
-> eps
bexpr -> bterm morebterms morebool
morebterms -> || bterm
-> eps
morebool -> boolop bexpr
-> eps
bterm -> expr morebfactors
morebfactors -> && bfactor morebfactors
-> eps
expr -> term moreterms
moreterms -> + term moreterms
-> - term moreterms
-> eps
term -> factor morefactors
morefactors -> * factor morefactors
-> / factor morefactors
-> % factor morefactors
-> eps
factor -> - factor
-> ! factor
-> + factor
-> ( value )
-> idexpr
-> number
idexpr -> id paramlist
paramlist -> ( params )
-> eps
params -> value , params
-> value
*/