ยปCore Development>Code coverage>Parser/printgrammar.c

Python code coverage for Parser/printgrammar.c

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