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 | /* Parser implementation */ |
| 3 | |
| 4 | /* For a description, see the comments at end of this file */ |
| 5 | |
| 6 | /* XXX To do: error recovery */ |
| 7 | |
Tim Peters | 1ca1296 | 2001-12-04 03:18:48 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | #include "token.h" |
| 11 | #include "grammar.h" |
| 12 | #include "node.h" |
| 13 | #include "parser.h" |
| 14 | #include "errcode.h" |
| 15 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 17 | #ifdef Py_DEBUG |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 18 | extern int Py_DebugFlag; |
| 19 | #define D(x) if (!Py_DebugFlag); else x |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 20 | #else |
| 21 | #define D(x) |
| 22 | #endif |
| 23 | |
| 24 | |
| 25 | /* STACK DATA TYPE */ |
| 26 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 27 | static void s_reset(stack *); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
| 29 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 30 | s_reset(stack *s) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | { |
| 32 | s->s_top = &s->s_base[MAXSTACK]; |
| 33 | } |
| 34 | |
| 35 | #define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK]) |
| 36 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 38 | s_push(register stack *s, dfa *d, node *parent) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | { |
| 40 | register stackentry *top; |
| 41 | if (s->s_top == s->s_base) { |
| 42 | fprintf(stderr, "s_push: parser stack overflow\n"); |
Guido van Rossum | e3c3b27 | 2000-10-02 10:21:59 +0000 | [diff] [blame] | 43 | return E_NOMEM; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | } |
| 45 | top = --s->s_top; |
| 46 | top->s_dfa = d; |
| 47 | top->s_parent = parent; |
| 48 | top->s_state = 0; |
| 49 | return 0; |
| 50 | } |
| 51 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 52 | #ifdef Py_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 55 | s_pop(register stack *s) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | { |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 57 | if (s_empty(s)) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 58 | Py_FatalError("s_pop: parser stack underflow -- FATAL"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | s->s_top++; |
| 60 | } |
| 61 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 62 | #else /* !Py_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | |
| 64 | #define s_pop(s) (s)->s_top++ |
| 65 | |
| 66 | #endif |
| 67 | |
| 68 | |
| 69 | /* PARSER CREATION */ |
| 70 | |
| 71 | parser_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 72 | PyParser_New(grammar *g, int start) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | { |
| 74 | parser_state *ps; |
| 75 | |
| 76 | if (!g->g_accel) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 77 | PyGrammar_AddAccelerators(g); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | ps = (parser_state *)PyMem_MALLOC(sizeof(parser_state)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | if (ps == NULL) |
| 80 | return NULL; |
| 81 | ps->p_grammar = g; |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 82 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
| 83 | ps->p_flags = 0; |
Neil Schemenauer | c155dd4 | 2002-03-22 23:38:11 +0000 | [diff] [blame] | 84 | #endif |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 85 | ps->p_tree = PyNode_New(start); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | if (ps->p_tree == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 87 | PyMem_FREE(ps); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
| 90 | s_reset(&ps->p_stack); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 91 | (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | return ps; |
| 93 | } |
| 94 | |
| 95 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 96 | PyParser_Delete(parser_state *ps) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | { |
Guido van Rossum | 99f02d4 | 1990-11-18 17:38:42 +0000 | [diff] [blame] | 98 | /* NB If you want to save the parse tree, |
| 99 | you must set p_tree to NULL before calling delparser! */ |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 100 | PyNode_Free(ps->p_tree); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 101 | PyMem_FREE(ps); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | /* PARSER STACK OPERATIONS */ |
| 106 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | static int |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 108 | shift(register stack *s, int type, char *str, int newstate, int lineno, int col_offset) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | { |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 110 | int err; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | assert(!s_empty(s)); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 112 | err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset); |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 113 | if (err) |
| 114 | return err; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | s->s_top->s_state = newstate; |
| 116 | return 0; |
| 117 | } |
| 118 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | static int |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 120 | push(register stack *s, int type, dfa *d, int newstate, int lineno, int col_offset) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | { |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 122 | int err; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | register node *n; |
| 124 | n = s->s_top->s_parent; |
| 125 | assert(!s_empty(s)); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 126 | err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset); |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 127 | if (err) |
| 128 | return err; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 129 | s->s_top->s_state = newstate; |
| 130 | return s_push(s, d, CHILD(n, NCH(n)-1)); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* PARSER PROPER */ |
| 135 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | static int |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 137 | classify(parser_state *ps, int type, char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | { |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 139 | grammar *g = ps->p_grammar; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | register int n = g->g_ll.ll_nlabels; |
| 141 | |
| 142 | if (type == NAME) { |
| 143 | register char *s = str; |
| 144 | register label *l = g->g_ll.ll_label; |
| 145 | register int i; |
| 146 | for (i = n; i > 0; i--, l++) { |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 147 | if (l->lb_type != NAME || l->lb_str == NULL || |
| 148 | l->lb_str[0] != s[0] || |
| 149 | strcmp(l->lb_str, s) != 0) |
| 150 | continue; |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 151 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 152 | /* Leaving this in as an example */ |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 153 | if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) { |
| 154 | if (s[0] == 'w' && strcmp(s, "with") == 0) |
| 155 | break; /* not a keyword yet */ |
| 156 | else if (s[0] == 'a' && strcmp(s, "as") == 0) |
| 157 | break; /* not a keyword yet */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | } |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 159 | #endif |
| 160 | D(printf("It's a keyword\n")); |
| 161 | return n - i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | { |
| 166 | register label *l = g->g_ll.ll_label; |
| 167 | register int i; |
| 168 | for (i = n; i > 0; i--, l++) { |
| 169 | if (l->lb_type == type && l->lb_str == NULL) { |
| 170 | D(printf("It's a token we know\n")); |
| 171 | return n - i; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | D(printf("Illegal token\n")); |
| 177 | return -1; |
| 178 | } |
| 179 | |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 180 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 181 | /* Leaving this in as an example */ |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 182 | static void |
| 183 | future_hack(parser_state *ps) |
| 184 | { |
| 185 | node *n = ps->p_stack.s_top->s_parent; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 186 | node *ch, *cch; |
Guido van Rossum | 3c03323 | 2001-07-19 15:27:45 +0000 | [diff] [blame] | 187 | int i; |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 188 | |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 189 | /* from __future__ import ..., must have at least 4 children */ |
| 190 | n = CHILD(n, 0); |
| 191 | if (NCH(n) < 4) |
| 192 | return; |
| 193 | ch = CHILD(n, 0); |
| 194 | if (STR(ch) == NULL || strcmp(STR(ch), "from") != 0) |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 195 | return; |
| 196 | ch = CHILD(n, 1); |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 197 | if (NCH(ch) == 1 && STR(CHILD(ch, 0)) && |
| 198 | strcmp(STR(CHILD(ch, 0)), "__future__") != 0) |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 199 | return; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 200 | ch = CHILD(n, 3); |
| 201 | /* ch can be a star, a parenthesis or import_as_names */ |
| 202 | if (TYPE(ch) == STAR) |
| 203 | return; |
| 204 | if (TYPE(ch) == LPAR) |
| 205 | ch = CHILD(n, 4); |
| 206 | |
| 207 | for (i = 0; i < NCH(ch); i += 2) { |
| 208 | cch = CHILD(ch, i); |
| 209 | if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME && |
| 210 | strcmp(STR(CHILD(cch, 0)), "with_statement") == 0) { |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 211 | ps->p_flags |= CO_FUTURE_WITH_STATEMENT; |
Guido van Rossum | 3c03323 | 2001-07-19 15:27:45 +0000 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | } |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 215 | } |
Neil Schemenauer | c155dd4 | 2002-03-22 23:38:11 +0000 | [diff] [blame] | 216 | #endif /* future keyword */ |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 219 | PyParser_AddToken(register parser_state *ps, register int type, char *str, |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 220 | int lineno, int col_offset, int *expected_ret) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | { |
| 222 | register int ilabel; |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 223 | int err; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 225 | D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | |
| 227 | /* Find out which label this token is */ |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 228 | ilabel = classify(ps, type, str); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | if (ilabel < 0) |
| 230 | return E_SYNTAX; |
| 231 | |
| 232 | /* Loop until the token is shifted or an error occurred */ |
| 233 | for (;;) { |
| 234 | /* Fetch the current dfa and state */ |
| 235 | register dfa *d = ps->p_stack.s_top->s_dfa; |
| 236 | register state *s = &d->d_state[ps->p_stack.s_top->s_state]; |
| 237 | |
| 238 | D(printf(" DFA '%s', state %d:", |
| 239 | d->d_name, ps->p_stack.s_top->s_state)); |
| 240 | |
| 241 | /* Check accelerator */ |
| 242 | if (s->s_lower <= ilabel && ilabel < s->s_upper) { |
| 243 | register int x = s->s_accel[ilabel - s->s_lower]; |
| 244 | if (x != -1) { |
| 245 | if (x & (1<<7)) { |
| 246 | /* Push non-terminal */ |
| 247 | int nt = (x >> 8) + NT_OFFSET; |
| 248 | int arrow = x & ((1<<7)-1); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 249 | dfa *d1 = PyGrammar_FindDFA( |
| 250 | ps->p_grammar, nt); |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 251 | if ((err = push(&ps->p_stack, nt, d1, |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 252 | arrow, lineno, col_offset)) > 0) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 253 | D(printf(" MemError: push\n")); |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 254 | return err; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 255 | } |
| 256 | D(printf(" Push ...\n")); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | /* Shift the token */ |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 261 | if ((err = shift(&ps->p_stack, type, str, |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 262 | x, lineno, col_offset)) > 0) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | D(printf(" MemError: shift.\n")); |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 264 | return err; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | } |
| 266 | D(printf(" Shift.\n")); |
| 267 | /* Pop while we are in an accept-only state */ |
| 268 | while (s = &d->d_state |
| 269 | [ps->p_stack.s_top->s_state], |
| 270 | s->s_accept && s->s_narcs == 1) { |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 271 | D(printf(" DFA '%s', state %d: " |
| 272 | "Direct pop.\n", |
| 273 | d->d_name, |
| 274 | ps->p_stack.s_top->s_state)); |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 275 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 276 | if (d->d_name[0] == 'i' && |
| 277 | strcmp(d->d_name, |
| 278 | "import_stmt") == 0) |
| 279 | future_hack(ps); |
Neil Schemenauer | c155dd4 | 2002-03-22 23:38:11 +0000 | [diff] [blame] | 280 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | s_pop(&ps->p_stack); |
| 282 | if (s_empty(&ps->p_stack)) { |
| 283 | D(printf(" ACCEPT.\n")); |
| 284 | return E_DONE; |
| 285 | } |
| 286 | d = ps->p_stack.s_top->s_dfa; |
| 287 | } |
| 288 | return E_OK; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if (s->s_accept) { |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 293 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 294 | if (d->d_name[0] == 'i' && |
| 295 | strcmp(d->d_name, "import_stmt") == 0) |
| 296 | future_hack(ps); |
Neil Schemenauer | c155dd4 | 2002-03-22 23:38:11 +0000 | [diff] [blame] | 297 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | /* Pop this dfa and try again */ |
| 299 | s_pop(&ps->p_stack); |
| 300 | D(printf(" Pop ...\n")); |
| 301 | if (s_empty(&ps->p_stack)) { |
| 302 | D(printf(" Error: bottom of stack.\n")); |
| 303 | return E_SYNTAX; |
| 304 | } |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | /* Stuck, report syntax error */ |
| 309 | D(printf(" Error.\n")); |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 310 | if (expected_ret) { |
| 311 | if (s->s_lower == s->s_upper - 1) { |
| 312 | /* Only one possible expected token */ |
| 313 | *expected_ret = ps->p_grammar-> |
| 314 | g_ll.ll_label[s->s_lower].lb_type; |
| 315 | } |
| 316 | else |
| 317 | *expected_ret = -1; |
| 318 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | return E_SYNTAX; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 324 | #ifdef Py_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | |
| 326 | /* DEBUG OUTPUT */ |
| 327 | |
| 328 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 329 | dumptree(grammar *g, node *n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 330 | { |
| 331 | int i; |
| 332 | |
| 333 | if (n == NULL) |
| 334 | printf("NIL"); |
| 335 | else { |
| 336 | label l; |
| 337 | l.lb_type = TYPE(n); |
Guido van Rossum | cf7448b | 1992-09-03 20:46:37 +0000 | [diff] [blame] | 338 | l.lb_str = STR(n); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 339 | printf("%s", PyGrammar_LabelRepr(&l)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 340 | if (ISNONTERMINAL(TYPE(n))) { |
| 341 | printf("("); |
| 342 | for (i = 0; i < NCH(n); i++) { |
| 343 | if (i > 0) |
| 344 | printf(","); |
| 345 | dumptree(g, CHILD(n, i)); |
| 346 | } |
| 347 | printf(")"); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 353 | showtree(grammar *g, node *n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | { |
| 355 | int i; |
| 356 | |
| 357 | if (n == NULL) |
| 358 | return; |
| 359 | if (ISNONTERMINAL(TYPE(n))) { |
| 360 | for (i = 0; i < NCH(n); i++) |
| 361 | showtree(g, CHILD(n, i)); |
| 362 | } |
| 363 | else if (ISTERMINAL(TYPE(n))) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 364 | printf("%s", _PyParser_TokenNames[TYPE(n)]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | if (TYPE(n) == NUMBER || TYPE(n) == NAME) |
| 366 | printf("(%s)", STR(n)); |
| 367 | printf(" "); |
| 368 | } |
| 369 | else |
| 370 | printf("? "); |
| 371 | } |
| 372 | |
| 373 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 374 | printtree(parser_state *ps) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 375 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 376 | if (Py_DebugFlag) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | printf("Parse tree:\n"); |
| 378 | dumptree(ps->p_grammar, ps->p_tree); |
| 379 | printf("\n"); |
| 380 | printf("Tokens:\n"); |
| 381 | showtree(ps->p_grammar, ps->p_tree); |
| 382 | printf("\n"); |
| 383 | } |
| 384 | printf("Listing:\n"); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 385 | PyNode_ListTree(ps->p_tree); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 386 | printf("\n"); |
| 387 | } |
| 388 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 389 | #endif /* Py_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | |
| 391 | /* |
| 392 | |
| 393 | Description |
| 394 | ----------- |
| 395 | |
| 396 | The parser's interface is different than usual: the function addtoken() |
| 397 | must be called for each token in the input. This makes it possible to |
| 398 | turn it into an incremental parsing system later. The parsing system |
| 399 | constructs a parse tree as it goes. |
| 400 | |
| 401 | A parsing rule is represented as a Deterministic Finite-state Automaton |
| 402 | (DFA). A node in a DFA represents a state of the parser; an arc represents |
| 403 | a transition. Transitions are either labeled with terminal symbols or |
| 404 | with non-terminals. When the parser decides to follow an arc labeled |
| 405 | with a non-terminal, it is invoked recursively with the DFA representing |
| 406 | the parsing rule for that as its initial state; when that DFA accepts, |
| 407 | the parser that invoked it continues. The parse tree constructed by the |
| 408 | recursively called parser is inserted as a child in the current parse tree. |
| 409 | |
| 410 | The DFA's can be constructed automatically from a more conventional |
| 411 | language description. An extended LL(1) grammar (ELL(1)) is suitable. |
| 412 | Certain restrictions make the parser's life easier: rules that can produce |
| 413 | the empty string should be outlawed (there are other ways to put loops |
| 414 | or optional parts in the language). To avoid the need to construct |
| 415 | FIRST sets, we can require that all but the last alternative of a rule |
| 416 | (really: arc going out of a DFA's state) must begin with a terminal |
| 417 | symbol. |
| 418 | |
| 419 | As an example, consider this grammar: |
| 420 | |
| 421 | expr: term (OP term)* |
| 422 | term: CONSTANT | '(' expr ')' |
| 423 | |
| 424 | The DFA corresponding to the rule for expr is: |
| 425 | |
| 426 | ------->.---term-->.-------> |
| 427 | ^ | |
| 428 | | | |
| 429 | \----OP----/ |
| 430 | |
| 431 | The parse tree generated for the input a+b is: |
| 432 | |
| 433 | (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b))) |
| 434 | |
| 435 | */ |