Mercurial > cpython-lru_cache-weakref
annotate Parser/printgrammar.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 9eaa2caed1c3 |
| children | 916fb024ce94 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2060
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Print a bunch of C initializers that represent a grammar */ |
| 33 | |
| 86 | 34 #include "pgenheaders.h" |
| 35 #include "grammar.h" | |
| 36 | |
| 37 /* Forward */ | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
38 static void printarcs Py_PROTO((int, dfa *, FILE *)); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
39 static void printstates Py_PROTO((grammar *, FILE *)); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
40 static void printdfas Py_PROTO((grammar *, FILE *)); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
41 static void printlabels Py_PROTO((grammar *, FILE *)); |
| 6 | 42 |
| 86 | 43 void |
| 44 printgrammar(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); | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5379
diff
changeset
|
52 fprintf(fp, "grammar _PyParser_Grammar = {\n"); |
| 86 | 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"); | |
| 58 } | |
| 59 | |
| 60 void | |
| 61 printnonterminals(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 } | |
| 6 | 72 |
| 73 static void | |
| 74 printarcs(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 | |
| 94 static void | |
| 95 printstates(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 | |
| 116 static void | |
| 117 printdfas(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 | |
| 138 static void | |
| 139 printlabels(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 } |
