blob: 705cbcb2265e3e0215a27834adadf0903dde211a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumb9f8d6e1995-01-04 19:08:09 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Print a bunch of C initializers that represent a grammar */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035#include "grammar.h"
36
Guido van Rossum3f5da241990-12-20 15:06:42 +000037/* Forward */
Guido van Rossum86bea461997-04-29 21:03:06 +000038static void printarcs Py_PROTO((int, dfa *, FILE *));
39static void printstates Py_PROTO((grammar *, FILE *));
40static void printdfas Py_PROTO((grammar *, FILE *));
41static void printlabels Py_PROTO((grammar *, FILE *));
Guido van Rossum3f5da241990-12-20 15:06:42 +000042
43void
44printgrammar(g, fp)
45 grammar *g;
46 FILE *fp;
47{
48 fprintf(fp, "#include \"pgenheaders.h\"\n");
49 fprintf(fp, "#include \"grammar.h\"\n");
50 printdfas(g, fp);
51 printlabels(g, fp);
Guido van Rossumee2373b1997-05-07 23:51:07 +000052 fprintf(fp, "grammar _PyParser_Grammar = {\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000053 fprintf(fp, "\t%d,\n", g->g_ndfas);
54 fprintf(fp, "\tdfas,\n");
55 fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
56 fprintf(fp, "\t%d\n", g->g_start);
57 fprintf(fp, "};\n");
58}
59
60void
61printnonterminals(g, fp)
62 grammar *g;
63 FILE *fp;
64{
65 dfa *d;
66 int i;
67
68 d = g->g_dfa;
69 for (i = g->g_ndfas; --i >= 0; d++)
70 fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
71}
72
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073static void
74printarcs(i, d, fp)
75 int i;
76 dfa *d;
77 FILE *fp;
78{
79 arc *a;
80 state *s;
81 int j, k;
82
83 s = d->d_state;
84 for (j = 0; j < d->d_nstates; j++, s++) {
85 fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
86 i, j, s->s_narcs);
87 a = s->s_arc;
88 for (k = 0; k < s->s_narcs; k++, a++)
89 fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
90 fprintf(fp, "};\n");
91 }
92}
93
94static void
95printstates(g, fp)
96 grammar *g;
97 FILE *fp;
98{
99 state *s;
100 dfa *d;
101 int i, j;
102
103 d = g->g_dfa;
104 for (i = 0; i < g->g_ndfas; i++, d++) {
105 printarcs(i, d, fp);
106 fprintf(fp, "static state states_%d[%d] = {\n",
107 i, d->d_nstates);
108 s = d->d_state;
109 for (j = 0; j < d->d_nstates; j++, s++)
110 fprintf(fp, "\t{%d, arcs_%d_%d},\n",
111 s->s_narcs, i, j);
112 fprintf(fp, "};\n");
113 }
114}
115
116static void
117printdfas(g, fp)
118 grammar *g;
119 FILE *fp;
120{
121 dfa *d;
122 int i, j;
123
124 printstates(g, fp);
125 fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
126 d = g->g_dfa;
127 for (i = 0; i < g->g_ndfas; i++, d++) {
128 fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n",
129 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
130 fprintf(fp, "\t \"");
131 for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
132 fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
133 fprintf(fp, "\"},\n");
134 }
135 fprintf(fp, "};\n");
136}
137
138static void
139printlabels(g, fp)
140 grammar *g;
141 FILE *fp;
142{
143 label *l;
144 int i;
145
146 fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
147 l = g->g_ll.ll_label;
148 for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
149 if (l->lb_str == NULL)
150 fprintf(fp, "\t{%d, 0},\n", l->lb_type);
151 else
152 fprintf(fp, "\t{%d, \"%s\"},\n",
153 l->lb_type, l->lb_str);
154 }
155 fprintf(fp, "};\n");
156}