10 static char rcsid[] = "lburg.c - faked rcsid";
12 static char *prefix = "";
14 static int ntnumber = 0;
15 static Nonterm start = 0;
22 } *memlist; /* list of allocated blocks */
24 static char *stringf(char *fmt, ...);
25 static void print(char *fmt, ...);
26 static void ckreach(Nonterm p);
27 static void emitclosure(Nonterm nts);
28 static void emitcost(Tree t, char *v);
29 static void emitdefs(Nonterm nts, int ntnumber);
30 static void emitheader(void);
31 static void emitkids(Rule rules, int nrules);
32 static void emitnts(Rule rules, int nrules);
33 static void emitrecalc(char *pre, Term root, Term kid);
34 static void emitrecord(char *pre, Rule r, char *c, int cost);
35 static void emitrule(Nonterm nts);
36 static void emitlabel(Term terms, Nonterm start, int ntnumber);
37 static void emitstring(Rule rules);
38 static void emitstruct(Nonterm nts, int ntnumber);
39 static void emittest(Tree t, char *v, char *suffix);
41 int main(int argc, char *argv[]) {
45 for (i = 1; i < argc; i++)
46 if (strcmp(argv[i], "-T") == 0)
48 else if (strncmp(argv[i], "-p", 2) == 0 && argv[i][2])
50 else if (strncmp(argv[i], "-p", 2) == 0 && i + 1 < argc)
52 else if (*argv[i] == '-' && argv[i][1]) {
53 yyerror("usage: %s [-T | -p prefix]... [ [ input ] output ] \n",
56 } else if (infp == NULL) {
57 if (strcmp(argv[i], "-") == 0)
59 else if ((infp = fopen(argv[i], "r")) == NULL) {
60 yyerror("%s: can't read `%s'\n", argv[0], argv[i]);
63 } else if (outfp == NULL) {
64 if (strcmp(argv[i], "-") == 0)
66 if ((outfp = fopen(argv[i], "w")) == NULL) {
67 yyerror("%s: can't write `%s'\n", argv[0], argv[i]);
78 for (p = nts; p; p = p->link) {
80 yyerror("undefined nonterminal `%s'\n", p->name);
82 yyerror("can't reach nonterminal `%s'\n", p->name);
85 emitdefs(nts, ntnumber);
86 emitstruct(nts, ntnumber);
87 emitnts(rules, nrules);
92 emitlabel(terms, start, ntnumber);
93 emitkids(rules, nrules);
95 while ((c = getc(infp)) != EOF)
97 while (memlist) { /* for purify */
98 struct block *q = memlist->link;
105 /* alloc - allocate nbytes or issue fatal error */
106 void *alloc(int nbytes) {
107 struct block *p = calloc(1, sizeof *p + nbytes);
110 yyerror("out of memory\n");
118 /* stringf - format and save a string */
119 static char *stringf(char *fmt, ...) {
124 vsprintf(buf, fmt, ap);
126 return strcpy(alloc(strlen(buf) + 1), buf);
137 #define HASHSIZE (sizeof table/sizeof table[0])
139 /* hash - return hash number for str */
140 static unsigned hash(char *str) {
148 /* lookup - lookup symbol name */
149 static void *lookup(char *name) {
150 struct entry *p = table[hash(name)%HASHSIZE];
152 for ( ; p; p = p->link)
153 if (strcmp(name, p->sym.name) == 0)
158 /* install - install symbol name */
159 static void *install(char *name) {
160 struct entry *p = alloc(sizeof *p);
161 int i = hash(name)%HASHSIZE;
169 /* nonterm - create a new terminal id, if necessary */
170 Nonterm nonterm(char *id) {
171 Nonterm p = lookup(id), *q = &nts;
173 if (p && p->kind == NONTERM)
175 if (p && p->kind == TERM)
176 yyerror("`%s' is a terminal\n", id);
179 p->number = ++ntnumber;
182 while (*q && (*q)->number < p->number)
184 assert(*q == 0 || (*q)->number != p->number);
190 /* term - create a new terminal id with external symbol number esn */
191 Term term(char *id, int esn) {
192 Term p = lookup(id), *q = &terms;
195 yyerror("redefinition of terminal `%s'\n", id);
201 while (*q && (*q)->esn < p->esn)
203 if (*q && (*q)->esn == p->esn)
204 yyerror("duplicate external symbol number `%s=%d'\n",
211 /* tree - create & initialize a tree node with the given fields */
212 Tree tree(char *id, Tree left, Tree right) {
213 Tree t = alloc(sizeof *t);
221 if (p == NULL && arity > 0) {
222 yyerror("undefined terminal `%s'\n", id);
224 } else if (p == NULL && arity == 0)
225 p = (Term)nonterm(id);
226 else if (p && p->kind == NONTERM && arity > 0) {
227 yyerror("`%s' is a nonterminal\n", id);
230 if (p->kind == TERM && p->arity == -1)
232 if (p->kind == TERM && arity != p->arity)
233 yyerror("inconsistent arity for terminal `%s'\n", id);
235 t->nterms = p->kind == TERM;
236 if ((t->left = left) != NULL)
237 t->nterms += left->nterms;
238 if ((t->right = right) != NULL)
239 t->nterms += right->nterms;
243 /* rule - create & initialize a rule with the given fields */
244 Rule rule(char *id, Tree pattern, char *template, char *code) {
245 Rule r = alloc(sizeof *r), *q;
246 Term p = pattern->op;
249 r->lhs = nonterm(id);
250 r->packed = ++r->lhs->lhscount;
251 for (q = &r->lhs->rules; *q; q = &(*q)->decode)
254 r->pattern = pattern;
256 r->template = template;
258 r->cost = strtol(code, &end, 10);
261 r->code = stringf("(%s)", code);
263 if (p->kind == TERM) {
264 for (q = &p->rules; *q; q = &(*q)->next)
267 } else if (pattern->left == NULL && pattern->right == NULL) {
268 Nonterm p = pattern->op;
272 yyerror("illegal nonconstant cost `%s'\n", code);
274 for (q = &rules; *q; q = &(*q)->link)
281 /* print - formatted output */
282 static void print(char *fmt, ...) {
289 case 'd': fprintf(outfp, "%d", va_arg(ap, int)); break;
290 case 's': fputs(va_arg(ap, char *), outfp); break;
291 case 'P': fprintf(outfp, "%s_", prefix); break;
293 Tree t = va_arg(ap, Tree);
295 if (t->left && t->right)
296 print("(%T,%T)", t->left, t->right);
298 print("(%T)", t->left);
302 Rule r = va_arg(ap, Rule);
303 print("%S: %T", r->lhs, r->pattern);
306 case 'S': fputs(va_arg(ap, Term)->name, outfp); break;
307 case '1': case '2': case '3': case '4': case '5': {
313 default: putc(*fmt, outfp); break;
320 /* reach - mark all nonterminals in tree t as reachable */
321 static void reach(Tree t) {
324 if (p->kind == NONTERM)
333 /* ckreach - mark all nonterminals reachable from p */
334 static void ckreach(Nonterm p) {
338 for (r = p->rules; r; r = r->decode)
342 /* emitcase - emit one case in function state */
343 static void emitcase(Term p, int ntnumber) {
346 print("%1case %d: /* %S */\n", p->esn, p);
351 print("%2%Plabel(LEFT_CHILD(a));\n");
354 print("%2%Plabel(LEFT_CHILD(a));\n");
355 print("%2%Plabel(RIGHT_CHILD(a));\n");
359 for (r = p->rules; r; r = r->next) {
360 char *indent = "\t\t\0";
363 print("%2/* %R */\n", r);
365 print("%2c = %s;\n", r->code);
366 emitrecord("\t\t", r, "c", 0);
368 emitrecord("\t\t", r, r->code, 0);
371 if (r->pattern->nterms > 1) {
372 print("%2if (%1/* %R */\n", r);
373 emittest(r->pattern->left, "LEFT_CHILD(a)", " ");
377 print("%2/* %R */\n", r);
378 if (r->pattern->nterms == 2 && r->pattern->left
379 && r->pattern->right == NULL)
380 emitrecalc(indent, r->pattern->op, r->pattern->left->op);
381 print("%sc = ", indent);
382 emitcost(r->pattern->left, "LEFT_CHILD(a)");
383 print("%s;\n", r->code);
384 emitrecord(indent, r, "c", 0);
389 if (r->pattern->nterms > 1) {
390 print("%2if (%1/* %R */\n", r);
391 emittest(r->pattern->left, "LEFT_CHILD(a)",
392 r->pattern->right->nterms ? " && " : " ");
393 emittest(r->pattern->right, "RIGHT_CHILD(a)", " ");
397 print("%2/* %R */\n", r);
398 print("%sc = ", indent);
399 emitcost(r->pattern->left, "LEFT_CHILD(a)");
400 emitcost(r->pattern->right, "RIGHT_CHILD(a)");
401 print("%s;\n", r->code);
402 emitrecord(indent, r, "c", 0);
412 /* emitclosure - emit the closure functions */
413 static void emitclosure(Nonterm nts) {
416 for (p = nts; p; p = p->link)
418 print("static void %Pclosure_%S(NODEPTR_TYPE, int);\n", p);
420 for (p = nts; p; p = p->link)
423 print("static void %Pclosure_%S(NODEPTR_TYPE a, int c) {\n"
424 "%1struct %Pstate *p = STATE_LABEL(a);\n", p);
425 for (r = p->chain; r; r = r->chain)
426 emitrecord("\t", r, "c", r->cost);
431 /* emitcost - emit cost computation for tree t */
432 static void emitcost(Tree t, char *v) {
435 if (p->kind == TERM) {
437 emitcost(t->left, stringf("LEFT_CHILD(%s)", v));
439 emitcost(t->right, stringf("RIGHT_CHILD(%s)", v));
441 print("((struct %Pstate *)(%s->x.state))->cost[%P%S_NT] + ", v, p);
444 /* emitdefs - emit nonterminal defines and data structures */
445 static void emitdefs(Nonterm nts, int ntnumber) {
448 for (p = nts; p; p = p->link)
449 print("#define %P%S_NT %d\n", p, p->number);
451 print("static char *%Pntname[] = {\n%10,\n");
452 for (p = nts; p; p = p->link)
453 print("%1\"%S\",\n", p);
454 print("%10\n};\n\n");
457 /* emitheader - emit initial definitions */
458 static void emitheader(void) {
459 time_t timer = time(NULL);
461 print("/*\ngenerated at %sby %s\n*/\n", ctime(&timer), rcsid);
462 print("static void %Pkids(NODEPTR_TYPE, int, NODEPTR_TYPE[]);\n");
463 print("static void %Plabel(NODEPTR_TYPE);\n");
464 print("static int %Prule(void*, int);\n\n");
467 /* computekids - compute paths to kids in tree t */
468 static char *computekids(Tree t, char *v, char *bp, int *ip) {
471 if (p->kind == NONTERM) {
472 sprintf(bp, "\t\tkids[%d] = %s;\n", (*ip)++, v);
474 } else if (p->arity > 0) {
475 bp = computekids(t->left, stringf("LEFT_CHILD(%s)", v), bp, ip);
477 bp = computekids(t->right, stringf("RIGHT_CHILD(%s)", v), bp, ip);
482 /* emitkids - emit _kids */
483 static void emitkids(Rule rules, int nrules) {
485 Rule r, *rc = alloc((nrules + 1 + 1)*sizeof *rc);
486 char **str = alloc((nrules + 1 + 1)*sizeof *str);
488 for (i = 0, r = rules; r; r = r->link) {
490 char buf[1024], *bp = buf;
491 *computekids(r->pattern, "p", bp, &j) = 0;
492 for (j = 0; str[j] && strcmp(str[j], buf); j++)
495 str[j] = strcpy(alloc(strlen(buf) + 1), buf);
499 print("static void %Pkids(NODEPTR_TYPE p, int eruleno, NODEPTR_TYPE kids[]) {\n"
500 "%1if (!p)\n%2fatal(\"%Pkids\", \"Null tree\\n\", 0);\n"
501 "%1if (!kids)\n%2fatal(\"%Pkids\", \"Null kids\\n\", 0);\n"
502 "%1switch (eruleno) {\n");
503 for (i = 0; (r = rc[i]) != NULL; i++) {
504 for ( ; r; r = r->kids)
505 print("%1case %d: /* %R */\n", r->ern, r);
506 print("%s%2break;\n", str[i]);
508 print("%1default:\n%2fatal(\"%Pkids\", \"Bad rule number %%d\\n\", eruleno);\n%1}\n}\n\n");
511 /* emitlabel - emit label function */
512 static void emitlabel(Term terms, Nonterm start, int ntnumber) {
516 print("static void %Plabel(NODEPTR_TYPE a) {\n%1int c;\n"
517 "%1struct %Pstate *p;\n\n"
518 "%1if (!a)\n%2fatal(\"%Plabel\", \"Null tree\\n\", 0);\n");
519 print("%1STATE_LABEL(a) = p = allocate(sizeof *p, FUNC);\n"
520 "%1p->rule._stmt = 0;\n");
521 for (i = 1; i <= ntnumber; i++)
522 print("%1p->cost[%d] =\n", i);
523 print("%20x7fff;\n%1switch (OP_LABEL(a)) {\n");
524 for (p = terms; p; p = p->link)
525 emitcase(p, ntnumber);
527 "%2fatal(\"%Plabel\", \"Bad terminal %%d\\n\", OP_LABEL(a));\n%1}\n}\n\n");
530 /* computents - fill in bp with _nts vector for tree t */
531 static char *computents(Tree t, char *bp) {
534 if (p->kind == NONTERM) {
535 sprintf(bp, "%s_%s_NT, ", prefix, p->name);
538 bp = computents(t->right, computents(t->left, bp));
543 /* emitnts - emit _nts ragged array */
544 static void emitnts(Rule rules, int nrules) {
546 int i, j, *nts = alloc((nrules + 1)*sizeof *nts);
547 char **str = alloc((nrules + 1)*sizeof *str);
549 for (i = 0, r = rules; r; r = r->link) {
551 *computents(r->pattern, buf) = 0;
552 for (j = 0; str[j] && strcmp(str[j], buf); j++)
554 if (str[j] == NULL) {
555 print("static short %Pnts_%d[] = { %s0 };\n", j, buf);
556 str[j] = strcpy(alloc(strlen(buf) + 1), buf);
560 print("\nstatic short *%Pnts[] = {\n");
561 for (i = j = 0, r = rules; r; r = r->link) {
562 for ( ; j < r->ern; j++)
563 print("%10,%1/* %d */\n", j);
564 print("%1%Pnts_%d,%1/* %d */\n", nts[i++], j++);
569 /* emitrecalc - emit code that tests for recalculation of INDIR?(VREGP) */
570 static void emitrecalc(char *pre, Term root, Term kid) {
571 if (root->kind == TERM && strncmp(root->name, "INDIR", 5) == 0
572 && kid->kind == TERM && strcmp(kid->name, "VREGP" ) == 0) {
574 print("%sif (mayrecalc(a)) {\n", pre);
575 print("%s%1struct %Pstate *q = a->syms[RX]->u.t.cse->x.state;\n", pre);
576 for (p = nts; p; p = p->link) {
577 print("%s%1if (q->cost[%P%S_NT] == 0) {\n", pre, p);
578 print("%s%2p->cost[%P%S_NT] = 0;\n", pre, p);
579 print("%s%2p->rule.%P%S = q->rule.%P%S;\n", pre, p, p);
580 print("%s%1}\n", pre);
586 /* emitrecord - emit code that tests for a winning match of rule r */
587 static void emitrecord(char *pre, Rule r, char *c, int cost) {
589 print("%s%Ptrace(a, %d, %s + %d, p->cost[%P%S_NT]);\n",
590 pre, r->ern, c, cost, r->lhs);
591 print("%sif (", pre);
592 print("%s + %d < p->cost[%P%S_NT]) {\n"
593 "%s%1p->cost[%P%S_NT] = %s + %d;\n%s%1p->rule.%P%S = %d;\n",
594 c, cost, r->lhs, pre, r->lhs, c, cost, pre, r->lhs,
597 print("%s%1%Pclosure_%S(a, %s + %d);\n", pre, r->lhs, c, cost);
601 /* emitrule - emit decoding vectors and _rule */
602 static void emitrule(Nonterm nts) {
605 for (p = nts; p; p = p->link) {
607 print("static short %Pdecode_%S[] = {\n%10,\n", p);
608 for (r = p->rules; r; r = r->decode)
609 print("%1%d,\n", r->ern);
612 print("static int %Prule(void *state, int goalnt) {\n"
613 "%1if (goalnt < 1 || goalnt > %d)\n%2fatal(\"%Prule\", \"Bad goal nonterminal %%d\\n\", goalnt);\n"
614 "%1if (!state)\n%2return 0;\n%1switch (goalnt) {\n", ntnumber);
615 for (p = nts; p; p = p->link)
616 print("%1case %P%S_NT:"
617 "%1return %Pdecode_%S[((struct %Pstate *)state)->rule.%P%S];\n", p, p, p);
618 print("%1default:\n%2fatal(\"%Prule\", \"Bad goal nonterminal %%d\\n\", goalnt);\n%2return 0;\n%1}\n}\n\n");
621 /* emitstring - emit arrays of templates, instruction flags, and rules */
622 static void emitstring(Rule rules) {
625 print("static char *%Ptemplates[] = {\n");
626 print("/* 0 */%10,\n");
627 for (r = rules; r; r = r->link)
628 print("/* %d */%1\"%s\",%1/* %R */\n", r->ern, r->template, r);
630 print("\nstatic char %Pisinstruction[] = {\n");
631 print("/* 0 */%10,\n");
632 for (r = rules; r; r = r->link) {
633 int len = strlen(r->template);
634 print("/* %d */%1%d,%1/* %s */\n", r->ern,
635 len >= 2 && r->template[len-2] == '\\' && r->template[len-1] == 'n',
639 print("\nstatic char *%Pstring[] = {\n");
640 print("/* 0 */%10,\n");
641 for (r = rules; r; r = r->link)
642 print("/* %d */%1\"%R\",\n", r->ern, r);
646 /* emitstruct - emit the definition of the state structure */
647 static void emitstruct(Nonterm nts, int ntnumber) {
648 print("struct %Pstate {\n%1short cost[%d];\n%1struct {\n", ntnumber + 1);
649 for ( ; nts; nts = nts->link) {
650 int n = 1, m = nts->lhscount;
651 while ((m >>= 1) != 0)
653 print("%2unsigned int %P%S:%d;\n", nts, n);
655 print("%1} rule;\n};\n\n");
658 /* emittest - emit clause for testing a match */
659 static void emittest(Tree t, char *v, char *suffix) {
662 if (p->kind == TERM) {
663 print("%3%s->op == %d%s/* %S */\n", v, p->esn,
664 t->nterms > 1 ? " && " : suffix, p);
666 emittest(t->left, stringf("LEFT_CHILD(%s)", v),
667 t->right && t->right->nterms ? " && " : suffix);
669 emittest(t->right, stringf("RIGHT_CHILD(%s)", v), suffix);