2 /* Print a bunch of C initializers that represent a grammar */
4 #include "pgenheaders.h"
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 *);
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");
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
);
31 printnonterminals(grammar
*g
, FILE *fp
)
36 fprintf(fp
, "/* Generated by Parser/pgen */\n\n");
39 for (i
= g
->g_ndfas
; --i
>= 0; d
++)
40 fprintf(fp
, "#define %s %d\n", d
->d_name
, d
->d_type
);
44 printarcs(int i
, dfa
*d
, FILE *fp
)
51 for (j
= 0; j
< d
->d_nstates
; j
++, s
++) {
52 fprintf(fp
, "static arc arcs_%d_%d[%d] = {\n",
55 for (k
= 0; k
< s
->s_narcs
; k
++, a
++)
56 fprintf(fp
, "\t{%d, %d},\n", a
->a_lbl
, a
->a_arrow
);
62 printstates(grammar
*g
, FILE *fp
)
69 for (i
= 0; i
< g
->g_ndfas
; i
++, d
++) {
71 fprintf(fp
, "static state states_%d[%d] = {\n",
74 for (j
= 0; j
< d
->d_nstates
; j
++, s
++)
75 fprintf(fp
, "\t{%d, arcs_%d_%d},\n",
82 printdfas(grammar
*g
, FILE *fp
)
88 fprintf(fp
, "static dfa dfas[%d] = {\n", g
->g_ndfas
);
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
);
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");
102 printlabels(grammar
*g
, FILE *fp
)
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
);
113 fprintf(fp
, "\t{%d, \"%s\"},\n",
114 l
->lb_type
, l
->lb_str
);