-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.c
495 lines (454 loc) · 12.7 KB
/
grammar.c
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include "grammar.h"
#include "grammar_i.h"
#include "nullable.h"
#include "strhash.h"
#include "print.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/*
* Constructor for struct grammar *
*/
struct grammar *
grammar_alloc(void)
{
struct grammar *ret = (struct grammar *) calloc(1, sizeof(struct grammar));
if (! ret)
return NULL;
ret->hash = strhash_alloc();
if (! ret->hash) {
free(ret);
return NULL;
}
return ret;
}
static void
destroy_terminal(struct terminal *t)
{
(void) t;
}
static void
destroy_rule(struct rule *rule)
{
if (rule->host_code)
free((void *)rule->host_code);
if (rule->ls_label)
free((void *)rule->ls_label);
for (unsigned i = 0; i < rule->length; ++i) {
if (rule->rs[i].label)
free((void *)rule->rs[i].label);
}
free(rule);
}
static void
destroy_nonterminal(struct nonterminal *nt)
{
struct rule *r = (struct rule *) nt->rules;
while (r) {
struct rule *n = (struct rule *) r->next;
destroy_rule(r);
r = n;
}
}
static void
destroy_symbol(void *ptr)
{
struct symbol *sym = (struct symbol *) ptr;
if (! sym)
return;
switch (sym->type) {
case UNKNOWN:
break;
case TERMINAL:
destroy_terminal(&sym->t);
break;
case NONTERMINAL:
case START_NONTERMINAL:
destroy_nonterminal(&sym->nt);
break;
}
if (sym->host_type)
free((void *)sym->host_type);
if (sym->destructor_code)
free((void *)sym->destructor_code);
free(sym);
}
/*
* Destructor for struct grammar *
*/
void
grammar_free(struct grammar *grammar)
{
if (grammar->machine_name)
free((void *)grammar->machine_name);
if (grammar->terminal_host_type)
free((void *)grammar->terminal_host_type);
if (grammar->terminal_destructor_code)
free((void *)grammar->terminal_destructor_code);
if (grammar->host_code)
free((void *)grammar->host_code);
if (grammar->extra_argument)
free((void *)grammar->extra_argument);
strhash_free(grammar->hash, destroy_symbol);
free(grammar);
}
static void
commit_symbol(struct grammar *grammar, struct symbol *sym)
{
if (! grammar->symlist.first) {
sym->next = NULL;
grammar->symlist.first = sym;
grammar->symlist.last = sym;
return;
}
switch (sym->type) {
case UNKNOWN:
break;
case TERMINAL:
sym->next = grammar->symlist.first;
grammar->symlist.first = sym;
break;
case NONTERMINAL:
case START_NONTERMINAL:
sym->next = grammar->symlist.last->next;
grammar->symlist.last->next = sym;
grammar->symlist.last = sym;
break;
}
}
struct symbol *
grammar_symbol(struct grammar *grammar, const char *name, enum symbol_type type)
{
struct symbol **s = (struct symbol **) strhash_find(grammar->hash, name);
if (! *s) {
*s = calloc(1, sizeof(struct symbol));
if (! *s)
abort();
(*s)->type = (enum symbol_typex)type;
(*s)->name = strhash_key((void **)s);
if (type != SYMBOL_UNKNOWN)
commit_symbol(grammar, *s);
} else if ((*s)->type == UNKNOWN) {
if (type != SYMBOL_UNKNOWN) {
(*s)->type = type;
commit_symbol(grammar, *s);
}
} else if ((type != SYMBOL_UNKNOWN) && ((*s)->type != (enum symbol_typex)type)) {
print("error: reassigning type for symbol %s\n", name);
return NULL;
}
return *s;
}
/*
* Assign name to the grammar
*/
void
grammar_name(struct grammar *grammar, const char *name)
{
if (grammar->machine_name) {
print("error: grammar already has a name\n");
return;
}
grammar->machine_name = strdup(name);
}
/*
* Add raw host code to the grammar
*/
void
grammar_add_host_code(struct grammar *grammar, const char *host_code)
{
size_t len = strlen(host_code);
if (grammar->host_code)
len += strlen(grammar->host_code);
char *str = calloc(1, len + 1);
if (grammar->host_code)
strcpy(str, grammar->host_code);
strcat(str, host_code);
if (grammar->host_code)
free((void *)grammar->host_code);
grammar->host_code = str;
}
void
grammar_set_extra_argument(struct grammar *grammar, const char *extra_argument)
{
if (grammar->extra_argument) {
print("error: grammar already has extra_argument\n");
return;
}
grammar->extra_argument = strdup(extra_argument);
}
/*
* Assign host type to terminals.
*/
void
grammar_assign_terminal_type(struct grammar *grammar, const char *type)
{
if (grammar->terminal_host_type) {
print("error: reassigning terminal type\n");
return;
}
grammar->terminal_host_type = strdup(type);
}
/*
* Assign host type to a symbol.
*/
void
grammar_assign_type(struct grammar *grammar, const char *name, const char *type)
{
struct symbol *s = grammar_symbol(grammar, name, SYMBOL_NONTERMINAL);
if (! s)
return;
if (s->host_type) {
print("error: reassiginig symbol type for %s\n", name);
return;
}
s->host_type = strdup(type);
}
/*
* Assign host type to terminals.
*/
void
grammar_assign_terminal_destructor(struct grammar *grammar, const char *destructor_code)
{
if (grammar->terminal_destructor_code) {
print("error: reassigning terminal destructor code\n");
return;
}
grammar->terminal_destructor_code = strdup(destructor_code);
}
/*
* Assign destructor code to a symbol.
*/
void
grammar_assign_destructor(struct grammar *grammar, const char *name, const char *destructor_code)
{
struct symbol *s = grammar_symbol(grammar, name, SYMBOL_NONTERMINAL);
if (! s)
return;
if (s->destructor_code) {
print("error: reassiginig symbol destructor for %s\n", name);
return;
}
s->destructor_code = strdup(destructor_code);
}
void
grammar_deduce_type(struct grammar *grammar, const char *ls_name, const char *rs_name)
{
struct symbol *ls = grammar_symbol(grammar, ls_name, SYMBOL_UNKNOWN);
struct symbol *rs = grammar_symbol(grammar, rs_name, SYMBOL_UNKNOWN);
if (! ls->host_type) {
if (rs->host_type)
ls->host_type = strdup(rs->host_type);
} else {
if (! rs->host_type || strcmp(ls->host_type, rs->host_type))
print("error: overriding type for %s\n", ls_name);
}
}
/*
* Add a new nonterminal rule to the grammar while building it.
*/
void
grammar_nonterminal(struct grammar *grammar,
const struct grammar_element *ls,
unsigned rsn, const struct grammar_element rs[],
const char *host_code)
{
struct symbol *s = grammar_symbol(grammar, ls->name, SYMBOL_NONTERMINAL);
if (! s) {
print("error: symbol `%s' is not a nonterminal\n", ls->name);
return;
}
struct rule *rule = calloc(1, sizeof_struct_rule(rsn));
if (! rule)
abort();
rule->next = s->nt.rules;
rule->sym = s;
s->nt.rules = rule;
rule->id = ++(grammar->n_rules);
if (ls->label) {
const char *lbl = strdup(ls->label);
if (! lbl)
abort();
rule->ls_label = lbl;
}
if (host_code) {
const char *hc = strdup(host_code);
if (! hc)
abort();
rule->host_code = hc;
}
rule->length = rsn;
for (unsigned i = 0; i < rsn; ++i) {
rule->rs[i].sym = grammar_symbol(grammar, rs[i].name, SYMBOL_UNKNOWN);
if (rs[i].label) {
const char *lbl = strdup(rs[i].label);
if (! lbl)
abort();
rule->rs[i].label = lbl;
}
}
}
/*
* Set grammar start symbol
*/
void
grammar_start_symbol(struct grammar *grammar, const char *start)
{
grammar->start = grammar_symbol(grammar, start, SYMBOL_NONTERMINAL);
}
static void
resolve_symbols(struct grammar *grammar)
{
for (struct symbol * sym = grammar->symlist.first; sym; sym = sym->next) {
for (const struct rule * rule = sym->nt.rules; rule; rule = rule->next) {
for (unsigned i = 0; i < rule->length; ++i) {
struct symbol * s = rule->rs[i].sym;
if (s->type == UNKNOWN) {
s->type = TERMINAL;
commit_symbol(grammar, s);
}
++(s->use_count);
}
}
}
}
static void
add_sentinel_rule(struct grammar *grammar)
{
struct symbol ** eof = (struct symbol **) strhash_find(grammar->hash, "%eof");
assert(! *eof);
*eof = calloc(1, sizeof(struct symbol));
if (! *eof)
abort();
(*eof)->type = TERMINAL;
(*eof)->name = strhash_key((void **) eof);
commit_symbol(grammar, *eof);
struct symbol ** start = (struct symbol **) strhash_find(grammar->hash, "%start");
assert(! *start);
*start = calloc(1, sizeof(struct symbol));
if (! *start)
abort();
(*start)->type = START_NONTERMINAL;
(*start)->name = strhash_key((void **) start);
commit_symbol(grammar, *start);
struct rule * rule = calloc(1, sizeof_struct_rule(2));
if (! rule)
abort();
(*start)->nt.rules = rule;
++(grammar->n_rules);
rule->sym = *start;
rule->id = 0;
rule->length = 2;
rule->rs[0].sym = (struct symbol *) grammar->start;
rule->rs[1].sym = *eof;
for (unsigned i = 0; i < rule->length; ++i)
++(((struct symbol*)rule->rs[i].sym)->use_count);
grammar->start = *start;
}
static const char *
strtype(const struct symbol * sym)
{
switch (sym->type) {
case UNKNOWN:
return "!undefined!";
case TERMINAL:
return "terminal";
case NONTERMINAL:
case START_NONTERMINAL:
return "nonterminal";
}
return "BUG";
}
static void
count_symbols(struct grammar *grammar)
{
unsigned id = 0;
for (struct symbol * sym = grammar->symlist.first; sym; sym = sym->next, ++id) {
sym->id = id;
switch (sym->type) {
case UNKNOWN:
// FIXME this is actually impossible, UNKNOWN are not listed here
print("warning: symbol `%s' not defined in grammar\n", sym->name);
break;
case TERMINAL:
++(grammar->n_terminals);
break;
case NONTERMINAL:
case START_NONTERMINAL:
++(grammar->n_nonterminals);
break;
}
}
}
static void
determine_start(struct grammar * grammar)
{
for (struct symbol * sym = grammar->symlist.first; sym; sym = sym->next) {
if (! sym->use_count) {
if (! grammar->start && sym->type == NONTERMINAL) {
print("note: using `%s' as start symbol\n", sym->name);
grammar->start = sym;
} else if (sym != grammar->start && sym->type != UNKNOWN) {
print("warning: unused %s symbol `%s'\n", strtype(sym), sym->name);
}
}
}
}
static void
dump_grammar(const struct grammar *grammar)
{
print("-- Grammar:\n");
for (struct symbol * sym = grammar->symlist.first; sym; sym = sym->next) {
switch (sym->type) {
case UNKNOWN:
break;
case TERMINAL:
print("%%terminal %s.\n", sym->name);
break;
case NONTERMINAL:
case START_NONTERMINAL:
for (const struct rule * r = sym->nt.rules; r; r = r->next) {
print("// %i\n%s ::=", r->id, sym->name);
for (unsigned i = 0; i < r->length; ++i)
print(" %s", r->rs[i].sym->name);
print(".");
if (r->host_code)
print(" {%s}", r->host_code);
print("\n");
}
break;
}
}
print("--\n");
}
static void
dump_symbols(const struct grammar *grammar)
{
print("-- Terminals (%u):\n", grammar->n_terminals);
for (struct symbol * sym = grammar->symlist.first; sym; sym = sym->next) {
if (sym->id == grammar->n_terminals) {
print("-- Nonterminals (%u):\n", grammar->n_nonterminals);
}
print(" %s = %u\n", sym->name, sym->id);
}
print("--\n");
}
/*
* Finish building grammar
*/
void
grammar_complete(struct grammar *grammar)
{
resolve_symbols(grammar);
determine_start(grammar);
add_sentinel_rule(grammar);
count_symbols(grammar);
/* At this point we have complete symbol graph */
if (print_opt(P_GRAMMAR))
dump_grammar(grammar);
if (print_opt(P_SYMBOLS))
dump_symbols(grammar);
nullable_find(grammar);
if (print_opt(P_NULLABLE))
nullable_dump(grammar);
}