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