-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrect_syntax_check.h
185 lines (160 loc) · 5.24 KB
/
correct_syntax_check.h
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
// Copyright 2021 Oganyan Robert
#ifndef OGANYAN_LAMBDA_CALC_CORRECT_SYNTAX_CHECK_H
#define OGANYAN_LAMBDA_CALC_CORRECT_SYNTAX_CHECK_H
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include "terms_lib.h"
using std::cout;
using std::string;
using std::vector;
using std::to_string;
// Check if term is correctly written. If not, returns false and prints the mistake.
bool IsSyntaxCorrect(const string &term) {
// Check for correct brackets
int brackets_count = 0;
for (const char &elem : term) {
if (elem == '(') {
brackets_count++;
} else if (elem == ')') {
brackets_count--;
}
if (brackets_count < 0) {
cout << "Invalid syntax: some ending brackets dont belong to opening brackets" << "\n";
return false;
}
}
if (brackets_count < 0) {
cout << "Invalid syntax: invalid syntax: too much of ending brackets" << "\n";
return false;
}
if (brackets_count > 0) {
cout << "Invalid syntax: invalid syntax: too much of opening brackets" << "\n";
return false;
}
// Check for existing lambdas
bool exist_lib_fun = false;
for (char num : term) {
if ((num >= 'A' && num <= 'Z')) {
exist_lib_fun = true;
break;
}
}
if (!exist_lib_fun) {
for (size_t num = 0; num < term.size(); ++num) {
if (term[num] == '\\') {
break;
} else {
if (num == term.size() - 1) {
cout << "There are no lambdas: nothing to reduce" << "\n";
return false;
}
}
}
}
// Check for correct lambda-usages
for (size_t num = 0; num < term.size() - 1; ++num) {
if (term[num] == '\\' && !isalpha(term[num + 1])) {
cout << "Invalid syntax: there is no variable after lambda" << "\n";
return false;
}
}
if (term[term.size() - 1] == '\\') {
cout << "Invalid syntax: there is no variable after lambda" << "\n";
return false;
}
return true;
}
bool IsSyntaxCorrect(const vector<string> &term_vec) {
for (size_t elem = 0; elem < term_vec.size() - 1; ++elem) {
if (term_vec[elem][0] == '\\') {
bool is_correct = false;
for (size_t j = elem + 1; j < term_vec.size(); ++j) {
if (term_vec[j] != ")") {
is_correct = true;
}
}
if (!is_correct) {
cout << "Invalid syntax: there is no variable after lambda" << "\n";
return false;
}
}
}
return true;
}
// Basically just transforms string to vector
vector<string> ParseToVec(string term) {
vector<string> term_vector;
LibFuncs library;
for (size_t num = 0; num < term.size(); ++num) {
if (term[num] == ' ') {
continue;
}
if ((term[num] == ')') || (term[num] == '(')) {
string element;
element += term[num];
term_vector.push_back(element);
continue;
}
string element;
element.push_back(term[num]);
while ((num + 1 < term.size()) &&
term[num + 1] != ' ' &&
term[num + 1] != '(' &&
term[num + 1] != ')') {
element.push_back(term[num + 1]);
++num;
}
if ((element[0] >= 'A') && ((element[0] <= 'Z'))) {
if (!library.exist("There is no such library function!"));
}
term_vector.push_back(element);
}
return term_vector;
}
// Finds library functions in term (such as True, False, etc) and decompose them
bool ChangeLibFuncsToTerms(vector<string> &term_vec) {
LibFuncs library;
while (true) {
bool need_to_parse = false;
string new_term;
for (size_t elem = 0; elem < term_vec.size(); elem++) {
bool found_capital = false;
string cur_term = term_vec[elem];
for (char letter : cur_term) {
if ((letter >= 'A') && (letter <= 'Z')) {
found_capital = true;
break;
}
}
if (!found_capital) {
new_term += cur_term;
if (elem != term_vec.size() - 1) {
new_term += " ";
}
continue;
}
if (cur_term[0] == '\\') {
if (!library.exist(cur_term.substr(1))) {
cout << "Invalid syntax: capital characters can be only used for library functions" << "\n";
return false;
}
} else {
if (!library.exist(cur_term)) {
cout << "Invalid syntax: capital characters can be only used for library functions" << "\n";
return false;
}
}
new_term += library[cur_term];
if (elem != term_vec.size() - 1) {
new_term += " ";
}
need_to_parse = true;
}
term_vec = ParseToVec(new_term);
if (!need_to_parse) break;
}
return true;
}
#endif //OGANYAN_LAMBDA_CALC_CORRECT_SYNTAX_CHECK_H