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