Added all documentation.
[python/dscho.git] / Parser / printgrammar.c
blob705cbcb2265e3e0215a27834adadf0903dde211a
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Print a bunch of C initializers that represent a grammar */
34 #include "pgenheaders.h"
35 #include "grammar.h"
37 /* Forward */
38 static void printarcs Py_PROTO((int, dfa *, FILE *));
39 static void printstates Py_PROTO((grammar *, FILE *));
40 static void printdfas Py_PROTO((grammar *, FILE *));
41 static void printlabels Py_PROTO((grammar *, FILE *));
43 void
44 printgrammar(g, fp)
45 grammar *g;
46 FILE *fp;
48 fprintf(fp, "#include \"pgenheaders.h\"\n");
49 fprintf(fp, "#include \"grammar.h\"\n");
50 printdfas(g, fp);
51 printlabels(g, fp);
52 fprintf(fp, "grammar _PyParser_Grammar = {\n");
53 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");
60 void
61 printnonterminals(g, fp)
62 grammar *g;
63 FILE *fp;
65 dfa *d;
66 int i;
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);
73 static void
74 printarcs(i, d, fp)
75 int i;
76 dfa *d;
77 FILE *fp;
79 arc *a;
80 state *s;
81 int j, k;
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");
94 static void
95 printstates(g, fp)
96 grammar *g;
97 FILE *fp;
99 state *s;
100 dfa *d;
101 int i, j;
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");
116 static void
117 printdfas(g, fp)
118 grammar *g;
119 FILE *fp;
121 dfa *d;
122 int i, j;
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");
135 fprintf(fp, "};\n");
138 static void
139 printlabels(g, fp)
140 grammar *g;
141 FILE *fp;
143 label *l;
144 int i;
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);
155 fprintf(fp, "};\n");