blob: 7311e55170dee84bcd97729072cbe76027a824ee [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Print a bunch of C initializers that represent a grammar */
3
Victor Stinner60fe4992010-12-28 23:05:20 +00004#define PGEN
5
Guido van Rossum3f5da241990-12-20 15:06:42 +00006#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "grammar.h"
8
Guido van Rossum3f5da241990-12-20 15:06:42 +00009/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000010static void printarcs(int, dfa *, FILE *);
11static void printstates(grammar *, FILE *);
12static void printdfas(grammar *, FILE *);
13static void printlabels(grammar *, FILE *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
15void
Thomas Wouters23c9e002000-07-22 19:20:54 +000016printgrammar(grammar *g, FILE *fp)
Guido van Rossum3f5da241990-12-20 15:06:42 +000017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
19 fprintf(fp, "#include \"pgenheaders.h\"\n");
20 fprintf(fp, "#include \"grammar.h\"\n");
21 fprintf(fp, "PyAPI_DATA(grammar) _PyParser_Grammar;\n");
22 printdfas(g, fp);
23 printlabels(g, fp);
24 fprintf(fp, "grammar _PyParser_Grammar = {\n");
Victor Stinnerbeba8262010-05-15 23:00:51 +000025 fprintf(fp, " %d,\n", g->g_ndfas);
26 fprintf(fp, " dfas,\n");
27 fprintf(fp, " {%d, labels},\n", g->g_ll.ll_nlabels);
28 fprintf(fp, " %d\n", g->g_start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 fprintf(fp, "};\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000030}
31
32void
Thomas Wouters23c9e002000-07-22 19:20:54 +000033printnonterminals(grammar *g, FILE *fp)
Guido van Rossum3f5da241990-12-20 15:06:42 +000034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 dfa *d;
36 int i;
37
38 fprintf(fp, "/* Generated by Parser/pgen */\n\n");
39
40 d = g->g_dfa;
41 for (i = g->g_ndfas; --i >= 0; d++)
42 fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
Guido van Rossum3f5da241990-12-20 15:06:42 +000043}
44
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000046printarcs(int i, dfa *d, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 arc *a;
49 state *s;
50 int j, k;
51
52 s = d->d_state;
53 for (j = 0; j < d->d_nstates; j++, s++) {
54 fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
55 i, j, s->s_narcs);
56 a = s->s_arc;
57 for (k = 0; k < s->s_narcs; k++, a++)
Victor Stinnerbeba8262010-05-15 23:00:51 +000058 fprintf(fp, " {%d, %d},\n", a->a_lbl, a->a_arrow);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 fprintf(fp, "};\n");
60 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061}
62
63static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000064printstates(grammar *g, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 state *s;
67 dfa *d;
68 int i, j;
69
70 d = g->g_dfa;
71 for (i = 0; i < g->g_ndfas; i++, d++) {
72 printarcs(i, d, fp);
73 fprintf(fp, "static state states_%d[%d] = {\n",
74 i, d->d_nstates);
75 s = d->d_state;
76 for (j = 0; j < d->d_nstates; j++, s++)
Victor Stinnerbeba8262010-05-15 23:00:51 +000077 fprintf(fp, " {%d, arcs_%d_%d},\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 s->s_narcs, i, j);
79 fprintf(fp, "};\n");
80 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
83static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000084printdfas(grammar *g, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 dfa *d;
Serhiy Storchaka67c719b2014-09-05 10:10:23 +030087 int i, j, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088
89 printstates(g, fp);
90 fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
91 d = g->g_dfa;
92 for (i = 0; i < g->g_ndfas; i++, d++) {
Victor Stinnerbeba8262010-05-15 23:00:51 +000093 fprintf(fp, " {%d, \"%s\", %d, %d, states_%d,\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
Victor Stinnerbeba8262010-05-15 23:00:51 +000095 fprintf(fp, " \"");
Serhiy Storchaka67c719b2014-09-05 10:10:23 +030096 n = NBYTES(g->g_ll.ll_nlabels);
97 for (j = 0; j < n; j++)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
99 fprintf(fp, "\"},\n");
100 }
101 fprintf(fp, "};\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102}
103
104static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000105printlabels(grammar *g, FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 label *l;
108 int i;
109
110 fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
111 l = g->g_ll.ll_label;
112 for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
113 if (l->lb_str == NULL)
Victor Stinnerbeba8262010-05-15 23:00:51 +0000114 fprintf(fp, " {%d, 0},\n", l->lb_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 else
Victor Stinnerbeba8262010-05-15 23:00:51 +0000116 fprintf(fp, " {%d, \"%s\"},\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 l->lb_type, l->lb_str);
118 }
119 fprintf(fp, "};\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120}