blob: 55406981407ae19482f0ec84e797b85154d3b018 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Print a bunch of C initializers that represent a grammar */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005#include "grammar.h"
6
Guido van Rossum3f5da241990-12-20 15:06:42 +00007/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +00008static void printarcs(int, dfa *, FILE *);
9static void printstates(grammar *, FILE *);
10static void printdfas(grammar *, FILE *);
11static void printlabels(grammar *, FILE *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000012
13void
Thomas Wouters23c9e002000-07-22 19:20:54 +000014printgrammar(grammar *g, FILE *fp)
Guido van Rossum3f5da241990-12-20 15:06:42 +000015{
16 fprintf(fp, "#include \"pgenheaders.h\"\n");
17 fprintf(fp, "#include \"grammar.h\"\n");
18 printdfas(g, fp);
19 printlabels(g, fp);
Guido van Rossumee2373b1997-05-07 23:51:07 +000020 fprintf(fp, "grammar _PyParser_Grammar = {\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000021 fprintf(fp, "\t%d,\n", g->g_ndfas);
22 fprintf(fp, "\tdfas,\n");
23 fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
24 fprintf(fp, "\t%d\n", g->g_start);
25 fprintf(fp, "};\n");
26}
27
28void
Thomas Wouters23c9e002000-07-22 19:20:54 +000029printnonterminals(grammar *g, FILE *fp)
Guido van Rossum3f5da241990-12-20 15:06:42 +000030{
31 dfa *d;
32 int i;
33
34 d = g->g_dfa;
35 for (i = g->g_ndfas; --i >= 0; d++)
36 fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
37}
38
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000040printarcs(int i, dfa *d, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041{
42 arc *a;
43 state *s;
44 int j, k;
45
46 s = d->d_state;
47 for (j = 0; j < d->d_nstates; j++, s++) {
48 fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
49 i, j, s->s_narcs);
50 a = s->s_arc;
51 for (k = 0; k < s->s_narcs; k++, a++)
52 fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
53 fprintf(fp, "};\n");
54 }
55}
56
57static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000058printstates(grammar *g, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059{
60 state *s;
61 dfa *d;
62 int i, j;
63
64 d = g->g_dfa;
65 for (i = 0; i < g->g_ndfas; i++, d++) {
66 printarcs(i, d, fp);
67 fprintf(fp, "static state states_%d[%d] = {\n",
68 i, d->d_nstates);
69 s = d->d_state;
70 for (j = 0; j < d->d_nstates; j++, s++)
71 fprintf(fp, "\t{%d, arcs_%d_%d},\n",
72 s->s_narcs, i, j);
73 fprintf(fp, "};\n");
74 }
75}
76
77static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000078printdfas(grammar *g, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079{
80 dfa *d;
81 int i, j;
82
83 printstates(g, fp);
84 fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
85 d = g->g_dfa;
86 for (i = 0; i < g->g_ndfas; i++, d++) {
87 fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n",
88 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
89 fprintf(fp, "\t \"");
90 for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
91 fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
92 fprintf(fp, "\"},\n");
93 }
94 fprintf(fp, "};\n");
95}
96
97static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000098printlabels(grammar *g, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099{
100 label *l;
101 int i;
102
103 fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
104 l = g->g_ll.ll_label;
105 for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
106 if (l->lb_str == NULL)
107 fprintf(fp, "\t{%d, 0},\n", l->lb_type);
108 else
109 fprintf(fp, "\t{%d, \"%s\"},\n",
110 l->lb_type, l->lb_str);
111 }
112 fprintf(fp, "};\n");
113}