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