-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxAnalyzer.cpp
More file actions
514 lines (483 loc) · 21.7 KB
/
SyntaxAnalyzer.cpp
File metadata and controls
514 lines (483 loc) · 21.7 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//
// Created by root on 10/31/17.
//
#include "SyntaxAnalyzer.h"
#include "StringUtils.h"
#include <algorithm>
SyntaxAnalyzer::SyntaxAnalyzer() {
methods.push_back(*(new Method("eulers", { DERIVATIVE, DOUBLE }, TABLE)));
methods.push_back(*(new Method("rungekutta", { DERIVATIVE, DOUBLE }, TABLE)));
methods.push_back(*(new Method("print", { STRING }, VOID)));
methods.push_back(*(new Method("println", { STRING }, VOID)));
}
CheckingResult SyntaxAnalyzer::isSign(const string &text, int startingPosition) {
if (text == "+" || text == "-") return CheckingResult(true);
return CheckingResult(false, startingPosition, "Expected sign (+ or -)");
}
CheckingResult SyntaxAnalyzer::isAssignment(const string &text, int startingPosition) {
if (text == "=") return CheckingResult(true);
return CheckingResult(false, startingPosition, "Expected assignment operator \"=\"");
}
/*
* START - '_ a..z A..Z' -> A - '_ a..z A..Z 0..9' -> A(r)
* |
* - $ -> FINAL
* */
CheckingResult SyntaxAnalyzer::isIdentifier(const string &text, int startingPosition) {
if (find(RESERVED_IDENTIFIERS.begin(), RESERVED_IDENTIFIERS.end(), text) != RESERVED_IDENTIFIERS.end())
return CheckingResult(false, startingPosition, "Identifier name is reserved");
if (find(MATH_CONSTS.begin(), MATH_CONSTS.end(), text) != MATH_CONSTS.end())
return CheckingResult(false, startingPosition, "Identifier name is reserved for math constant");
State state = State::START;
for (unsigned int i = 0; i < text.length(); i++) {
if (state == START) {
if (text.at(i) == '_' || isalpha(text.at(i))) state = A;
else return CheckingResult(false, startingPosition, "Identifier must start with a letter or \"_\"");
} else {
if (text.at(i) != '_' && !isalnum(text.at(i)))
return CheckingResult(false, startingPosition + i, "Identifier must contain only letter, numbers or \"_\"");
}
}
return CheckingResult(true);
}
CheckingResult SyntaxAnalyzer::isExistingVariable(const string &text, int startingPosition, Type type) {
if (identifiers.containsIdentifier(text, type)) return CheckingResult(true);
return CheckingResult(false, startingPosition, "Identifier doesn't exist");
}
/*
* START - '+ -' -> A - '.' -> B - '0..9 -> C - '0..9' -> C(r)
* | | |
* | - 'WS' -> A(r) - $ -> FINAL
* | |
* | - '0..9' -> D
* |
* - '.' -> B
* |
* - '0..9' -> D - '0..9' -> D(r)
* |
* - '.' -> C
* |
* - $ -> FINAL
*/
CheckingResult SyntaxAnalyzer::isNumericConstWithSign(const string &text, int startingPosition) {
State state = START;
for (unsigned int i = 0; i < text.length(); i++) {
switch (state) {
case START:
if (isSign(string(1, text.at(i)), startingPosition).isSuccessful()) state = A;
else if (text.at(i) == '.') state = B;
else if (isdigit(text.at(i))) state = D;
else return CheckingResult(false, startingPosition,
R"(Numeric constant must start with "+", "-", "." or digit)");
break;
case A:
if (text.at(i) == ' ') continue;
if (text.at(i) == '.') state = B;
else if (isdigit(text.at(i))) state = D;
else return CheckingResult(false, startingPosition + i,
R"(Numeric constant must contain only one or zero "." and at least one digit)");
break;
case B:
if (isdigit(text.at(i))) state = C;
else return CheckingResult(false, startingPosition + i,
R"(Numeric constant must contain at least one digit)");
break;
case C:
if (isdigit(text.at(i))) continue;
else return CheckingResult(false, startingPosition + i, "Invalid symbol in numeric constant");
case D:
if (isdigit(text.at(i))) continue;
if (text.at(i) == '.') state = C;
else return CheckingResult(false, startingPosition + i, "Invalid symbol in numeric constant");
break;
}
}
if (state == C || state == D) return CheckingResult(true);
return CheckingResult(false, startingPosition, "Invalid numeric constant");
}
/*
* START - '.' -> A - '0..9 -> B - '0..9' -> B(r)
* | |
* | - $ -> FINAL
* |
* - '0..9' -> C - '0..9' -> C(r)
* |
* - '.' -> B
* |
* - $ -> FINAL
*/
CheckingResult SyntaxAnalyzer::isNumericConst(const string &text, int startingPosition) {
State state = START;
for (unsigned int i = 0; i < text.length(); i++) {
switch (state) {
case START:
if (text.at(i) == '.') state = A;
else if (isdigit(text.at(i))) state = C;
else return CheckingResult(false, startingPosition,
R"(Numeric constant must start with "." or digit)");
break;
case A:
if (isdigit(text.at(i))) state = B;
else return CheckingResult(false, startingPosition + i,
R"(Numeric constant must contain at least one digit)");
break;
case B:
if (isdigit(text.at(i))) continue;
else return CheckingResult(false, startingPosition + i, "Invalid symbol in numeric constant");
case C:
if (isdigit(text.at(i))) continue;
if (text.at(i) == '.') state = B;
else return CheckingResult(false, startingPosition + i, "Invalid symbol in numeric constant");
break;
}
}
if (state == C || state == B) return CheckingResult(true);
return CheckingResult(false, startingPosition, "Invalid numeric constant");
}
CheckingResult SyntaxAnalyzer::isMathFunction(const string &text, int startingPosition) {
if (find(MATH_FUNCTIONS.begin(), MATH_FUNCTIONS.end(), text) != MATH_FUNCTIONS.end()) return CheckingResult(true);
return CheckingResult(false, startingPosition, "Not a math function");
}
CheckingResult SyntaxAnalyzer::isMethodName(const string &text, int startingPosition) {
if (find(METHOD_NAMES.begin(), METHOD_NAMES.end(), text) != METHOD_NAMES.end()) return CheckingResult(true);
return CheckingResult(false, startingPosition, "There is no such method");
}
CheckingResult SyntaxAnalyzer::isMethodReturningType(const string &text, int startingPosition, Type returnType) {
for (const auto &method : methods) {
if (method.name == text && method.returnType == returnType) return CheckingResult(true);
}
return CheckingResult(false, startingPosition, "There is no such method with specified return type");
}
/*
* START - 'METHOD_NAME' -> A - '(' -> B - 'ARGS' -> C - ')' -> D - 'WS' -> D(r)
* | | |
* - 'WS' -> START(r) - 'WS' -> A(r) - '$' -> FINAL
*/
CheckingResult SyntaxAnalyzer::isMethodCall(const string &text, int startingPosition, const SyntaxAnalyzer::Method &method) {
State state = START;
for (int i = 0; i < text.length(); i++) {
switch (state) {
case START:
if (text.at(i) == ' ') continue;
int position = i;
string substring;
while (i < text.length()) {
if (text.at(i) == ' ' || text.at(i) == '(') break;
substring += string(1, text.at(i));
i++;
}
if (i >= text.length() || substring != method.name) return CheckingResult(false, position, "Invalid method name");
if (text.at(i) == ' ') state = A;
else state = B;
break;
case A:
if (text.at(i) == ' ') continue;
if (text.at(i) == '(') state = B;
else return CheckingResult(false, position, "Unexpected symbol in method call");
break;
case B:
int closingBracketIndex = StringUtils::indexOf(text)
string argsString =
else return CheckingResult(false, position, "Unexpected symbol in method call");
break;
}
}
return CheckingResult();
}
CheckingResult SyntaxAnalyzer::isMathOperation(const string &text, int startingPosition) {
if (find(MATH_OPERATIONS.begin(), MATH_OPERATIONS.end(), text) != MATH_OPERATIONS.end()) return CheckingResult(true);
return CheckingResult(false, startingPosition, "There is no such math operation");
}
CheckingResult SyntaxAnalyzer::isMathConst(const string &text, int startingPosition) {
if (find(MATH_CONSTS.begin(), MATH_CONSTS.end(), text) != MATH_CONSTS.end()) return CheckingResult(true);
return CheckingResult(false, startingPosition, "There is no such math constant");
}
/*
* START - 'MATH_CONST NUM_VS_SIGN EX_DOUBLE' -> FINAL
* |
* - 'WS' -> START(r)
* |
* - 'MATH_FUNC' -> A -> 'WS' -> A(r)
* |
* - '(' -> B - 'EXPR' -> C - ')' -> D - 'WS' -> D(r)
* |
* - $ -> FINAL
*/
CheckingResult SyntaxAnalyzer::isOperand(const string &text, int startingPosition) {
if (isMathConst(StringUtils::trim_copy(text), startingPosition).isSuccessful()
|| isNumericConst(StringUtils::trim_copy(text), startingPosition).isSuccessful()
|| isExistingVariable(StringUtils::trim_copy(text), startingPosition, DOUBLE).isSuccessful()) return CheckingResult(true);
State state = START;
unsigned int i = 0;
string substring;
while (i < text.length() && text.at(i) == ' ') i++;
int position = startingPosition + i;
while (i < text.length() && isalnum(text.at(i))) {
substring += string(1, text.at(i));
i++;
}
if (!isMathFunction(substring, position).isSuccessful())
return CheckingResult(false, position, "Invalid operand");
else state = A;
while (i < text.length()) {
switch (state) {
case A:
if (text.at(i) == ' ') break;
if (text.at(i) == '(') state = B;
else return CheckingResult(false, startingPosition + i, "Expected \"(\" after the math function name");
break;
case B: {
substring = "";
int newPosition = startingPosition + i;
int openedParentheses = 0;
while (i < text.length()) {
if (text.at(i) == '(') openedParentheses++;
else if (text.at(i) == ')') {
if (openedParentheses > 0) {
openedParentheses--;
} else {
i--;
break;
}
}
substring += string(1, text.at(i));
i++;
}
if (!isExpression(substring, newPosition).isSuccessful())
return CheckingResult(false, newPosition, "Invalid math function argument");
state = C;
break;
}
case C:
if (text.at(i) == ' ') break;
if (text.at(i) == ')') state = D;
break;
case D:
if (text.at(i) == ' ') break;
return CheckingResult(false, startingPosition + i, "Unexpected symbol after the math function call");
}
i++;
}
if (state == D) return CheckingResult(true);
return CheckingResult(false, startingPosition, "Invalid operand");
}
/*
* START - '(' -> A - 'EXPR' -> C - ')' -> D - '$' -> FINAL
* | |
* - 'WS -> START(r) - 'WS' -> D(r)
* | |
* - 'OPERAND' -> D - 'MATH_OP' -> B - 'WS' -> B(r)
* | |
* - '+ -' -> B - 'OPERAND' -> D
* |
* - '(' -> A
*/
CheckingResult SyntaxAnalyzer::isExpression(const string &text, int startingPosition) {
State state = START;
for (unsigned int i = 0; i < text.length(); i++) {
switch (state) {
case START:
if (text.at(i) == ' ') continue;
if (text.at(i) == '(') state = A;
else if (text.at(i) == '+' || text.at(i) == '-') state = B;
else {
string substring;
int openedParentheses = 0;
while (i < text.length()) {
if (text.at(i) == '(') openedParentheses++;
else if (text.at(i) == ')') openedParentheses--;
else if (openedParentheses == 0 && isMathOperation(string(1, text.at(i)), i).isSuccessful()) {
i--;
break;
}
substring += string(1, text.at(i));
i++;
}
if (!isOperand(substring, startingPosition).isSuccessful())
return CheckingResult(false, startingPosition, "Invalid expression");
state = D;
}
break;
case A: {
int newPosition = startingPosition + i;
string substring;
int openedParentheses = 0;
while (i < text.length()) {
if (text.at(i) == '(')
openedParentheses++;
else if (text.at(i) == ')')
if (openedParentheses > 0) {
openedParentheses--;
} else {
i--;
break;
}
substring += string(1, text.at(i));
i++;
}
if (!isExpression(substring, newPosition).isSuccessful())
return CheckingResult(false, newPosition, "Invalid expression");
state = C;
break;
}
case B:
if (text.at(i) == ' ') continue;
if (text.at(i) == '(') state = A;
else {
string substring;
int openedParentheses = 0;
while (i < text.length()) {
if (text.at(i) == '(') openedParentheses++;
else if (text.at(i) == ')') openedParentheses--;
else if (openedParentheses == 0 && isMathOperation(string(1, text.at(i)), startingPosition + i).isSuccessful()) {
i--;
break;
}
substring += string(1, text.at(i));
i++;
}
if (!isOperand(substring, startingPosition + i).isSuccessful())
return CheckingResult(false, startingPosition + i, "Error in expression");
state = D;
}
break;
case C:
if (text.at(i) == ')') state = D;
else return CheckingResult(false, i, "Expected \")\" in expression");
break;
case D:
if (text.at(i) == ' ') continue;
if (isMathOperation(string(1, text.at(i)), startingPosition + i).isSuccessful()) state = B;
else return CheckingResult(false, startingPosition + i, "Error in expression");
break;
}
}
if (state == D) return CheckingResult(true);
return CheckingResult(false, startingPosition, "Invalid expression");
}
CheckingResult SyntaxAnalyzer::isFirstDerivativeX(const string &text, int startingPosition) {
const string key = "x";
const string t = "t";
identifiers.addIdentifier(key, DOUBLE);
identifiers.addIdentifier(t, DOUBLE);
auto result = isExpression(text, startingPosition);
identifiers.removeIdentifier(key);
identifiers.removeIdentifier(t);
return result;
}
CheckingResult SyntaxAnalyzer::isFirstDerivativeY(const string &text, int startingPosition) {
const string key = "y";
const string t = "t";
identifiers.addIdentifier(key, DOUBLE);
identifiers.addIdentifier(t, DOUBLE);
auto result = isExpression(text, startingPosition);
identifiers.removeIdentifier(key);
identifiers.removeIdentifier(t);
return result;
}
CheckingResult SyntaxAnalyzer::isFirstDerivativeZ(const string &text, int startingPosition) {
const string key = "z";
const string t = "t";
identifiers.addIdentifier(key, DOUBLE);
identifiers.addIdentifier(t, DOUBLE);
auto result = isExpression(text, startingPosition);
identifiers.removeIdentifier(key);
identifiers.removeIdentifier(t);
return result;
}
/*
* START - 'eulers rungekutta' -> A - 'WS' -> A(r)
* | |
* - 'WS' -> START(r) - '(' -> B - 'dx dy dz' -> C - ',' -> D - 'EXPR' -> E - ')' -> F - 'WS' -> F(r)
* | | |
* - 'WS' -> B(r) - 'WS' -> C(r) - '$' -> FINAL
*/
CheckingResult SyntaxAnalyzer::isNumIntegrationCall(const string &text, int startingPosition) {
State state = START;
for (unsigned int i = 0; i < text.length(); i++) {
switch (state) {
case START:
if (text.at(i) == ' ') continue;
string substring;
int position = i;
while (i < text.length()) {
if (text.at(i) == ' ') {
if (isMethodReturningType(substring, startingPosition + position, TABLE).isSuccessful()) {
state = A;
continue;
} else {
return CheckingResult(false, startingPosition + position, "There is no such numeric integration method");
}
}
if (i >= text.length()) return CheckingResult(false, startingPosition, "");
i++;
}
break;
}
}
return CheckingResult();
}
SyntaxAnalyzer::Method SyntaxAnalyzer::recognizeMethod(const string &text) {
int index = StringUtils::indexOf(text, "(");
if (index == -1) return NULL;
string substring = text.substr(0, index);
StringUtils::trim(substring);
for (const auto &method : methods) {
if (method.name == substring) return method;
}
return NULL;
}
VariablesContainer &SyntaxAnalyzer::getIdentifiers() {
return identifiers;
}
void SyntaxAnalyzer::setIdentifiers(const VariablesContainer &identifiers) {
SyntaxAnalyzer::identifiers = identifiers;
}
/*
* START - 'WS' -> START(r)
* |
* - 'STR_ID' -> A - 'WS' -> A(r)
* | |
* - '"' -> B - '+' -> C - 'WS' -> C(r)
* | | |
* - '$' -> FINAL | - 'STR_ID' -> A
* | |
* | - '"' -> B - '"' -> A
* | |
* - '$' -> FINAL - 'ANY OTHER' -> B(r)
*/
CheckingResult SyntaxAnalyzer::isStringExpression(const string &text, int startingPosition) {
State state = START;
for (unsigned int i = 0; i < text.length(); i++) {
switch (state) {
case START:
if (text.at(i) == ' ') continue;
if (text.at(i) == '"') state = B;
else {
string substring;
int position = i;
for (i; i < text.length(); i++) {
if (text.at(i) == ' ' || text.at(i) == '+') break;
substring += string(1, text.at(i));
}
if (isExistingVariable(substring, startingPosition + position, STRING).isSuccessful()) state = A;
else return CheckingResult(false, startingPosition + position, "String variable with such name doesn't exist");
}
break;
case A:
if (text.at(i) == ' ') continue;
if (text.at(i) == '+') state = C;
else return CheckingResult(false, startingPosition + i, "Unexpected symbol in string expression");
break;
case B:
if (text.at(i) == '"' || text.at(i - 1) != '\\') state = A;
else return CheckingResult(false, startingPosition + i, "Unexpected symbol in string expression");
break;
}
}
return CheckingResult();
}
SyntaxAnalyzer::Method::Method() = default;
SyntaxAnalyzer::Method::Method(const string &name, const list<Type> &arguments,
Type returnType) : name(name), arguments(arguments),
returnType(returnType) {}