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