1
2 /* Print a bunch of C initializers that represent a grammar */
3
4 #include "pgenheaders.h"
5 #include "grammar.h"
6
7 /* Forward */
8 static void printarcs(int, dfa *, FILE *);
9 static void printstates(grammar *, FILE *);
10 static void printdfas(grammar *, FILE *);
11 static void printlabels(grammar *, FILE *);
12
13 void
printgrammar(grammar * g,FILE * fp)14 printgrammar(grammar *g, FILE *fp)
15 {
16 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
17 fprintf(fp, "#include \"pgenheaders.h\"\n");
18 fprintf(fp, "#include \"grammar.h\"\n");
19 fprintf(fp, "PyAPI_DATA(grammar) _PyParser_Grammar;\n");
20 printdfas(g, fp);
21 printlabels(g, fp);
22 fprintf(fp, "grammar _PyParser_Grammar = {\n");
23 fprintf(fp, " %d,\n", g->g_ndfas);
24 fprintf(fp, " dfas,\n");
25 fprintf(fp, " {%d, labels},\n", g->g_ll.ll_nlabels);
26 fprintf(fp, " %d\n", g->g_start);
27 fprintf(fp, "};\n");
28 }
29
30 void
printnonterminals(grammar * g,FILE * fp)31 printnonterminals(grammar *g, FILE *fp)
32 {
33 dfa *d;
34 int i;
35
36 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
37
38 d = g->g_dfa;
39 for (i = g->g_ndfas; --i >= 0; d++)
40 fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
41 }
42
43 static void
printarcs(int i,dfa * d,FILE * fp)44 printarcs(int i, dfa *d, FILE *fp)
45 {
46 arc *a;
47 state *s;
48 int j, k;
49
50 s = d->d_state;
51 for (j = 0; j < d->d_nstates; j++, s++) {
52 fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
53 i, j, s->s_narcs);
54 a = s->s_arc;
55 for (k = 0; k < s->s_narcs; k++, a++)
56 fprintf(fp, " {%d, %d},\n", a->a_lbl, a->a_arrow);
57 fprintf(fp, "};\n");
58 }
59 }
60
61 static void
printstates(grammar * g,FILE * fp)62 printstates(grammar *g, FILE *fp)
63 {
64 state *s;
65 dfa *d;
66 int i, j;
67
68 d = g->g_dfa;
69 for (i = 0; i < g->g_ndfas; i++, d++) {
70 printarcs(i, d, fp);
71 fprintf(fp, "static state states_%d[%d] = {\n",
72 i, d->d_nstates);
73 s = d->d_state;
74 for (j = 0; j < d->d_nstates; j++, s++)
75 fprintf(fp, " {%d, arcs_%d_%d},\n",
76 s->s_narcs, i, j);
77 fprintf(fp, "};\n");
78 }
79 }
80
81 static void
printdfas(grammar * g,FILE * fp)82 printdfas(grammar *g, FILE *fp)
83 {
84 dfa *d;
85 int i, j;
86
87 printstates(g, fp);
88 fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
89 d = g->g_dfa;
90 for (i = 0; i < g->g_ndfas; i++, d++) {
91 fprintf(fp, " {%d, \"%s\", %d, %d, states_%d,\n",
92 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
93 fprintf(fp, " \"");
94 for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
95 fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
96 fprintf(fp, "\"},\n");
97 }
98 fprintf(fp, "};\n");
99 }
100
101 static void
printlabels(grammar * g,FILE * fp)102 printlabels(grammar *g, FILE *fp)
103 {
104 label *l;
105 int i;
106
107 fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
108 l = g->g_ll.ll_label;
109 for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
110 if (l->lb_str == NULL)
111 fprintf(fp, " {%d, 0},\n", l->lb_type);
112 else
113 fprintf(fp, " {%d, \"%s\"},\n",
114 l->lb_type, l->lb_str);
115 }
116 fprintf(fp, "};\n");
117 }
118