Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | bab9d03 | 1992-04-05 14:26:55 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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 not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Print a bunch of C initializers that represent a grammar */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | #include "grammar.h" |
| 29 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | /* Forward */ |
| 31 | static void printarcs PROTO((int, dfa *, FILE *)); |
| 32 | static void printstates PROTO((grammar *, FILE *)); |
| 33 | static void printdfas PROTO((grammar *, FILE *)); |
| 34 | static void printlabels PROTO((grammar *, FILE *)); |
| 35 | |
| 36 | void |
| 37 | printgrammar(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 | |
| 53 | void |
| 54 | printnonterminals(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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | static void |
| 67 | printarcs(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 | |
| 87 | static void |
| 88 | printstates(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 | |
| 109 | static void |
| 110 | printdfas(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 | |
| 131 | static void |
| 132 | printlabels(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 | } |