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