blob: ead876fcc04fcb15107f7e2967057a49642be310 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Print a bunch of C initializers that represent a grammar */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028#include "grammar.h"
29
Guido van Rossum3f5da241990-12-20 15:06:42 +000030/* Forward */
31static void printarcs PROTO((int, dfa *, FILE *));
32static void printstates PROTO((grammar *, FILE *));
33static void printdfas PROTO((grammar *, FILE *));
34static void printlabels PROTO((grammar *, FILE *));
35
36void
37printgrammar(g, fp)
38 grammar *g;
39 FILE *fp;
40{
41 fprintf(fp, "#include \"pgenheaders.h\"\n");
42 fprintf(fp, "#include \"grammar.h\"\n");
43 printdfas(g, fp);
44 printlabels(g, fp);
45 fprintf(fp, "grammar gram = {\n");
46 fprintf(fp, "\t%d,\n", g->g_ndfas);
47 fprintf(fp, "\tdfas,\n");
48 fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
49 fprintf(fp, "\t%d\n", g->g_start);
50 fprintf(fp, "};\n");
51}
52
53void
54printnonterminals(g, fp)
55 grammar *g;
56 FILE *fp;
57{
58 dfa *d;
59 int i;
60
61 d = g->g_dfa;
62 for (i = g->g_ndfas; --i >= 0; d++)
63 fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
64}
65
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066static void
67printarcs(i, d, fp)
68 int i;
69 dfa *d;
70 FILE *fp;
71{
72 arc *a;
73 state *s;
74 int j, k;
75
76 s = d->d_state;
77 for (j = 0; j < d->d_nstates; j++, s++) {
78 fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
79 i, j, s->s_narcs);
80 a = s->s_arc;
81 for (k = 0; k < s->s_narcs; k++, a++)
82 fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
83 fprintf(fp, "};\n");
84 }
85}
86
87static void
88printstates(g, fp)
89 grammar *g;
90 FILE *fp;
91{
92 state *s;
93 dfa *d;
94 int i, j;
95
96 d = g->g_dfa;
97 for (i = 0; i < g->g_ndfas; i++, d++) {
98 printarcs(i, d, fp);
99 fprintf(fp, "static state states_%d[%d] = {\n",
100 i, d->d_nstates);
101 s = d->d_state;
102 for (j = 0; j < d->d_nstates; j++, s++)
103 fprintf(fp, "\t{%d, arcs_%d_%d},\n",
104 s->s_narcs, i, j);
105 fprintf(fp, "};\n");
106 }
107}
108
109static void
110printdfas(g, fp)
111 grammar *g;
112 FILE *fp;
113{
114 dfa *d;
115 int i, j;
116
117 printstates(g, fp);
118 fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
119 d = g->g_dfa;
120 for (i = 0; i < g->g_ndfas; i++, d++) {
121 fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n",
122 d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
123 fprintf(fp, "\t \"");
124 for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
125 fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
126 fprintf(fp, "\"},\n");
127 }
128 fprintf(fp, "};\n");
129}
130
131static void
132printlabels(g, fp)
133 grammar *g;
134 FILE *fp;
135{
136 label *l;
137 int i;
138
139 fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
140 l = g->g_ll.ll_label;
141 for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
142 if (l->lb_str == NULL)
143 fprintf(fp, "\t{%d, 0},\n", l->lb_type);
144 else
145 fprintf(fp, "\t{%d, \"%s\"},\n",
146 l->lb_type, l->lb_str);
147 }
148 fprintf(fp, "};\n");
149}