Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Print a bunch of C initializers that represent a grammar */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | #include "grammar.h" |
| 6 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 7 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 8 | static void printarcs(int, dfa *, FILE *); |
| 9 | static void printstates(grammar *, FILE *); |
| 10 | static void printdfas(grammar *, FILE *); |
| 11 | static void printlabels(grammar *, FILE *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | |
| 13 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 14 | printgrammar(grammar *g, FILE *fp) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | { |
| 16 | fprintf(fp, "#include \"pgenheaders.h\"\n"); |
| 17 | fprintf(fp, "#include \"grammar.h\"\n"); |
| 18 | printdfas(g, fp); |
| 19 | printlabels(g, fp); |
Guido van Rossum | ee2373b | 1997-05-07 23:51:07 +0000 | [diff] [blame] | 20 | fprintf(fp, "grammar _PyParser_Grammar = {\n"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | fprintf(fp, "\t%d,\n", g->g_ndfas); |
| 22 | fprintf(fp, "\tdfas,\n"); |
| 23 | fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels); |
| 24 | fprintf(fp, "\t%d\n", g->g_start); |
| 25 | fprintf(fp, "};\n"); |
| 26 | } |
| 27 | |
| 28 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 29 | printnonterminals(grammar *g, FILE *fp) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | { |
| 31 | dfa *d; |
| 32 | int i; |
| 33 | |
| 34 | d = g->g_dfa; |
| 35 | for (i = g->g_ndfas; --i >= 0; d++) |
| 36 | fprintf(fp, "#define %s %d\n", d->d_name, d->d_type); |
| 37 | } |
| 38 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 40 | printarcs(int i, dfa *d, FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | { |
| 42 | arc *a; |
| 43 | state *s; |
| 44 | int j, k; |
| 45 | |
| 46 | s = d->d_state; |
| 47 | for (j = 0; j < d->d_nstates; j++, s++) { |
| 48 | fprintf(fp, "static arc arcs_%d_%d[%d] = {\n", |
| 49 | i, j, s->s_narcs); |
| 50 | a = s->s_arc; |
| 51 | for (k = 0; k < s->s_narcs; k++, a++) |
| 52 | fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow); |
| 53 | fprintf(fp, "};\n"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 58 | printstates(grammar *g, FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | { |
| 60 | state *s; |
| 61 | dfa *d; |
| 62 | int i, j; |
| 63 | |
| 64 | d = g->g_dfa; |
| 65 | for (i = 0; i < g->g_ndfas; i++, d++) { |
| 66 | printarcs(i, d, fp); |
| 67 | fprintf(fp, "static state states_%d[%d] = {\n", |
| 68 | i, d->d_nstates); |
| 69 | s = d->d_state; |
| 70 | for (j = 0; j < d->d_nstates; j++, s++) |
| 71 | fprintf(fp, "\t{%d, arcs_%d_%d},\n", |
| 72 | s->s_narcs, i, j); |
| 73 | fprintf(fp, "};\n"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 78 | printdfas(grammar *g, FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | { |
| 80 | dfa *d; |
| 81 | int i, j; |
| 82 | |
| 83 | printstates(g, fp); |
| 84 | fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas); |
| 85 | d = g->g_dfa; |
| 86 | for (i = 0; i < g->g_ndfas; i++, d++) { |
| 87 | fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n", |
| 88 | d->d_type, d->d_name, d->d_initial, d->d_nstates, i); |
| 89 | fprintf(fp, "\t \""); |
| 90 | for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++) |
| 91 | fprintf(fp, "\\%03o", d->d_first[j] & 0xff); |
| 92 | fprintf(fp, "\"},\n"); |
| 93 | } |
| 94 | fprintf(fp, "};\n"); |
| 95 | } |
| 96 | |
| 97 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 98 | printlabels(grammar *g, FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | { |
| 100 | label *l; |
| 101 | int i; |
| 102 | |
| 103 | fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels); |
| 104 | l = g->g_ll.ll_label; |
| 105 | for (i = g->g_ll.ll_nlabels; --i >= 0; l++) { |
| 106 | if (l->lb_str == NULL) |
| 107 | fprintf(fp, "\t{%d, 0},\n", l->lb_type); |
| 108 | else |
| 109 | fprintf(fp, "\t{%d, \"%s\"},\n", |
| 110 | l->lb_type, l->lb_str); |
| 111 | } |
| 112 | fprintf(fp, "};\n"); |
| 113 | } |