-
Notifications
You must be signed in to change notification settings - Fork 0
/
thue.cc
307 lines (287 loc) · 7.15 KB
/
thue.cc
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <fstream>
#include <locale>
#include <numeric>
#include <random>
#include <sstream>
#include <utility>
#include <vector>
namespace {
enum class Mode {
kDefault,
kLeft,
kRight
};
enum class RuleType {
kProduction,
kInput,
kOutput,
kNewline
};
struct Rule {
RuleType type;
std::string lhs;
std::string rhs;
};
void Help(void) {
std::cout << "thue [-cdlnr] [-S U] [-s N] FILE" << std::endl
<< "options:" << std::endl
<< " -c leave CR at EOL as is" << std::endl
<< " -d enable the debug print" << std::endl
<< " -l enable the Left mode: priority given to the leftmost match" << std::endl
<< " -n disable the feature to print a newline by rules of form ...::=~" << std::endl
<< " -r enable the Right mode; priority given to the rightmost match" << std::endl
<< " -S U specify U as a random seed" << std::endl
<< " -s N stop after N steps at most" << std::endl
<< " -h, --help print the help" << std::endl
<< " -V, --version print the version" << std::endl;
}
void Version() {
std::cout << "0.0.1" << std::endl;
}
bool ContainsNonWhitespace(const std::string &s) {
return std::find_if(s.begin(), s.end(), [](unsigned char ch) {return !std::isspace(ch);}) != s.end();
}
void TrimCrAtEol(std::string &s) {
auto n = s.length();
if (n > 0 && s[n-1] == '\r')
s.resize(n-1);
}
using Match = std::pair<size_t, const Rule &>;
bool CompareMatch(const Match &x, const Match &y) {
return x.first < y.first;
}
}
int main(int argc, const char *argv[])
{
bool debug = false;
Mode mode = Mode::kDefault;
bool leave_cr = false;
bool newline_convention = true;
int s_seed = -1;
int stop = -1;
if (argc < 2) {
Help();
return EXIT_FAILURE;
}
int i, k = -1;
for (i=1;i<argc;i++) {
if (std::strcmp("-h", argv[i]) == 0 ||
std::strcmp("--help", argv[i]) == 0) {
Help();
return EXIT_SUCCESS;
}
if (std::strcmp("-V", argv[i]) == 0 ||
std::strcmp("--version", argv[i]) == 0) {
Version();
return EXIT_SUCCESS;
}
if (std::strcmp("-c", argv[i]) == 0) {
leave_cr = true;
continue;
}
if (std::strcmp("-d", argv[i]) == 0) {
debug = true;
continue;
}
if (std::strcmp("-l", argv[i]) == 0) {
if (mode == Mode::kRight) {
std::cerr << "!error: choose only one mode" << std::endl;
return EXIT_FAILURE;
}
mode = Mode::kLeft;
continue;
}
if (std::strcmp("-n", argv[i]) == 0) {
newline_convention = false;
continue;
}
if (std::strcmp("-r", argv[i]) == 0) {
if (mode == Mode::kLeft) {
std::cerr << "!error: choose only one mode" << std::endl;
return EXIT_FAILURE;
}
mode = Mode::kRight;
continue;
}
if (std::strcmp("-S", argv[i]) == 0) {
if (++i < argc) {
s_seed = std::stoi(argv[i]);
if (s_seed < 0) {
std::cerr << "!error: give the -S option a non-negative integer" << std::endl;
return EXIT_FAILURE;
}
} else {
std::cerr << "!error: give the -S option a non-negative integer" << std::endl;
return EXIT_FAILURE;
}
continue;
}
if (std::strcmp("-s", argv[i]) == 0) {
if (++i < argc) {
stop = std::stoi(argv[i]);
if (stop < 0) {
std::cerr << "!error: give the -s option a non-negative integer" << std::endl;
return EXIT_FAILURE;
}
} else {
std::cerr << "!error: give the -s option a non-negative integer" << std::endl;
return EXIT_FAILURE;
}
continue;
}
if (k > 0) {
std::cerr << "!error: two or more arguments given" << std::endl;
return EXIT_FAILURE;
}
k = i;
}
if (k <= 0) {
std::cerr << "!error: no code file given" << std::endl;
return EXIT_FAILURE;
}
std::ifstream ifs(argv[k], std::ios::binary);
if (!ifs) {
std::cerr << "!error: failed to open " << argv[k] << std::endl;
return EXIT_FAILURE;
}
std::vector<Rule> rules;
std::string line;
while (std::getline(ifs, line)) {
if (!ContainsNonWhitespace(line))
continue;
auto n = line.find("::=");
if (n == std::string::npos) {
std::cerr << "!error: no production symbol (\"::=\") found" << std::endl;
return EXIT_FAILURE;
}
std::string lhs = line.substr(0, n);
std::string rhs = line.substr(n + 3);
if (!ContainsNonWhitespace(lhs) &&
!ContainsNonWhitespace(rhs))
break;
if (!leave_cr)
TrimCrAtEol(rhs);
Rule r;
if (rhs == ":::") {
r.type = RuleType::kInput;
} else if (rhs.find("~") == 0) {
if (newline_convention && rhs.length() == 1)
r.type = RuleType::kNewline;
else
r.type = RuleType::kOutput;
} else {
r.type = RuleType::kProduction;
}
r.lhs = lhs;
r.rhs = rhs;
rules.push_back(r);
}
if (debug) {
std::cerr << "!rules:" << std::endl;
std::for_each(rules.begin(), rules.end(), [](const Rule &r) {
std::cerr << "|lhs|" << r.lhs << std::endl;
std::cerr << "|rhs|" << r.rhs << std::endl;
});
}
std::vector<std::string> data;
while (std::getline(ifs, line)) {
if (!leave_cr)
TrimCrAtEol(line);
data.push_back(line);
}
ifs.close();
std::string state = std::accumulate(data.begin(), data.end(), std::string(""));
std::mt19937::result_type seed;
if (s_seed >= 0) {
seed = static_cast<std::mt19937::result_type>(s_seed);
} else {
std::random_device rd;
seed = rd();
}
std::mt19937 g(seed);
if (debug) {
std::cerr << "!seed: " << seed << std::endl;
std::cerr << "!state:" << std::endl
<< "|" << state << std::endl;
}
std::vector<Match> pos;
for (int i = 0; stop < 0 || i < stop; ++i) {
pos.clear();
for (const auto &r : rules) {
size_t n = 0;
do {
n = state.find(r.lhs, n);
if (n == std::string::npos)
break;
pos.emplace_back(n++, r);
} while (n < state.length());
}
if (pos.empty())
break;
std::vector<Match>::iterator it;
switch (mode) {
case Mode::kDefault:
{
std::uniform_int_distribution<int> dist(0, pos.size()-1);
int k = dist(g);
it = pos.begin() + k;
}
break;
case Mode::kLeft:
it = std::min_element(pos.begin(), pos.end(), CompareMatch);
break;
case Mode::kRight:
it = std::max_element(pos.begin(), pos.end(), CompareMatch);
break;
}
size_t n = it->first;
const auto &r = it->second;
switch (r.type) {
case RuleType::kProduction:
{
state.replace(n, r.lhs.length(), r.rhs);
if (debug)
std::cerr << "|" << state << std::endl;
}
break;
case RuleType::kInput:
{
if (!std::getline(std::cin, line)) {
std::cerr << "!error: failed to read input" << std::endl;
return EXIT_FAILURE;
}
if (!leave_cr)
TrimCrAtEol(line);
state.replace(n, r.lhs.length(), line);
if (debug)
std::cerr << "|" << state << std::endl;
}
break;
case RuleType::kOutput:
{
state.erase(n, r.lhs.length());
std::cout << r.rhs.substr(1) << std::flush;
if (debug)
std::cerr << "|" << state << std::endl;
}
break;
case RuleType::kNewline:
{
state.erase(n, r.lhs.length());
std::cout << std::endl;
if (debug)
std::cerr << "|" << state << std::endl;
}
break;
}
}
return EXIT_SUCCESS;
}