/
cook.c
82 lines (70 loc) · 1.86 KB
/
cook.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arg.h"
#include "cook.h"
#include "error.h"
#include "preen.h"
#include "syntax.h"
/* cook() prepares a grammar for feeding */
/* perhaps we should use the walk() function from sugar.c, but that doesn't
* support the "pre" variable that we use. */
static void cook0(struct s_node *n) {
static char *name;
struct s_node *p;
if (n->type == rule)
name = n->text;
/* Replace
* ... _ foo
* with
* ... _ (foo / !.)
* for any non-empty foo.
*/
if (n->type == cafe) {
struct s_node *t;
//fprintf(stderr, "match at node %ld\n", n->id);
if (!n->next)
fatal3("invalid `$' at the end of rule `", name, "'");
t = s_kid(seq, s_kid(not, s_new(any)));
t = cons(s_kid(seq, n->next), t);
t = s_kid(alt, t);
// pre->next = t;
// free(n);
n->next = t;
}
if (s_has_children(n->type))
for (p = n->first; p; p = p->next)
cook0(p);
}
/* make all rules void, and remove all expr nodes */
static void dexpr(struct s_node *n, struct s_node *par, struct s_node *sib) {
struct s_node *p, *q;
if (n->type == expr) {
assert(par || sib);
if (sib)
sib->next = n->next;
else
par->first = 0;
/* at this point we should free(n) and its children, but we can't do
* that inside the loop */
}
/* XXX in principle, we could remove all bindings *except* those
* used in guards. */
if (s_has_children(n->type))
for (p = 0, q = n->first; q; p = q, q = q->next)
dexpr(q, n, p);
}
void cook(struct s_node *g) {
char *newname;
int l;
dexpr(g, 0, 0);
cook0(g);
l = strlen(g->text) + strlen("_feed") + 1;
newname = realloc(0, l);
if (!newname) nomem();
strcpy(newname, g->text);
strcat(newname, "_feed");
g->text = newname;
check_recursion(g);
}