Mercurial > cpython-lru_cache-weakref
annotate Parser/parser.c @ 17581:c1937faabb23 2.1
Added support for optional explanation parameter to the \versionadded
macro.
Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most
of the work in a helper function, with the do_cmd_*() wrappers just supplying
a portion of the replacement text.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 18 Apr 2001 17:29:14 +0000 |
| parents | 5336946b6495 |
| children | 59446d0f8341 |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Parser implementation */ |
| 3 | |
| 4 /* For a description, see the comments at end of this file */ | |
| 5 | |
| 6 /* XXX To do: error recovery */ | |
| 7 | |
| 86 | 8 #include "pgenheaders.h" |
| 6 | 9 #include "assert.h" |
| 10 #include "token.h" | |
| 11 #include "grammar.h" | |
| 12 #include "node.h" | |
| 13 #include "parser.h" | |
| 14 #include "errcode.h" | |
| 15 | |
| 16 | |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
17 #ifdef Py_DEBUG |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
18 extern int Py_DebugFlag; |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
19 #define D(x) if (!Py_DebugFlag); else x |
| 6 | 20 #else |
| 21 #define D(x) | |
| 22 #endif | |
| 23 | |
| 24 | |
| 25 /* STACK DATA TYPE */ | |
| 26 | |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
27 static void s_reset(stack *); |
| 6 | 28 |
| 29 static void | |
| 13659 | 30 s_reset(stack *s) |
| 6 | 31 { |
| 32 s->s_top = &s->s_base[MAXSTACK]; | |
| 33 } | |
| 34 | |
| 35 #define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK]) | |
| 36 | |
| 37 static int | |
| 13659 | 38 s_push(register stack *s, dfa *d, node *parent) |
| 6 | 39 { |
| 40 register stackentry *top; | |
| 41 if (s->s_top == s->s_base) { | |
| 42 fprintf(stderr, "s_push: parser stack overflow\n"); | |
|
14968
5336946b6495
Fix a bug in stack overflow error handling. This fixes half of Bug
Guido van Rossum <guido@python.org>
parents:
14375
diff
changeset
|
43 return E_NOMEM; |
| 6 | 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 | |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
52 #ifdef Py_DEBUG |
| 6 | 53 |
| 54 static void | |
| 13659 | 55 s_pop(register stack *s) |
| 6 | 56 { |
|
2044
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
57 if (s_empty(s)) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
58 Py_FatalError("s_pop: parser stack underflow -- FATAL"); |
| 6 | 59 s->s_top++; |
| 60 } | |
| 61 | |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
62 #else /* !Py_DEBUG */ |
| 6 | 63 |
| 64 #define s_pop(s) (s)->s_top++ | |
| 65 | |
| 66 #endif | |
| 67 | |
| 68 | |
| 69 /* PARSER CREATION */ | |
| 70 | |
| 71 parser_state * | |
| 13659 | 72 PyParser_New(grammar *g, int start) |
| 6 | 73 { |
| 74 parser_state *ps; | |
| 75 | |
| 76 if (!g->g_accel) | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
77 PyGrammar_AddAccelerators(g); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
78 ps = PyMem_NEW(parser_state, 1); |
| 6 | 79 if (ps == NULL) |
| 80 return NULL; | |
| 81 ps->p_grammar = g; | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
82 ps->p_tree = PyNode_New(start); |
| 6 | 83 if (ps->p_tree == NULL) { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
84 PyMem_DEL(ps); |
| 6 | 85 return NULL; |
| 86 } | |
| 87 s_reset(&ps->p_stack); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
88 (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree); |
| 6 | 89 return ps; |
| 90 } | |
| 91 | |
| 92 void | |
| 13659 | 93 PyParser_Delete(parser_state *ps) |
| 6 | 94 { |
|
79
6c43e5476de6
Free parse tree when deleting parser.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
95 /* NB If you want to save the parse tree, |
|
6c43e5476de6
Free parse tree when deleting parser.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
96 you must set p_tree to NULL before calling delparser! */ |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
97 PyNode_Free(ps->p_tree); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
98 PyMem_DEL(ps); |
| 6 | 99 } |
| 100 | |
| 101 | |
| 102 /* PARSER STACK OPERATIONS */ | |
| 103 | |
| 104 static int | |
| 13659 | 105 shift(register stack *s, int type, char *str, int newstate, int lineno) |
| 6 | 106 { |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
107 int err; |
| 6 | 108 assert(!s_empty(s)); |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
109 err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno); |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
110 if (err) |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
111 return err; |
| 6 | 112 s->s_top->s_state = newstate; |
| 113 return 0; | |
| 114 } | |
| 115 | |
| 116 static int | |
| 13659 | 117 push(register stack *s, int type, dfa *d, int newstate, int lineno) |
| 6 | 118 { |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
119 int err; |
| 6 | 120 register node *n; |
| 121 n = s->s_top->s_parent; | |
| 122 assert(!s_empty(s)); | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
123 err = PyNode_AddChild(n, type, (char *)NULL, lineno); |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
124 if (err) |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
125 return err; |
| 6 | 126 s->s_top->s_state = newstate; |
| 127 return s_push(s, d, CHILD(n, NCH(n)-1)); | |
| 128 } | |
| 129 | |
| 130 | |
| 131 /* PARSER PROPER */ | |
| 132 | |
| 133 static int | |
| 13659 | 134 classify(grammar *g, int type, char *str) |
| 6 | 135 { |
| 136 register int n = g->g_ll.ll_nlabels; | |
| 137 | |
| 138 if (type == NAME) { | |
| 139 register char *s = str; | |
| 140 register label *l = g->g_ll.ll_label; | |
| 141 register int i; | |
| 142 for (i = n; i > 0; i--, l++) { | |
| 143 if (l->lb_type == NAME && l->lb_str != NULL && | |
| 144 l->lb_str[0] == s[0] && | |
| 145 strcmp(l->lb_str, s) == 0) { | |
| 146 D(printf("It's a keyword\n")); | |
| 147 return n - i; | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 { | |
| 153 register label *l = g->g_ll.ll_label; | |
| 154 register int i; | |
| 155 for (i = n; i > 0; i--, l++) { | |
| 156 if (l->lb_type == type && l->lb_str == NULL) { | |
| 157 D(printf("It's a token we know\n")); | |
| 158 return n - i; | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 D(printf("Illegal token\n")); | |
| 164 return -1; | |
| 165 } | |
| 166 | |
| 167 int | |
| 13659 | 168 PyParser_AddToken(register parser_state *ps, register int type, char *str, |
| 169 int lineno, int *expected_ret) | |
| 6 | 170 { |
| 171 register int ilabel; | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
172 int err; |
| 6 | 173 |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
174 D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str)); |
| 6 | 175 |
| 176 /* Find out which label this token is */ | |
| 177 ilabel = classify(ps->p_grammar, type, str); | |
| 178 if (ilabel < 0) | |
| 179 return E_SYNTAX; | |
| 180 | |
| 181 /* Loop until the token is shifted or an error occurred */ | |
| 182 for (;;) { | |
| 183 /* Fetch the current dfa and state */ | |
| 184 register dfa *d = ps->p_stack.s_top->s_dfa; | |
| 185 register state *s = &d->d_state[ps->p_stack.s_top->s_state]; | |
| 186 | |
| 187 D(printf(" DFA '%s', state %d:", | |
| 188 d->d_name, ps->p_stack.s_top->s_state)); | |
| 189 | |
| 190 /* Check accelerator */ | |
| 191 if (s->s_lower <= ilabel && ilabel < s->s_upper) { | |
| 192 register int x = s->s_accel[ilabel - s->s_lower]; | |
| 193 if (x != -1) { | |
| 194 if (x & (1<<7)) { | |
| 195 /* Push non-terminal */ | |
| 196 int nt = (x >> 8) + NT_OFFSET; | |
| 197 int arrow = x & ((1<<7)-1); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
198 dfa *d1 = PyGrammar_FindDFA( |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
199 ps->p_grammar, nt); |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
200 if ((err = push(&ps->p_stack, nt, d1, |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
201 arrow, lineno)) > 0) { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
202 D(printf(" MemError: push\n")); |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
203 return err; |
| 6 | 204 } |
| 205 D(printf(" Push ...\n")); | |
| 206 continue; | |
| 207 } | |
| 208 | |
| 209 /* Shift the token */ | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
210 if ((err = shift(&ps->p_stack, type, str, |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
211 x, lineno)) > 0) { |
| 6 | 212 D(printf(" MemError: shift.\n")); |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
213 return err; |
| 6 | 214 } |
| 215 D(printf(" Shift.\n")); | |
| 216 /* Pop while we are in an accept-only state */ | |
| 217 while (s = &d->d_state | |
| 218 [ps->p_stack.s_top->s_state], | |
| 219 s->s_accept && s->s_narcs == 1) { | |
| 220 D(printf(" Direct pop.\n")); | |
| 221 s_pop(&ps->p_stack); | |
| 222 if (s_empty(&ps->p_stack)) { | |
| 223 D(printf(" ACCEPT.\n")); | |
| 224 return E_DONE; | |
| 225 } | |
| 226 d = ps->p_stack.s_top->s_dfa; | |
| 227 } | |
| 228 return E_OK; | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 if (s->s_accept) { | |
| 233 /* Pop this dfa and try again */ | |
| 234 s_pop(&ps->p_stack); | |
| 235 D(printf(" Pop ...\n")); | |
| 236 if (s_empty(&ps->p_stack)) { | |
| 237 D(printf(" Error: bottom of stack.\n")); | |
| 238 return E_SYNTAX; | |
| 239 } | |
| 240 continue; | |
| 241 } | |
| 242 | |
| 243 /* Stuck, report syntax error */ | |
| 244 D(printf(" Error.\n")); | |
|
13539
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
245 if (expected_ret) { |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
246 if (s->s_lower == s->s_upper - 1) { |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
247 /* Only one possible expected token */ |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
248 *expected_ret = ps->p_grammar-> |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
249 g_ll.ll_label[s->s_lower].lb_type; |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
250 } |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
251 else |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
252 *expected_ret = -1; |
|
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
253 } |
| 6 | 254 return E_SYNTAX; |
| 255 } | |
| 256 } | |
| 257 | |
| 258 | |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
259 #ifdef Py_DEBUG |
| 6 | 260 |
| 261 /* DEBUG OUTPUT */ | |
| 262 | |
| 263 void | |
| 13659 | 264 dumptree(grammar *g, node *n) |
| 6 | 265 { |
| 266 int i; | |
| 267 | |
| 268 if (n == NULL) | |
| 269 printf("NIL"); | |
| 270 else { | |
| 271 label l; | |
| 272 l.lb_type = TYPE(n); | |
|
1027
513940eb4d7b
Correct debug printing code
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
273 l.lb_str = STR(n); |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
274 printf("%s", PyGrammar_LabelRepr(&l)); |
| 6 | 275 if (ISNONTERMINAL(TYPE(n))) { |
| 276 printf("("); | |
| 277 for (i = 0; i < NCH(n); i++) { | |
| 278 if (i > 0) | |
| 279 printf(","); | |
| 280 dumptree(g, CHILD(n, i)); | |
| 281 } | |
| 282 printf(")"); | |
| 283 } | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 void | |
| 13659 | 288 showtree(grammar *g, node *n) |
| 6 | 289 { |
| 290 int i; | |
| 291 | |
| 292 if (n == NULL) | |
| 293 return; | |
| 294 if (ISNONTERMINAL(TYPE(n))) { | |
| 295 for (i = 0; i < NCH(n); i++) | |
| 296 showtree(g, CHILD(n, i)); | |
| 297 } | |
| 298 else if (ISTERMINAL(TYPE(n))) { | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
299 printf("%s", _PyParser_TokenNames[TYPE(n)]); |
| 6 | 300 if (TYPE(n) == NUMBER || TYPE(n) == NAME) |
| 301 printf("(%s)", STR(n)); | |
| 302 printf(" "); | |
| 303 } | |
| 304 else | |
| 305 printf("? "); | |
| 306 } | |
| 307 | |
| 308 void | |
| 13659 | 309 printtree(parser_state *ps) |
| 6 | 310 { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
311 if (Py_DebugFlag) { |
| 6 | 312 printf("Parse tree:\n"); |
| 313 dumptree(ps->p_grammar, ps->p_tree); | |
| 314 printf("\n"); | |
| 315 printf("Tokens:\n"); | |
| 316 showtree(ps->p_grammar, ps->p_tree); | |
| 317 printf("\n"); | |
| 318 } | |
| 319 printf("Listing:\n"); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
320 PyNode_ListTree(ps->p_tree); |
| 6 | 321 printf("\n"); |
| 322 } | |
| 323 | |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
324 #endif /* Py_DEBUG */ |
| 6 | 325 |
| 326 /* | |
| 327 | |
| 328 Description | |
| 329 ----------- | |
| 330 | |
| 331 The parser's interface is different than usual: the function addtoken() | |
| 332 must be called for each token in the input. This makes it possible to | |
| 333 turn it into an incremental parsing system later. The parsing system | |
| 334 constructs a parse tree as it goes. | |
| 335 | |
| 336 A parsing rule is represented as a Deterministic Finite-state Automaton | |
| 337 (DFA). A node in a DFA represents a state of the parser; an arc represents | |
| 338 a transition. Transitions are either labeled with terminal symbols or | |
| 339 with non-terminals. When the parser decides to follow an arc labeled | |
| 340 with a non-terminal, it is invoked recursively with the DFA representing | |
| 341 the parsing rule for that as its initial state; when that DFA accepts, | |
| 342 the parser that invoked it continues. The parse tree constructed by the | |
| 343 recursively called parser is inserted as a child in the current parse tree. | |
| 344 | |
| 345 The DFA's can be constructed automatically from a more conventional | |
| 346 language description. An extended LL(1) grammar (ELL(1)) is suitable. | |
| 347 Certain restrictions make the parser's life easier: rules that can produce | |
| 348 the empty string should be outlawed (there are other ways to put loops | |
| 349 or optional parts in the language). To avoid the need to construct | |
| 350 FIRST sets, we can require that all but the last alternative of a rule | |
| 351 (really: arc going out of a DFA's state) must begin with a terminal | |
| 352 symbol. | |
| 353 | |
| 354 As an example, consider this grammar: | |
| 355 | |
| 356 expr: term (OP term)* | |
| 357 term: CONSTANT | '(' expr ')' | |
| 358 | |
| 359 The DFA corresponding to the rule for expr is: | |
| 360 | |
| 361 ------->.---term-->.-------> | |
| 362 ^ | | |
| 363 | | | |
| 364 \----OP----/ | |
| 365 | |
| 366 The parse tree generated for the input a+b is: | |
| 367 | |
| 368 (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b))) | |
| 369 | |
| 370 */ |
