-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrammar.c
353 lines (325 loc) · 9.77 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
#include <string.h>
#include "grammar.h"
#include "gstr.h"
#include "list.h"
#include "rxutil.h"
#include "choose.h"
extern int maxStackDepth;
extern int dynamic;
int stackDepth = 0;
GRAMMAR *grammar_binding(GRAMMAR *gram, char *label);
GRAMMAR *grammar_new() {
GRAMMAR *ng = calloc(1, sizeof(GRAMMAR));
ng->parent = NULL;
ng->contents = dict_new();
return ng;
}
/* destroy a grammar */
void grammar_free(GRAMMAR *g) {
dict_freeValues(g->contents, (Destructor) grambit_free);
free(g);
}
static char *getLabel(char *packagedLabel);
void grammar_add(GRAMMAR *g, RULE *r) {
char *label = rule_getLabel(r);
/* if the scope is lexical, add to this grammar */
if (r->scope == LOCAL_SCOPE) {
dict_put(g->contents, label, r);
} else if (r->scope == NON_LOCAL_SCOPE) {
/* otherwise walk up the grammar stack */
GRAMMAR *frame = NULL;
frame = grammar_binding(g, label);
if (!frame) { /* no binding. walk up to top frame */
frame = g;
while (frame->parent) {
frame = frame->parent;
}
}
/* now we're at the right frame. */
dict_put(frame->contents, label, r);
} else if (r->scope == GLOBAL_SCOPE) {
/* walk up to the top frame */
GRAMMAR *frame = g;
while (frame->parent) {
frame = frame->parent;
}
dict_put(frame->contents, label, r);
}
}
void grammar_addAll(GRAMMAR *g, LIST *l) {
int i, len = list_length(l);
for (i = 0; i < len; i++) {
grammar_add(g, (RULE *) list_get(l, i));
}
}
static char *getLabel(char *packagedLabel) {
int i;
char *p;
for (p = packagedLabel, i = 0; *p && *p != '.'; p++, i++)
;
if (*p) { /* found a '.' */
GSTR *g = gstr_new("");
gstr_append(g, packagedLabel + i + 1);
return gstr_detach(g);
} else { /* didn't--this isn't a packaged label */
return NULL;
}
}
/* walk the grammar stack to find the grammar with a binding for the given label */
GRAMMAR *grammar_binding(GRAMMAR *gram, char *label) {
char *noPackage = NULL;
GRAMMAR *frame = NULL;
RULE *r = (RULE *) dict_get(gram->contents, label);
if (r) {
return gram;
}
/* not found. try the parent grammar */
if (gram->parent) {
frame = grammar_binding(gram->parent, label);
}
if (frame) {
return frame;
}
/* not found. try it without the package (if any) */
noPackage = getLabel(label);
if (noPackage) {
return grammar_binding(gram, noPackage);
}
/* OK, there really is no such binding. */
return NULL;
}
RULE *grammar_lookUp(GRAMMAR *gram, char *label) {
char *noPackage = NULL;
RULE *r = NULL;
if (!dynamic) {
gram = grammar_binding(gram, label);
}
if (gram) {
r = (RULE *) dict_get(gram->contents, label);
if (r)
return r;
noPackage = getLabel(label); /* this is redundant but that's no biggie */
if (!noPackage) {
/* this is fatal, because it's inconsistent with the
grammar_binding results in hand */
fprintf(stderr, "binding disappeared for %s\n", label);
exit(-1);
}
r = (RULE *) dict_get(gram->contents, noPackage);
if (r)
return r;
}
return NULL;
}
LIST *expand_choice(GRAMMAR *gram, LIST *c) {
int i;
LIST *l = list_new();
for (i = 0; i < list_length(c); i++) {
GRAMBIT *g = (GRAMBIT *) list_get(c, i);
list_appendAndFree(l, grammar_expand(gram, g));
}
return l;
}
LIST *grammar_expand(GRAMMAR *parentGram, GRAMBIT *g) {
RULE *r;
LIST *result;
int x, j;
GRAMMAR *gram;
stackDepth++;
if (maxStackDepth > 0 && stackDepth > maxStackDepth) {
fprintf(stderr, "warning: stack depth exceeded\n");
result = list_new();
return result;
}
if (dynamic) {
gram = parentGram;
} else {
/* create a new grammar stack frame */
gram = grammar_new();
gram->parent = parentGram;
}
result = list_new();
x = choose_next((g->max_x - g->min_x) + 1) + g->min_x;
for (j = 0; j < x; j++) {
switch (g->type) {
case LITERAL_T:
case TRANS_T:
list_add(result, literal_new(grambit_toString(gram, g)));
break;
case LABEL_T:
#ifdef DEBUG
{
int i;
for(i = 0; i < stackDepth-1; i++)
fputc('>',stdout);
grambit_print(g,stdout);
printf("\n");
}
#endif
r = grammar_lookUp(gram, g->l);
if (r) {
LIST *cs;
LIST *c, *ex;
cs = (LIST *) rule_getChoices(r);
c = (LIST *) list_get(cs, choose_next(list_length(cs)));
ex = expand_choice(gram, c);
#ifdef DEBUG
{
int i;
for(i = 0; i < stackDepth-1; i++)
fputc('<',stdout);
for(i = 0; i < ex->length; i++) {
GRAMBIT *gb = (GRAMBIT *)list_get(ex,i);
grambit_print(gb,stdout);
}
printf("\n");
}
#endif
list_appendAndFree(result, ex);
} else {
/* rule not found; produce the rule name */
fprintf(stderr, "rmutt: warning: rule not found: %s\n", g->l);
list_add(result, literal_new(g->l));
}
break;
case RULE_T:
/* copy the rule to the grammar */
grammar_add(parentGram, grambit_copy(g));
break;
case ASSIGNMENT_T:
/* produce the rhs, and then create a new literal
rule with the result as its only choice, and
add it to the grammar */
if (1) {
RULE *r;
GRAMBIT *lit;
LIST *onlyChoice;
LIST *choices;
char *str;
/* first, produce the rhs */
str = grammar_produce(gram, choice_new(rule_getChoices(g)));
/* now create a single term and single choice */
onlyChoice = list_new();
lit = literal_new(str);
free(str);
list_add(onlyChoice, lit);
choices = list_new();
list_add(choices, onlyChoice);
/* make that the rhs of a new rule */
r = rule_new(rule_getLabel(g), choices, g->scope);
/* add the rule to the grammar */
grammar_add(parentGram, r);
}
break;
case CHOICE_T:
if (1) {
LIST *c;
LIST *cs;
cs = (LIST *) choice_getChoices(g);
c = (LIST *) list_get(cs, choose_next(list_length(cs)));
list_appendAndFree(result, expand_choice(gram, c));
}
break;
case RXSUB_T:
case MAPPING_T:
case RXMATCH_T:
list_add(result, g);
break;
default:
fprintf(stderr, "rmutt: fatal: illegal grambit type %d\n", g->type);
exit(-1);
}
}
stackDepth--;
if (!dynamic) {
/* release the local frame */
grammar_free(gram);
}
return result;
}
char *grammar_produce(GRAMMAR *gram, GRAMBIT *g) {
LIST *result;
int i;
GSTR *str;
result = grammar_expand(gram, g);
str = gstr_new("");
for (i = 0; i < result->length; i++) {
char *s;
GRAMBIT *g = (GRAMBIT *) list_get(result, i);
s = grambit_toString(gram, g);
#ifdef DEBUG
printf("produced %s\n",s);
#endif
gstr_append(str, s);
free(s);
}
list_free(result);
return gstr_detach(str);
}
/**
* a big function, this transforms a grambit into a string,
* whether it be a literal, an operation of some sort such as
* a transformation or call to an external script, or what have you
*/
char *grambit_toString(GRAMMAR *gram, GRAMBIT *g) {
switch (g->type) {
case LITERAL_T:
return strdup(g->l);
case TRANS_T:
if (1) {
char *str;
LIST *tl;
int i;
/* first, expand the source grambit into a string */
str = grammar_produce(gram, g->source);
#ifdef DEBUG
printf("transforming \"%s\"",str);
#endif
/* now expand the (list of) transformation(s) */
tl = grammar_expand(gram, g->trans);
/* now apply them in series */
for (i = 0; i < tl->length; i++) {
char *ns;
ns = transform(gram, str, (GRAMBIT *) list_get(tl, i));
#ifdef DEBUG
printf("%s",ns);
#endif
free(str);
str = ns;
}
#ifdef DEBUG
printf("\n");
#endif
return str;
}
}
return NULL;
}
/* apply a transformation to a string */
char *transform(GRAMMAR *gram, char *str, GRAMBIT *trans) {
switch (trans->type) {
case RXSUB_T:
#ifdef DEBUG
printf(">/%s/%s/",trans->rx_rx,trans->rx_rep);
#endif
return regsub(str, trans->rx_rx, trans->rx_rep, REG_EXTENDED);
case MAPPING_T:
if (!strcmp(str, trans->rx_rx)) {
return (grammar_produce(gram, trans->trans));
} else {
return (strdup(str));
}
case RXMATCH_T: {
char **result = regmatch(str, trans->rx_rx, 1, REG_EXTENDED, 0);
if (result == NULL) {
return strdup(str);
} else {
free(*result);
free(result);
return grammar_produce(gram, trans->trans);
}
}
}
fprintf(stderr, "error: illegal transformation\n");
return NULL;
}