Issue #4677: add two list comprehension tests to pybench.
[python.git] / Parser / printgrammar.c
blobec8f4daa4ef45e40e32e98242fdb9c31805434d5
2 /* Print a bunch of C initializers that represent a grammar */
4 #include "pgenheaders.h"
5 #include "grammar.h"
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 *);
13 void
14 printgrammar(grammar *g, FILE *fp)
16 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
17 fprintf(fp, "#include \"pgenheaders.h\"\n");
18 fprintf(fp, "#include \"grammar.h\"\n");
19 printdfas(g, fp);
20 printlabels(g, fp);
21 fprintf(fp, "grammar _PyParser_Grammar = {\n");
22 fprintf(fp, "\t%d,\n", g->g_ndfas);
23 fprintf(fp, "\tdfas,\n");
24 fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
25 fprintf(fp, "\t%d\n", g->g_start);
26 fprintf(fp, "};\n");
29 void
30 printnonterminals(grammar *g, FILE *fp)
32 dfa *d;
33 int i;
35 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
37 d = g->g_dfa;
38 for (i = g->g_ndfas; --i >= 0; d++)
39 fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
42 static void
43 printarcs(int i, dfa *d, FILE *fp)
45 arc *a;
46 state *s;
47 int j, k;
49 s = d->d_state;
50 for (j = 0; j < d->d_nstates; j++, s++) {
51 fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
52 i, j, s->s_narcs);
53 a = s->s_arc;
54 for (k = 0; k < s->s_narcs; k++, a++)
55 fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
56 fprintf(fp, "};\n");
60 static void
61 printstates(grammar *g, FILE *fp)
63 state *s;
64 dfa *d;
65 int i, j;
67 d = g->g_dfa;
68 for (i = 0; i < g->g_ndfas; i++, d++) {
69 printarcs(i, d, fp);
70 fprintf(fp, "static state states_%d[%d] = {\n",
71 i, d->d_nstates);
72 s = d->d_state;
73 for (j = 0; j < d->d_nstates; j++, s++)
74 fprintf(fp, "\t{%d, arcs_%d_%d},\n",
75 s->s_narcs, i, j);
76 fprintf(fp, "};\n");
80 static void
81 printdfas(grammar *g, FILE *fp)
83 dfa *d;
84 int i, j;
86 printstates(g, fp);
87 fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
88 d = g->g_dfa;
89 for (i = 0; i < g->g_ndfas; i++, d++) {
90 fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n",
91 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
92 fprintf(fp, "\t \"");
93 for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
94 fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
95 fprintf(fp, "\"},\n");
97 fprintf(fp, "};\n");
100 static void
101 printlabels(grammar *g, FILE *fp)
103 label *l;
104 int i;
106 fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
107 l = g->g_ll.ll_label;
108 for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
109 if (l->lb_str == NULL)
110 fprintf(fp, "\t{%d, 0},\n", l->lb_type);
111 else
112 fprintf(fp, "\t{%d, \"%s\"},\n",
113 l->lb_type, l->lb_str);
115 fprintf(fp, "};\n");