1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Print a bunch of C initializers that represent a grammar */
|
|
|
|
|
2010-12-28 19:05:20 -04:00
|
|
|
#define PGEN
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "pgenheaders.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
#include "grammar.h"
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Forward */
|
2000-07-09 00:09:57 -03:00
|
|
|
static void printarcs(int, dfa *, FILE *);
|
|
|
|
static void printstates(grammar *, FILE *);
|
|
|
|
static void printdfas(grammar *, FILE *);
|
|
|
|
static void printlabels(grammar *, FILE *);
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
void
|
2000-07-22 16:20:54 -03:00
|
|
|
printgrammar(grammar *g, FILE *fp)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
fprintf(fp, "/* Generated by Parser/pgen */\n\n");
|
|
|
|
fprintf(fp, "#include \"pgenheaders.h\"\n");
|
|
|
|
fprintf(fp, "#include \"grammar.h\"\n");
|
2018-09-12 16:06:42 -03:00
|
|
|
fprintf(fp, "grammar _PyParser_Grammar;\n");
|
2010-05-09 12:52:27 -03:00
|
|
|
printdfas(g, fp);
|
|
|
|
printlabels(g, fp);
|
|
|
|
fprintf(fp, "grammar _PyParser_Grammar = {\n");
|
2010-05-15 20:00:51 -03:00
|
|
|
fprintf(fp, " %d,\n", g->g_ndfas);
|
|
|
|
fprintf(fp, " dfas,\n");
|
|
|
|
fprintf(fp, " {%d, labels},\n", g->g_ll.ll_nlabels);
|
|
|
|
fprintf(fp, " %d\n", g->g_start);
|
2010-05-09 12:52:27 -03:00
|
|
|
fprintf(fp, "};\n");
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-22 16:20:54 -03:00
|
|
|
printnonterminals(grammar *g, FILE *fp)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
dfa *d;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(fp, "/* Generated by Parser/pgen */\n\n");
|
|
|
|
|
|
|
|
d = g->g_dfa;
|
|
|
|
for (i = g->g_ndfas; --i >= 0; d++)
|
|
|
|
fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2000-07-22 16:20:54 -03:00
|
|
|
printarcs(int i, dfa *d, FILE *fp)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
arc *a;
|
|
|
|
state *s;
|
|
|
|
int j, k;
|
|
|
|
|
|
|
|
s = d->d_state;
|
|
|
|
for (j = 0; j < d->d_nstates; j++, s++) {
|
|
|
|
fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
|
|
|
|
i, j, s->s_narcs);
|
|
|
|
a = s->s_arc;
|
|
|
|
for (k = 0; k < s->s_narcs; k++, a++)
|
2010-05-15 20:00:51 -03:00
|
|
|
fprintf(fp, " {%d, %d},\n", a->a_lbl, a->a_arrow);
|
2010-05-09 12:52:27 -03:00
|
|
|
fprintf(fp, "};\n");
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 16:20:54 -03:00
|
|
|
printstates(grammar *g, FILE *fp)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
state *s;
|
|
|
|
dfa *d;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
d = g->g_dfa;
|
|
|
|
for (i = 0; i < g->g_ndfas; i++, d++) {
|
|
|
|
printarcs(i, d, fp);
|
|
|
|
fprintf(fp, "static state states_%d[%d] = {\n",
|
|
|
|
i, d->d_nstates);
|
|
|
|
s = d->d_state;
|
|
|
|
for (j = 0; j < d->d_nstates; j++, s++)
|
2010-05-15 20:00:51 -03:00
|
|
|
fprintf(fp, " {%d, arcs_%d_%d},\n",
|
2010-05-09 12:52:27 -03:00
|
|
|
s->s_narcs, i, j);
|
|
|
|
fprintf(fp, "};\n");
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 16:20:54 -03:00
|
|
|
printdfas(grammar *g, FILE *fp)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
dfa *d;
|
2014-09-05 04:10:23 -03:00
|
|
|
int i, j, n;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
printstates(g, fp);
|
|
|
|
fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
|
|
|
|
d = g->g_dfa;
|
|
|
|
for (i = 0; i < g->g_ndfas; i++, d++) {
|
2010-05-15 20:00:51 -03:00
|
|
|
fprintf(fp, " {%d, \"%s\", %d, %d, states_%d,\n",
|
2010-05-09 12:52:27 -03:00
|
|
|
d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
|
2010-05-15 20:00:51 -03:00
|
|
|
fprintf(fp, " \"");
|
2014-09-05 04:10:23 -03:00
|
|
|
n = NBYTES(g->g_ll.ll_nlabels);
|
|
|
|
for (j = 0; j < n; j++)
|
2010-05-09 12:52:27 -03:00
|
|
|
fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
|
|
|
|
fprintf(fp, "\"},\n");
|
|
|
|
}
|
|
|
|
fprintf(fp, "};\n");
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 16:20:54 -03:00
|
|
|
printlabels(grammar *g, FILE *fp)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
label *l;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
|
|
|
|
l = g->g_ll.ll_label;
|
|
|
|
for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
|
|
|
|
if (l->lb_str == NULL)
|
2010-05-15 20:00:51 -03:00
|
|
|
fprintf(fp, " {%d, 0},\n", l->lb_type);
|
2010-05-09 12:52:27 -03:00
|
|
|
else
|
2010-05-15 20:00:51 -03:00
|
|
|
fprintf(fp, " {%d, \"%s\"},\n",
|
2010-05-09 12:52:27 -03:00
|
|
|
l->lb_type, l->lb_str);
|
|
|
|
}
|
|
|
|
fprintf(fp, "};\n");
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|