-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConFreeGr.cpp
341 lines (276 loc) · 9.95 KB
/
ConFreeGr.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
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
//----------------------------------------------------------------
#include "ConFreeGr.h"
//----------------------------------------------------------------
namespace Grammars {
//----------------------------------------------------------------
// Read a file and construct the Grammar corresponding the that file
//
// Inputs:
// - std::string infile: the name of the file to read
//
// Outputs:
//
ContextFreeGrammar::ContextFreeGrammar(std::string infile) {
// Open file input stream
std::fstream fin{ infile };
if (!fin) throw Errors(infile, 0, Errors::ErrorType::fileNotFound);
filename = infile;
// Read number of terminal symbols
int nTermSymbols;
fin >> nTermSymbols;
if (fin.bad() || nTermSymbols < 1)
throw Errors(filename, 1, Errors::ErrorType::nTermSymbolsError);
char tempSymbol = ' ';
// Read the terminal symbols and check for duplicates
for (int i = 0; i < nTermSymbols; ++i)
if (!termSymbols.contains(fin.peek())) {
fin >> tempSymbol;
termSymbols.insert(tempSymbol);
}
else
throw Errors(filename, 2, Errors::ErrorType::duplicateTermSymbol);
// Read number of non-terminal symbols
int nNonTermSymbols;
fin >> nNonTermSymbols;
if (fin.bad() || nNonTermSymbols < 1)
throw Errors(filename, 3, Errors::ErrorType::nNonTermSymbolsError);
// Read the non-terminal symbols and check for duplicates
// both in nonTermSymbols vector and in termSymbols vector
for (int i = 0; i < nNonTermSymbols; ++i)
if (!nonTermSymbols.contains(fin.peek()) || !termSymbols.contains(fin.peek())) {
fin >> tempSymbol;
nonTermSymbols.insert(tempSymbol);
}
else
throw Errors(filename, 4, Errors::ErrorType::duplicateNonTermSymbol);
// Read the initial symbol and check if it is defined in the non-terminal symbols
fin >> initialSymbol;
if(fin.bad() || !nonTermSymbols.contains(initialSymbol))
throw Errors(filename, 5, Errors::ErrorType::initialSymbolError);
// Read the number of rules
int nRules;
fin >> nRules;
if (fin.bad() || nRules < 1)
throw Errors(filename, 6, Errors::ErrorType::nRulesError);
// Read rules and check for duplicates
char ruleInput = ' ';
std::string ruleOutput;
maxRuleGenLen = 0;
for (int i = 0; i < nRules; ++i) {
fin >> ruleInput;
if(!nonTermSymbols.contains(ruleInput))
throw Errors(filename, 7 + i, Errors::ErrorType::rulesError);
if (!isspace(fin.peek())) fin.setstate(std::fstream::badbit);
std::getline(fin, ruleOutput);
std::string noSpaces;
for (const char ch : ruleOutput)
if (!isspace(ch))
noSpaces += ch;
ruleOutput = noSpaces;
if (ruleOutput == EMPTYSTRING) ruleOutput = "";
for(const char ch : ruleOutput)
if(!termSymbols.contains(ch) && !nonTermSymbols.contains(ch))
throw Errors(filename, 7 + i, Errors::ErrorType::rulesError);
if (fin.bad() || std::find(ruleMap[ruleInput].begin(), ruleMap[ruleInput].end(),
ruleOutput) != ruleMap[ruleInput].end())
throw Errors(filename, 7 + i, Errors::ErrorType::rulesError);
// Discard rules that won't make a difference
if (std::string{ ruleInput } == ruleOutput) continue;
ruleMap[ruleInput].push_back(ruleOutput);
// Define the max length of the rule outputs
bool onlyNonTerms = true;
for (char ch : ruleOutput)
if (termSymbols.contains(ch)) {
onlyNonTerms = false;
break;
}
if (onlyNonTerms)
if (!maxRuleGenLen)
maxRuleGenLen = ruleOutput.length();
else
if (maxRuleGenLen < ruleOutput.length())
maxRuleGenLen = ruleOutput.length();
}
// Delete all the rules with empty outputs and create new rules from the
// existing ones deleting the non-terminal symbol that outputs empty string
// Example:
// THIS IS A MESS (but it works (atleast for small inputs))
// IT NEEDS MANY IMPROVEMENTS
bool searchAgain = true;
while (searchAgain) {
searchAgain = false;
std::unordered_map<char, std::vector<std::string>> dummyRuleMap;
for (const auto& pairI : ruleMap) {
for (int i = 0; i < pairI.second.size(); ++i) {
if (!pairI.second[i].length()) {
searchAgain = true;
dummyRuleMap.clear();
dummyRuleMap[pairI.first].push_back(std::string{ pairI.first });
dummyRuleMap[pairI.first].push_back("");
{
std::vector<std::string> tempV = pairI.second;
tempV.erase(tempV.begin() + i);
ruleMap[pairI.first] = tempV;
}
for (const auto& pairJ : ruleMap) {
for (int j = 0; j < pairJ.second.size(); ++j)
{
int newOutputsSize = 0;
int lastRuleIndex = -1;
for (int k = 0; k < pairJ.second[j].length(); ++k)
if (pairJ.second[j][k] == pairI.first) {
if (!newOutputsSize)
newOutputsSize = 2;
else
newOutputsSize *= 2;
lastRuleIndex = k;
}
if (lastRuleIndex == -1) continue;
std::vector<std::string> newOutputs(newOutputsSize);
size_t vIndex = 0;
generate_words(pairJ.second[j], 0, dummyRuleMap, lastRuleIndex, newOutputs, vIndex);
std::vector<std::string> tempV = pairJ.second;
for (int k = 0; k < newOutputs.size(); ++k)
if (!newOutputs[k].empty() &&
std::find(tempV.begin(), tempV.end(), newOutputs[k]) == tempV.end())
tempV.push_back(newOutputs[k]);
ruleMap[pairJ.first] = tempV;
}
}
}
}
}
}
#ifdef SHOW_RULES
std::cout << filename << '\n';
for (const auto& pair : ruleMap) {
std::cout << pair.first << ": ";
for (std::string output : pair.second)
std::cout << output << ' ';
std::cout << '\n';
}
std::cout << '\n';
#endif // SHOW_RULES
} // of constructor ContextFreeGrammar
//----------------------------------------------------------------
// Check if a word can be generated from 'this' grammar
//
// Inputs:
// - std::string word: the given word
//
// Outputs:
// - bool true: 'word' was accepted
// - bool false: 'word' was NOT accepted
//
bool ContextFreeGrammar::check_word(std::string word) const {
// Check if any symbol from 'word' is not part of the terminal symbols
for (char ch : word)
if (!termSymbols.contains(ch))
return false;
// Creating the root node for the tree
TreeNode* root = new TreeNode{ nullptr, std::string{initialSymbol}, 0, 1 };
// Adding the node to the frontier
FrontierNode* frontierHead = new FrontierNode{ root };
FrontierNode* frontierTail = frontierHead;
// Creating a vector for the nodes that will be used to delete them
// after searcing for a solution
std::vector<TreeNode*> to_be_deleted_nodes;
// Creating a set for the words added to the tree
std::unordered_set<std::string> wordSet{ root->word };
// A TreeNode-pointer vector to store the children generated in every loop
std::vector<TreeNode*> children;
// A variable to store the TreeNode that the solution will be found
TreeNode* solutionNode = nullptr;
// Loop until frontierHead variable "is empty" or if a solution was found
while (true) {
// Get the next to be expanded leef node
TreeNode* currNode = get_front(&frontierHead, &frontierTail);
to_be_deleted_nodes.push_back(currNode);
if (!currNode) break;
// Check if it holds the solution
if (currNode->word == word) {
solutionNode = currNode;
break;
}
#ifdef SHOW_DETAILS
{
using namespace std::chrono;
auto time = system_clock::now();
std::cout << currNode->word << '\n';
#endif // SHOW_DETAILS
// Generate children nodes and add them to frontier
generate_children(currNode, ruleMap, children, nonTermSymbols);
#ifdef SHOW_DETAILS
std::cout << "Generation time: "
<< duration_cast<milliseconds>(system_clock::now() - time).count()
<< " ms\n";
#endif // SHOW_DETAILS
#ifdef SHOW_DETAILS
time = system_clock::now();
#endif // SHOW_DETAILS
// Prune the node if it is already in the tree
// or if there is no possible way to find a solution throught it
for (int i = 0; i < children.size(); ++i)
if (prune(word, children[i], wordSet, termSymbols, nonTermSymbols, maxRuleGenLen)) {
#ifdef SHOW_PRUNED
std::cout << children[i]->word << '\n';
#endif // SHOW_PRUNED
delete children[i];
children[i] = nullptr;
}
else {
#ifdef SHOW_GENERATED
std::cout << children[i]->word << '\n';
#endif // SHOW_GENERATED
wordSet.insert(children[i]->word);
}
#ifdef SHOW_DETAILS
std::cout << "Prune time: "
<< duration_cast<milliseconds>(system_clock::now() - time).count()
<< " ms\n\n";
}
#endif // SHOW_DETAILS
// Add children to the back of the frontier
for (int i = 0; i < children.size(); ++i)
if (children[i]) {
#ifdef HEURISTIC
add_in_order(&frontierHead, &frontierTail, children[i]);
#else
add_to_back(&frontierHead, &frontierTail, children[i]);
#endif // HEURISTIC
}
children.clear();
} // while(true) (generation loop)
// If a solution was found print it
bool solutionFound = solutionNode;
if(solutionNode)
show_solution(solutionNode, nonTermSymbols);
clear_tree(frontierHead);
for (size_t i = 0; i < to_be_deleted_nodes.size(); ++i)
delete to_be_deleted_nodes[i];
return solutionFound;
} // of function check_word
//----------------------------------------------------------------
// Check if 'filename' is the same as 'this->filename'
//
// Inputs:
// - std::string grammarName: the filename of the grammar
// - std::string filename: the input file to check
//
// Outputs:
// - bool true: The grammar in 'filename' is already defined
// - bool false: The grammar in 'filename' is NOT already defined
//
bool operator==(ContextFreeGrammar& grammar, std::string filename) {
std::string grammarName = grammar;
for (char& ch : grammarName)
if (ch == '\\' || ch == '/')
ch = ' ';
for (char& ch : filename)
if (ch == '\\' || ch == '/')
ch = ' ';
return grammarName == filename;
} // of operator==
//----------------------------------------------------------------
} // of namespace Grammars
//----------------------------------------------------------------