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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/* Generated by re2c */
#include <stdio.h>
#include <stdlib.h> /* malloc, free */
static void *read_file
( const char *fname
, size_t unit
, size_t padding
, size_t *pfsize
)
{
void *buffer = NULL;
size_t fsize = 0;
/* open file */
FILE *f = fopen(fname, "rb");
if(f == NULL) {
goto error;
}
/* get file size */
fseek(f, 0, SEEK_END);
fsize = (size_t) ftell(f) / unit;
fseek(f, 0, SEEK_SET);
/* allocate memory for file and padding */
buffer = malloc(unit * (fsize + padding));
if (buffer == NULL) {
goto error;
}
/* read the whole file in memory */
if (fread(buffer, unit, fsize, f) != fsize) {
goto error;
}
fclose(f);
*pfsize = fsize;
return buffer;
error:
fprintf(stderr, "error: cannot read file '%s'\n", fname);
free(buffer);
if (f != NULL) {
fclose(f);
}
return NULL;
}
enum YYCONDTYPE {
yycr1,
yycr2,
};
#define YYCTYPE unsigned char
#define YYKEYTYPE unsigned char
#define YYPEEK() *cursor
#define YYSKIP() ++cursor
#define YYLESSTHAN(n) (limit - cursor) < n
#define YYFILL(n) { break; }
static int action_line13_r1
( unsigned int i
, const YYKEYTYPE *keys
, const YYCTYPE *start
, const YYCTYPE *token
, const YYCTYPE **cursor
, YYKEYTYPE rule_act
)
{
const long pos = token - start;
const long len_act = *cursor - token;
const long len_exp = (long) keys [3 * i + 1];
const YYKEYTYPE rule_exp = keys [3 * i + 2];
if (rule_exp == 255) {
fprintf
( stderr
, "warning: lex_line13_r1: control flow is undefined for input"
" at position %ld, rerun re2c with '-W'\n"
, pos
);
}
if (len_act == len_exp && rule_act == rule_exp) {
const YYKEYTYPE offset = keys[3 * i];
*cursor = token + offset;
return 0;
} else {
fprintf
( stderr
, "error: lex_line13_r1: at position %ld (iteration %u):\n"
"\texpected: match length %ld, rule %u\n"
"\tactual: match length %ld, rule %u\n"
, pos
, i
, len_exp
, rule_exp
, len_act
, rule_act
);
return 1;
}
}
int lex_line13_r1()
{
const size_t padding = 1; /* YYMAXFILL */
int status = 0;
size_t input_len = 0;
size_t keys_count = 0;
YYCTYPE *input = NULL;
YYKEYTYPE *keys = NULL;
const YYCTYPE *cursor = NULL;
const YYCTYPE *limit = NULL;
const YYCTYPE *token = NULL;
const YYCTYPE *eof = NULL;
unsigned int i = 0;
input = (YYCTYPE *) read_file
("repeat-02.Scgir.c.line13_r1.input"
, sizeof (YYCTYPE)
, padding
, &input_len
);
if (input == NULL) {
status = 1;
goto end;
}
keys = (YYKEYTYPE *) read_file
("repeat-02.Scgir.c.line13_r1.keys"
, 3 * sizeof (YYKEYTYPE)
, 0
, &keys_count
);
if (keys == NULL) {
status = 1;
goto end;
}
cursor = input;
limit = input + input_len + padding;
eof = input + input_len;
for (i = 0; status == 0 && i < keys_count; ++i) {
token = cursor;
YYCTYPE yych;
if (YYLESSTHAN (1)) YYFILL(1);
yych = YYPEEK ();
if (yych <= '2') {
if (yych <= '0') goto yy3;
if (yych <= '1') goto yy6;
goto yy4;
} else {
if (yych <= '`') goto yy3;
if (yych <= 'a') goto yy8;
if (yych <= 'b') goto yy10;
}
yy3:
yy4:
YYSKIP ();
status = action_line13_r1(i, keys, input, token, &cursor, 6);
continue;
yy6:
YYSKIP ();
status = action_line13_r1(i, keys, input, token, &cursor, 5);
continue;
yy8:
YYSKIP ();
status = action_line13_r1(i, keys, input, token, &cursor, 2);
continue;
yy10:
YYSKIP ();
status = action_line13_r1(i, keys, input, token, &cursor, 3);
continue;
}
if (status == 0) {
if (cursor != eof) {
status = 1;
const long pos = token - input;
fprintf(stderr, "error: lex_line13_r1: unused input strings left at position %ld\n", pos);
}
if (i != keys_count) {
status = 1;
fprintf(stderr, "error: lex_line13_r1: unused keys left after %u iterations\n", i);
}
}
end:
free(input);
free(keys);
return status;
}
#undef YYCTYPE
#undef YYKEYTYPE
#undef YYPEEK
#undef YYSKIP
#undef YYLESSTHAN
#undef YYFILL
#define YYCTYPE unsigned char
#define YYKEYTYPE unsigned char
#define YYPEEK() *cursor
#define YYSKIP() ++cursor
#define YYLESSTHAN(n) (limit - cursor) < n
#define YYFILL(n) { break; }
static int action_line13_r2
( unsigned int i
, const YYKEYTYPE *keys
, const YYCTYPE *start
, const YYCTYPE *token
, const YYCTYPE **cursor
, YYKEYTYPE rule_act
)
{
const long pos = token - start;
const long len_act = *cursor - token;
const long len_exp = (long) keys [3 * i + 1];
const YYKEYTYPE rule_exp = keys [3 * i + 2];
if (rule_exp == 255) {
fprintf
( stderr
, "warning: lex_line13_r2: control flow is undefined for input"
" at position %ld, rerun re2c with '-W'\n"
, pos
);
}
if (len_act == len_exp && rule_act == rule_exp) {
const YYKEYTYPE offset = keys[3 * i];
*cursor = token + offset;
return 0;
} else {
fprintf
( stderr
, "error: lex_line13_r2: at position %ld (iteration %u):\n"
"\texpected: match length %ld, rule %u\n"
"\tactual: match length %ld, rule %u\n"
, pos
, i
, len_exp
, rule_exp
, len_act
, rule_act
);
return 1;
}
}
int lex_line13_r2()
{
const size_t padding = 1; /* YYMAXFILL */
int status = 0;
size_t input_len = 0;
size_t keys_count = 0;
YYCTYPE *input = NULL;
YYKEYTYPE *keys = NULL;
const YYCTYPE *cursor = NULL;
const YYCTYPE *limit = NULL;
const YYCTYPE *token = NULL;
const YYCTYPE *eof = NULL;
unsigned int i = 0;
input = (YYCTYPE *) read_file
("repeat-02.Scgir.c.line13_r2.input"
, sizeof (YYCTYPE)
, padding
, &input_len
);
if (input == NULL) {
status = 1;
goto end;
}
keys = (YYKEYTYPE *) read_file
("repeat-02.Scgir.c.line13_r2.keys"
, 3 * sizeof (YYKEYTYPE)
, 0
, &keys_count
);
if (keys == NULL) {
status = 1;
goto end;
}
cursor = input;
limit = input + input_len + padding;
eof = input + input_len;
for (i = 0; status == 0 && i < keys_count; ++i) {
token = cursor;
YYCTYPE yych;
if (YYLESSTHAN (1)) YYFILL(1);
yych = YYPEEK ();
if (yych <= '2') {
if (yych <= '0') goto yy15;
if (yych <= '1') goto yy18;
goto yy16;
} else {
if (yych == 'b') goto yy20;
}
yy15:
yy16:
YYSKIP ();
status = action_line13_r2(i, keys, input, token, &cursor, 6);
continue;
yy18:
YYSKIP ();
status = action_line13_r2(i, keys, input, token, &cursor, 5);
continue;
yy20:
YYSKIP ();
status = action_line13_r2(i, keys, input, token, &cursor, 4);
continue;
}
if (status == 0) {
if (cursor != eof) {
status = 1;
const long pos = token - input;
fprintf(stderr, "error: lex_line13_r2: unused input strings left at position %ld\n", pos);
}
if (i != keys_count) {
status = 1;
fprintf(stderr, "error: lex_line13_r2: unused keys left after %u iterations\n", i);
}
}
end:
free(input);
free(keys);
return status;
}
#undef YYCTYPE
#undef YYKEYTYPE
#undef YYPEEK
#undef YYSKIP
#undef YYLESSTHAN
#undef YYFILL
int main()
{
if(lex_line13_r1() != 0) {
return 1;
}
if(lex_line13_r2() != 0) {
return 1;
}
return 0;
}
21ab
|