-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
296 lines (233 loc) · 6.96 KB
/
Main.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
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
//------------------------------------------------------------------------
#include <iostream>
#include <filesystem>
#include <sstream>
//------------------------------------------------------------------------
#include "Macros.h"
#ifdef SHOW_TIME
#include <chrono>
#endif // SHOW_TIME
//------------------------------------------------------------------------
#include "ConFreeGr.h"
#include "GramErr.h"
//------------------------------------------------------------------------
// Define a new grammar from an input file
//
// Inputs:
// - std::vector<Grammars::ContextFreeGrammar>& grammar:
// A vector containing all the defined grammars
//
// Outputs:
//
void define_new_grammar(std::vector<Grammars::ContextFreeGrammar>& grammars, bool folder) {
// Get input filename
std::cout << "Type 'back' to go back\n";
if (folder)
std::cout << "Folder name: ";
else
std::cout << "Filename: ";
std::string name;
std::cin >> name;
std::cout << '\n';
if (name == "back") return;
// Push the new grammar to the vector
try {
if (folder) {
// For every file in the folder 'name'
for (const auto& infile : std::filesystem::directory_iterator(name)) {
// Check if any grammar is already defined
bool alreadyDefined = false;
for (Grammars::ContextFreeGrammar& grammar : grammars) {
if (grammar == infile.path().string()) {
alreadyDefined = true;
break;
}
}
// If it not defined define it
if (!alreadyDefined)
grammars.push_back(Grammars::ContextFreeGrammar{ infile.path().string() });
}
}
else {
// Check if grammar is already defined
for (Grammars::ContextFreeGrammar& grammar : grammars)
if (name == grammar) return;
// If it not defined define it
grammars.push_back(Grammars::ContextFreeGrammar{ name });
}
}
catch (const Grammars::Errors& e) {
std::cerr << e.what() << "\n\n";
}
catch (const std::runtime_error& e) {
std::cerr << e.what() << "\n\n";
}
}
//------------------------------------------------------------------------
// Delete grammars
// Inputs:
// - std::vector<Grammars::ContextFreeGrammar>& grammars: The defined grammars
//
// Outputs:
//
void delete_grammar(std::vector<Grammars::ContextFreeGrammar>& grammars) {
// Check if any grammar is defined
if (!grammars.size()) {
std::cout << "There is no grammar to delete\n\n";
return;
}
// Show appropriate message
std::cout << "Choose grammars to delete (1-" << grammars.size() << "):\n";
std::cout << "You can delete multiple grammars by typing 'all' to delete all of them\n";
std::cout << "or by typing the numbers of the grammars seperated with space\n";
std::cout << "0: Back\n";
for (unsigned int i = 0; i < grammars.size(); ++i)
std::cout << i + 1 << ": " << (std::string)grammars[i] << '\n';
std::cout << '\n';
// Get answer
std::string answer;
std::cout << ">> ";
// Check answer
if (isalpha(std::cin.peek())) {
// Delete all grammars
std::cin >> answer;
if (answer == "all")
grammars.clear();
}
else if (isdigit(std::cin.peek())) {
// Delete specific grammars
int grammarNum = 0;
std::vector<int> toBeDeletedGrammars;
std::getline(std::cin, answer);
std::stringstream ssanswer{ answer };
while (ssanswer) {
ssanswer >> grammarNum;
if (std::find(toBeDeletedGrammars.begin(), toBeDeletedGrammars.end(), grammarNum) == toBeDeletedGrammars.end() &&
grammarNum > 0 && grammarNum <= grammars.size())
toBeDeletedGrammars.push_back(grammarNum - 1);
if (ssanswer.bad())
break;
}
std::sort(toBeDeletedGrammars.begin(), toBeDeletedGrammars.end(), std::greater<int>());
for (int num : toBeDeletedGrammars)
grammars.erase(grammars.begin() + num);
}
std::cout << '\n';
}
//------------------------------------------------------------------------
// Check words in an already defined grammar
//
// Inputs:
// - const std::vector<Grammars::ContextFreeGrammar>& grammar: Defined grammar
//
// Outputs:
//
void use_grammar(const std::vector<Grammars::ContextFreeGrammar>& grammars) {
// Check if any grammar is defined
if (!grammars.size()) {
std::cout << "There is no grammar to use. You need to define one!\n\n";
return;
}
// Show appropriate message
std::cout << "Choose an grammar to use (1-" << grammars.size() << "):\n";
std::cout << "0: Back\n";
for (unsigned int i = 0; i < grammars.size(); ++i)
std::cout << i + 1 << ": " << (std::string)grammars[i] << '\n';
std::cout << '\n';
// Get answer
int grammarNum = -1;
while (std::cin.bad() || grammarNum < 0 || grammarNum > grammars.size()) {
std::cout << ">> ";
std::cin >> grammarNum;
std::cin.clear();
}
std::cout << '\n';
if (!grammarNum) return;
std::cin.ignore();
// Check words
while (true) {
// Get a new word
std::cout << "Enter a word: ";
std::string word;
std::getline(std::cin, word);
std::string noSpaces;
for (const char ch : word)
if (!isspace(ch))
noSpaces += ch;
#ifdef SHOW_TIME
{
using namespace std::chrono;
auto time = system_clock::now();
#endif // SHOW_TIME
// Check the word and show message
if (grammars[STATICCASTGRAMMAR(grammarNum) - 1].check_word(noSpaces))
std::cout << "\nThe word '" << word << "' can be generated!\n\n";
else
std::cout << "The word '" << word << "' cannot be generated!\n\n";
#ifdef SHOW_TIME
std::cout << "Time: "
<< duration_cast<milliseconds>(system_clock::now() - time).count()
<< " ms\n\n";
}
#endif // SHOW_TIME
// More words?
std::cout << "Do you want to enter another word? (yes,no): ";
// Get answer
std::string answer;
while (answer != "yes" && answer != "no") {
std::cin >> answer;
std::cin.clear();
}
std::cout << '\n';
std::cin.ignore();
if (answer == "no") break;
} // while (true) (check words)
}
//------------------------------------------------------------------------
int main()
try {
std::vector<Grammars::ContextFreeGrammar> grammars;
// Program loop
while (true) {
// Show appropriate message
std::cout << "What do you want to do?\n";
std::cout << "1: Insert new grammar\n";
std::cout << "2: Delete a grammar\n";
std::cout << "3: Use a grammar\n";
std::cout << "4: Exit\n\n";
std::cout << ">> ";
// Get input
std::string input;
std::cin >> input;
std::cin.ignore();
std::cout << '\n';
if (input == "1") {
std::cout << "0: Back\n";
std::cout << "1: Insert a grammar\n";
std::cout << "2: Insert grammars from a folder\n\n";
int answer = -1;
while (std::cin.bad() || answer < 0 || answer > 2) {
std::cout << ">> ";
std::cin >> answer;
std::cin.clear();
}
std::cout << '\n';
if (!answer) continue;
define_new_grammar(grammars, answer - 1);
}
else if (input == "2") {
delete_grammar(grammars);
}
else if (input == "3") {
use_grammar(grammars);
}
else if (input == "4") break;
} // while (true) (program loop)
}
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
}
catch (...) {
std::cerr << "Default exception...\n";
}
//------------------------------------------------------------------------