No issues found
1 /* Print a bunch of C initializers that represent a grammar */
2
3 #include "pgenheaders.h"
4 #include "grammar.h"
5
6 /* Forward */
7 static void printarcs(int, dfa *, FILE *);
8 static void printstates(grammar *, FILE *);
9 static void printdfas(grammar *, FILE *);
10 static void printlabels(grammar *, FILE *);
11
12 void
13 printgrammar(grammar *g, FILE *fp)
14 {
15 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
16 fprintf(fp, "#include \"pgenheaders.h\"\n");
17 fprintf(fp, "#include \"grammar.h\"\n");
18 fprintf(fp, "PyAPI_DATA(grammar) _PyParser_Grammar;\n");
19 printdfas(g, fp);
20 printlabels(g, fp);
21 fprintf(fp, "grammar _PyParser_Grammar = {\n");
22 fprintf(fp, " %d,\n", g->g_ndfas);
23 fprintf(fp, " dfas,\n");
24 fprintf(fp, " {%d, labels},\n", g->g_ll.ll_nlabels);
25 fprintf(fp, " %d\n", g->g_start);
26 fprintf(fp, "};\n");
27 }
28
29 void
30 printnonterminals(grammar *g, FILE *fp)
31 {
32 dfa *d;
33 int i;
34
35 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
36
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);
40 }
41
42 static void
43 printarcs(int i, dfa *d, FILE *fp)
44 {
45 arc *a;
46 state *s;
47 int j, k;
48
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, " {%d, %d},\n", a->a_lbl, a->a_arrow);
56 fprintf(fp, "};\n");
57 }
58 }
59
60 static void
61 printstates(grammar *g, FILE *fp)
62 {
63 state *s;
64 dfa *d;
65 int i, j;
66
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, " {%d, arcs_%d_%d},\n",
75 s->s_narcs, i, j);
76 fprintf(fp, "};\n");
77 }
78 }
79
80 static void
81 printdfas(grammar *g, FILE *fp)
82 {
83 dfa *d;
84 int i, j;
85
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, " {%d, \"%s\", %d, %d, states_%d,\n",
91 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
92 fprintf(fp, " \"");
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");
96 }
97 fprintf(fp, "};\n");
98 }
99
100 static void
101 printlabels(grammar *g, FILE *fp)
102 {
103 label *l;
104 int i;
105
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, " {%d, 0},\n", l->lb_type);
111 else
112 fprintf(fp, " {%d, \"%s\"},\n",
113 l->lb_type, l->lb_str);
114 }
115 fprintf(fp, "};\n");
116 }