-
Notifications
You must be signed in to change notification settings - Fork 125
/
mini-grammar-parser.cpp
285 lines (244 loc) · 8.87 KB
/
mini-grammar-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
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
/*
#
# mini-grammar-parser.cpp - A simple class to parse/generate grammar-based output.
# Copyright (C) 2014 Axel "0vercl0k" Souchet - http://www.twitter.com/0vercl0k
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
*/
#include "mini-grammar-parser.hpp"
#include <fstream>
#include <list>
#include <iostream>
#include <sstream>
MiniGrammarParser::MiniGrammarParser(std::string &path_grammar)
: m_path(path_grammar)
{
}
std::string MiniGrammarParser::generate_(std::shared_ptr<Rule> &rule)
{
std::stringstream ss;
if (rule->type == Rule::RuleChoice)
{
// If we have a RuleChoice, we pick one randomly according to
// the different weights we got
int idx = rand() % rule->total_weights;
for (size_t i = 0; i < rule->children.size(); ++i)
{
idx -= rule->weights.at(i);
if (idx < 0)
{
ss << generate_(rule->children.at(i));
break;
}
}
}
else if (rule->type == Rule::RuleStar)
{
// RuleStar means we can execute the symbol child X times
size_t max = (rand() % rule->max_occurences) + 1;
for (size_t i = 0; i < max; ++i)
ss << generate_(rule->children.at(0));
}
else if (rule->type == Rule::RuleConcatenation)
{
for (auto it = rule->children.begin(); it != rule->children.end(); ++it)
ss << generate_(*it);
}
else if (rule->type == Rule::RuleSymbolName)
// The children are usually RuleSymbolName ; we have to look into
// the symbol store to find the rule
ss << generate_(m_symbols.at(rule->name));
else if (rule->type == Rule::RuleString)
{
// Here we can handle specific strings if we need so
if (rule->name == "\\n")
ss << std::endl;
else if (rule->name == "\\t")
ss << "\t";
else
ss << rule->name;
}
else
throw std::runtime_error("Type unknown");
return ss.str();
}
std::string MiniGrammarParser::generate(const std::string &rulename)
{
return generate_(m_symbols.at(rulename));
}
size_t MiniGrammarParser::extract_integer(std::string &line, size_t &i)
{
// Eat whitespaces
for (; i < line.size() && (line.at(i) == ' ' || line.at(i) == '\t'); ++i);
std::string integer_s;
for (; i < line.size() && line.at(i) != ' '; i++)
integer_s += line.at(i);
size_t integer = atoi(integer_s.c_str());
return integer;
}
bool MiniGrammarParser::extract_symbol_or_string(std::string &line, size_t &i, std::string &value_out)
{
bool is_string = false;
// Eat whitespaces
for (; i < line.size() && (line.at(i) == ' ' || line.at(i) == '\t'); ++i);
if (i >= line.size())
{
value_out = "line ending";
goto end;
}
// Direct string
if (line.at(i) == '\'' || line.at(i) == '"')
{
is_string = true;
unsigned char delim = line.at(i);
// Go over the delimiter obviously :))
i++;
// Retrieve the value
for (; i < line.size() && line.at(i) != delim; ++i)
value_out += line.at(i);
// Go over the end delimiter now
i++;
}
// Symbol name
else
for (; i < line.size() && line.at(i) != ' '; ++i)
value_out += line.at(i);
end:
return is_string;
}
void MiniGrammarParser::parse()
{
std::ifstream input_file;
input_file.open(m_path);
std::string line;
size_t line_n = 0;
while (std::getline(input_file, line))
{
if (line == "" || line.at(0) == ' ' || line.at(0) == '#')
continue;
// First extract the name
std::shared_ptr<Rule> rule = std::make_shared<Rule>();
size_t i = 0;
extract_symbol_or_string(line, i, rule->name);
if (i == line.size())
throw std::runtime_error("Corrupted line");
std::cout << "[" << line_n << "] Symbol: " << rule->name << " ";
// Eat whitespaces
for (; i < line.size() && (line.at(i) == ' ' || line.at(i) == '\t'); ++i);
if (i >= line.size())
throw std::runtime_error("Corrupted line");
// Parse the rule
if (isdigit(line.at(i)))
{
rule->type = Rule::RuleChoice;
rule->total_weights = 0;
std::cout << "[Choice]";
// Save the position, as we need to read several lines
// for that type of rule
std::streampos pos = input_file.tellg();
do
{
if (line == "" || line.at(i) == '#')
continue;
// If this is true, it means this is the starting of a new
// rule definition
if (isalpha(line.at(i)))
break;
// Extract the weight value
size_t weight = extract_integer(line, i);
// Extract the symbol
std::string out;
bool is_string = extract_symbol_or_string(line, i, out);
if (out == "line ending")
break;
std::cout << ", ";
std::shared_ptr<Rule> child = std::make_shared<Rule>();
if (is_string)
child->type = Rule::RuleString;
else
child->type = Rule::RuleSymbolName;
// Add the rule into the symbol store
child->name = out;
rule->children.push_back(child);
rule->weights.push_back(weight);
rule->total_weights += weight;
std::cout << "`" << child->name << "`(" << weight << ")";
// As we are going to read a new line, set the current
// offset to 0
i = 0;
} while (std::getline(input_file, line));
// Don't forget to get back to the previous position
// to continue the parsing
input_file.seekg(pos);
}
else if (line.at(i) == '*')
{
rule->type = Rule::RuleStar;
std::cout << "Star";
i++;
// Extract the maximum number of recursivity
size_t max_occurences = extract_integer(line, i);
std::string out;
bool is_string = extract_symbol_or_string(line, i, out);
std::cout << ", ";
if (is_string)
throw std::runtime_error("Found a string in a RuleStar definition");
// Build the child rule
std::shared_ptr<Rule> child = std::make_shared<Rule>();
child->type = Rule::RuleSymbolName;
child->name = out;
// Build the rule
rule->max_occurences = max_occurences;
rule->children.push_back(child);
std::cout << "`" << child->name << "`(max count: " << max_occurences << ")";
}
else if (isalnum(line.at(i)) || line.at(i) == '"' || line.at(i) == '\'')
{
rule->type = Rule::RuleConcatenation;
std::cout << "Concat";
while (i < line.size())
{
std::string out;
bool is_string = extract_symbol_or_string(line, i, out);
if (out == "line ending")
break;
std::cout << ", ";
std::shared_ptr<Rule> child = std::make_shared<Rule>();
if (is_string)
child->type = Rule::RuleString;
else
child->type = Rule::RuleSymbolName;
child->name = out;
rule->children.push_back(child);
std::cout << "`" << child->name << "`";
}
}
else
throw std::runtime_error("A rule seems to be corrupted");
line_n++;
std::cout << std::endl;
if (rule->type == Rule::RuleStar && rule->children.size() != 1)
throw std::runtime_error("You have a Star rule with more than one child, hu?");
if (rule->type != Rule::RuleString && rule->type != Rule::RuleSymbolName && rule->children.size() == 0)
throw std::runtime_error("Seems you have a rule that does not have a children");
m_symbols.insert(
std::make_pair(
rule->name,
rule
)
);
}
input_file.close();
}