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");
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
);
30 printnonterminals(grammar
*g
, FILE *fp
)
35 fprintf(fp
, "/* Generated by Parser/pgen */\n\n");
38 for (i
= g
->g_ndfas
; --i
>= 0; d
++)
39 fprintf(fp
, "#define %s %d\n", d
->d_name
, d
->d_type
);
43 printarcs(int i
, dfa
*d
, FILE *fp
)
50 for (j
= 0; j
< d
->d_nstates
; j
++, s
++) {
51 fprintf(fp
, "static arc arcs_%d_%d[%d] = {\n",
54 for (k
= 0; k
< s
->s_narcs
; k
++, a
++)
55 fprintf(fp
, "\t{%d, %d},\n", a
->a_lbl
, a
->a_arrow
);
61 printstates(grammar
*g
, FILE *fp
)
68 for (i
= 0; i
< g
->g_ndfas
; i
++, d
++) {
70 fprintf(fp
, "static state states_%d[%d] = {\n",
73 for (j
= 0; j
< d
->d_nstates
; j
++, s
++)
74 fprintf(fp
, "\t{%d, arcs_%d_%d},\n",
81 printdfas(grammar
*g
, FILE *fp
)
87 fprintf(fp
, "static dfa dfas[%d] = {\n", g
->g_ndfas
);
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
);
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");
101 printlabels(grammar
*g
, FILE *fp
)
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
);
112 fprintf(fp
, "\t{%d, \"%s\"},\n",
113 l
->lb_type
, l
->lb_str
);