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 | /* Grammar implementation */ |
| 3 | |
Tim Peters | 1ca1296 | 2001-12-04 03:18:48 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | #include "pgenheaders.h" |
| 6 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | #include "token.h" |
| 10 | #include "grammar.h" |
| 11 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 12 | extern int Py_DebugFlag; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13 | |
| 14 | grammar * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 15 | newgrammar(int start) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 16 | { |
| 17 | grammar *g; |
| 18 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 19 | g = PyMem_NEW(grammar, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 20 | if (g == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 21 | Py_FatalError("no mem for new grammar"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 22 | g->g_ndfas = 0; |
| 23 | g->g_dfa = NULL; |
| 24 | g->g_start = start; |
| 25 | g->g_ll.ll_nlabels = 0; |
| 26 | g->g_ll.ll_label = NULL; |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 27 | g->g_accel = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | return g; |
| 29 | } |
| 30 | |
| 31 | dfa * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 32 | adddfa(grammar *g, int type, char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | { |
| 34 | dfa *d; |
| 35 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 36 | PyMem_RESIZE(g->g_dfa, dfa, g->g_ndfas + 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | if (g->g_dfa == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 38 | Py_FatalError("no mem to resize dfa in adddfa"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | d = &g->g_dfa[g->g_ndfas++]; |
| 40 | d->d_type = type; |
| 41 | d->d_name = name; |
| 42 | d->d_nstates = 0; |
| 43 | d->d_state = NULL; |
| 44 | d->d_initial = -1; |
| 45 | d->d_first = NULL; |
| 46 | return d; /* Only use while fresh! */ |
| 47 | } |
| 48 | |
| 49 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 50 | addstate(dfa *d) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | { |
| 52 | state *s; |
| 53 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 54 | PyMem_RESIZE(d->d_state, state, d->d_nstates + 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | if (d->d_state == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 56 | Py_FatalError("no mem to resize state in addstate"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | s = &d->d_state[d->d_nstates++]; |
| 58 | s->s_narcs = 0; |
| 59 | s->s_arc = NULL; |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 60 | s->s_lower = 0; |
| 61 | s->s_upper = 0; |
| 62 | s->s_accel = NULL; |
| 63 | s->s_accept = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | return s - d->d_state; |
| 65 | } |
| 66 | |
| 67 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 68 | addarc(dfa *d, int from, int to, int lbl) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 69 | { |
| 70 | state *s; |
| 71 | arc *a; |
| 72 | |
| 73 | assert(0 <= from && from < d->d_nstates); |
| 74 | assert(0 <= to && to < d->d_nstates); |
| 75 | |
| 76 | s = &d->d_state[from]; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 77 | PyMem_RESIZE(s->s_arc, arc, s->s_narcs + 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | if (s->s_arc == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 79 | Py_FatalError("no mem to resize arc list in addarc"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | a = &s->s_arc[s->s_narcs++]; |
| 81 | a->a_lbl = lbl; |
| 82 | a->a_arrow = to; |
| 83 | } |
| 84 | |
| 85 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 86 | addlabel(labellist *ll, int type, char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | { |
| 88 | int i; |
| 89 | label *lb; |
| 90 | |
| 91 | for (i = 0; i < ll->ll_nlabels; i++) { |
| 92 | if (ll->ll_label[i].lb_type == type && |
| 93 | strcmp(ll->ll_label[i].lb_str, str) == 0) |
| 94 | return i; |
| 95 | } |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 96 | PyMem_RESIZE(ll->ll_label, label, ll->ll_nlabels + 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | if (ll->ll_label == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 98 | Py_FatalError("no mem to resize labellist in addlabel"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | lb = &ll->ll_label[ll->ll_nlabels++]; |
| 100 | lb->lb_type = type; |
| 101 | lb->lb_str = str; /* XXX strdup(str) ??? */ |
| 102 | return lb - ll->ll_label; |
| 103 | } |
| 104 | |
| 105 | /* Same, but rather dies than adds */ |
| 106 | |
| 107 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 108 | findlabel(labellist *ll, int type, char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | { |
| 110 | int i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | |
| 112 | for (i = 0; i < ll->ll_nlabels; i++) { |
| 113 | if (ll->ll_label[i].lb_type == type /*&& |
| 114 | strcmp(ll->ll_label[i].lb_str, str) == 0*/) |
| 115 | return i; |
| 116 | } |
| 117 | fprintf(stderr, "Label %d/'%s' not found\n", type, str); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 118 | Py_FatalError("grammar.c:findlabel()"); |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 119 | return 0; /* Make gcc -Wall happy */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 122 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 123 | static void translabel(grammar *, label *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 124 | |
| 125 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 126 | translatelabels(grammar *g) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | { |
| 128 | int i; |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 130 | #ifdef Py_DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 131 | printf("Translating labels ...\n"); |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 132 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 133 | /* Don't translate EMPTY */ |
| 134 | for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++) |
| 135 | translabel(g, &g->g_ll.ll_label[i]); |
| 136 | } |
| 137 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 139 | translabel(grammar *g, label *lb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | { |
| 141 | int i; |
| 142 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 143 | if (Py_DebugFlag) |
| 144 | printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | |
| 146 | if (lb->lb_type == NAME) { |
| 147 | for (i = 0; i < g->g_ndfas; i++) { |
| 148 | if (strcmp(lb->lb_str, g->g_dfa[i].d_name) == 0) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 149 | if (Py_DebugFlag) |
| 150 | printf( |
| 151 | "Label %s is non-terminal %d.\n", |
| 152 | lb->lb_str, |
| 153 | g->g_dfa[i].d_type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | lb->lb_type = g->g_dfa[i].d_type; |
| 155 | lb->lb_str = NULL; |
| 156 | return; |
| 157 | } |
| 158 | } |
| 159 | for (i = 0; i < (int)N_TOKENS; i++) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 160 | if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) { |
| 161 | if (Py_DebugFlag) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | printf("Label %s is terminal %d.\n", |
| 163 | lb->lb_str, i); |
| 164 | lb->lb_type = i; |
| 165 | lb->lb_str = NULL; |
| 166 | return; |
| 167 | } |
| 168 | } |
| 169 | printf("Can't translate NAME label '%s'\n", lb->lb_str); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (lb->lb_type == STRING) { |
Guido van Rossum | bb3649e | 1998-04-10 22:09:39 +0000 | [diff] [blame] | 174 | if (isalpha((int)(lb->lb_str[1])) || lb->lb_str[1] == '_') { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 175 | char *p; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 176 | if (Py_DebugFlag) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | printf("Label %s is a keyword\n", lb->lb_str); |
| 178 | lb->lb_type = NAME; |
| 179 | lb->lb_str++; |
| 180 | p = strchr(lb->lb_str, '\''); |
| 181 | if (p) |
| 182 | *p = '\0'; |
| 183 | } |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 184 | else if (lb->lb_str[2] == lb->lb_str[0]) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 185 | int type = (int) PyToken_OneChar(lb->lb_str[1]); |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 186 | if (type != OP) { |
| 187 | lb->lb_type = type; |
| 188 | lb->lb_str = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | } |
| 190 | else |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 191 | printf("Unknown OP label %s\n", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | lb->lb_str); |
| 193 | } |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 194 | else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 195 | int type = (int) PyToken_TwoChars(lb->lb_str[1], |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 196 | lb->lb_str[2]); |
| 197 | if (type != OP) { |
| 198 | lb->lb_type = type; |
| 199 | lb->lb_str = NULL; |
| 200 | } |
| 201 | else |
| 202 | printf("Unknown OP label %s\n", |
| 203 | lb->lb_str); |
| 204 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 205 | else if (lb->lb_str[2] && lb->lb_str[3] && lb->lb_str[4] == lb->lb_str[0]) { |
| 206 | int type = (int) PyToken_ThreeChars(lb->lb_str[1], |
| 207 | lb->lb_str[2], |
| 208 | lb->lb_str[3]); |
| 209 | if (type != OP) { |
| 210 | lb->lb_type = type; |
| 211 | lb->lb_str = NULL; |
| 212 | } |
| 213 | else |
| 214 | printf("Unknown OP label %s\n", |
| 215 | lb->lb_str); |
| 216 | } |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 217 | else |
| 218 | printf("Can't translate STRING label %s\n", |
| 219 | lb->lb_str); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | } |
| 221 | else |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 222 | printf("Can't translate label '%s'\n", |
| 223 | PyGrammar_LabelRepr(lb)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | } |