Fix typo in comment.
[python.git] / Parser / printgrammar.c
blobae180e11f2ea89bf67aed6214e13bbc63f6de5f2
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 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, "\t%d,\n", g->g_ndfas);
24 fprintf(fp, "\tdfas,\n");
25 fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
26 fprintf(fp, "\t%d\n", g->g_start);
27 fprintf(fp, "};\n");
30 void
31 printnonterminals(grammar *g, FILE *fp)
33 dfa *d;
34 int i;
36 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
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);
43 static void
44 printarcs(int i, dfa *d, FILE *fp)
46 arc *a;
47 state *s;
48 int j, k;
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, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
57 fprintf(fp, "};\n");
61 static void
62 printstates(grammar *g, FILE *fp)
64 state *s;
65 dfa *d;
66 int i, j;
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, "\t{%d, arcs_%d_%d},\n",
76 s->s_narcs, i, j);
77 fprintf(fp, "};\n");
81 static void
82 printdfas(grammar *g, FILE *fp)
84 dfa *d;
85 int i, j;
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, "\t{%d, \"%s\", %d, %d, states_%d,\n",
92 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
93 fprintf(fp, "\t \"");
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");
98 fprintf(fp, "};\n");
101 static void
102 printlabels(grammar *g, FILE *fp)
104 label *l;
105 int i;
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, "\t{%d, 0},\n", l->lb_type);
112 else
113 fprintf(fp, "\t{%d, \"%s\"},\n",
114 l->lb_type, l->lb_str);
116 fprintf(fp, "};\n");