Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b9f8d6e | 1995-01-04 19:08:09 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
| 20 | |
| 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. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Print a bunch of C initializers that represent a grammar */ |
| 33 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | #include "grammar.h" |
| 36 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | /* Forward */ |
| 38 | static void printarcs PROTO((int, dfa *, FILE *)); |
| 39 | static void printstates PROTO((grammar *, FILE *)); |
| 40 | static void printdfas PROTO((grammar *, FILE *)); |
| 41 | static void printlabels PROTO((grammar *, FILE *)); |
| 42 | |
| 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); |
| 52 | fprintf(fp, "grammar gram = {\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"); |
| 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 | } |
| 72 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | } |