blob: 9e115df1890d5ddd00bd4b4d8c0cb95c32240ad4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Parser implementation */
3
4/* For a description, see the comments at end of this file */
5
6/* XXX To do: error recovery */
7
Tim Peters1ca12962001-12-04 03:18:48 +00008#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010#include "token.h"
11#include "grammar.h"
12#include "node.h"
13#include "parser.h"
14#include "errcode.h"
15
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016
Guido van Rossum408027e1996-12-30 16:17:54 +000017#ifdef Py_DEBUG
Guido van Rossum86bea461997-04-29 21:03:06 +000018extern int Py_DebugFlag;
19#define D(x) if (!Py_DebugFlag); else x
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020#else
21#define D(x)
22#endif
23
24
25/* STACK DATA TYPE */
26
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static void s_reset(stack *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
29static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000030s_reset(stack *s)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
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 Rossum85a5fbb1990-10-14 12:07:46 +000037static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000038s_push(register stack *s, dfa *d, node *parent)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039{
40 register stackentry *top;
41 if (s->s_top == s->s_base) {
42 fprintf(stderr, "s_push: parser stack overflow\n");
Guido van Rossume3c3b272000-10-02 10:21:59 +000043 return E_NOMEM;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044 }
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 Rossum408027e1996-12-30 16:17:54 +000052#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000055s_pop(register stack *s)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056{
Guido van Rossum588633d1994-12-30 15:46:02 +000057 if (s_empty(s))
Guido van Rossum86bea461997-04-29 21:03:06 +000058 Py_FatalError("s_pop: parser stack underflow -- FATAL");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 s->s_top++;
60}
61
Guido van Rossum408027e1996-12-30 16:17:54 +000062#else /* !Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063
64#define s_pop(s) (s)->s_top++
65
66#endif
67
68
69/* PARSER CREATION */
70
71parser_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +000072PyParser_New(grammar *g, int start)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073{
74 parser_state *ps;
75
76 if (!g->g_accel)
Guido van Rossum86bea461997-04-29 21:03:06 +000077 PyGrammar_AddAccelerators(g);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 ps = (parser_state *)PyMem_MALLOC(sizeof(parser_state));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 if (ps == NULL)
80 return NULL;
81 ps->p_grammar = g;
Thomas Wouters34aa7ba2006-02-28 19:02:24 +000082#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
83 ps->p_flags = 0;
Neil Schemenauerc155dd42002-03-22 23:38:11 +000084#endif
Guido van Rossum86bea461997-04-29 21:03:06 +000085 ps->p_tree = PyNode_New(start);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 if (ps->p_tree == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087 PyMem_FREE(ps);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 return NULL;
89 }
90 s_reset(&ps->p_stack);
Guido van Rossum86bea461997-04-29 21:03:06 +000091 (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 return ps;
93}
94
95void
Thomas Wouters23c9e002000-07-22 19:20:54 +000096PyParser_Delete(parser_state *ps)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossum99f02d41990-11-18 17:38:42 +000098 /* NB If you want to save the parse tree,
99 you must set p_tree to NULL before calling delparser! */
Guido van Rossum86bea461997-04-29 21:03:06 +0000100 PyNode_Free(ps->p_tree);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101 PyMem_FREE(ps);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102}
103
104
105/* PARSER STACK OPERATIONS */
106
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107static int
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000108shift(register stack *s, int type, char *str, int newstate, int lineno, int col_offset)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109{
Jeremy Hylton94988062000-06-20 19:10:44 +0000110 int err;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 assert(!s_empty(s));
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000112 err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset);
Jeremy Hylton94988062000-06-20 19:10:44 +0000113 if (err)
114 return err;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 s->s_top->s_state = newstate;
116 return 0;
117}
118
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119static int
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000120push(register stack *s, int type, dfa *d, int newstate, int lineno, int col_offset)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Jeremy Hylton94988062000-06-20 19:10:44 +0000122 int err;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 register node *n;
124 n = s->s_top->s_parent;
125 assert(!s_empty(s));
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000126 err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset);
Jeremy Hylton94988062000-06-20 19:10:44 +0000127 if (err)
128 return err;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129 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 Rossum85a5fbb1990-10-14 12:07:46 +0000136static int
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000137classify(parser_state *ps, int type, char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138{
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000139 grammar *g = ps->p_grammar;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 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 Wouters8ae12952006-02-28 22:42:15 +0000147 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 Wouters34aa7ba2006-02-28 19:02:24 +0000151#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Guido van Rossum45aecf42006-03-15 04:58:47 +0000152 /* Leaving this in as an example */
Thomas Wouters8ae12952006-02-28 22:42:15 +0000153 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 Rossum85a5fbb1990-10-14 12:07:46 +0000158 }
Thomas Wouters8ae12952006-02-28 22:42:15 +0000159#endif
160 D(printf("It's a keyword\n"));
161 return n - i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162 }
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 Wouters34aa7ba2006-02-28 19:02:24 +0000180#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Guido van Rossum45aecf42006-03-15 04:58:47 +0000181/* Leaving this in as an example */
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000182static void
183future_hack(parser_state *ps)
184{
185 node *n = ps->p_stack.s_top->s_parent;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 node *ch, *cch;
Guido van Rossum3c033232001-07-19 15:27:45 +0000187 int i;
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000188
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000189 /* 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 Rossumb09f7ed2001-07-15 21:08:29 +0000195 return;
196 ch = CHILD(n, 1);
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000197 if (NCH(ch) == 1 && STR(CHILD(ch, 0)) &&
198 strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000199 return;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000200 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);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000209 if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME) {
210 char *str_ch = STR(CHILD(cch, 0));
211 if (strcmp(str_ch, FUTURE_WITH_STATEMENT) == 0) {
212 ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
213 break;
214 } else if (strcmp(str_ch, FUTURE_PRINT_FUNCTION) == 0) {
215 ps->p_flags |= CO_FUTURE_PRINT_FUNCTION;
216 break;
217 } else if (strcmp(str_ch, FUTURE_UNICODE_LITERALS) == 0) {
218 ps->p_flags |= CO_FUTURE_UNICODE_LITERALS;
219 break;
220 }
Guido van Rossum3c033232001-07-19 15:27:45 +0000221 }
222 }
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000223}
Neil Schemenauerc155dd42002-03-22 23:38:11 +0000224#endif /* future keyword */
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000225
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000227PyParser_AddToken(register parser_state *ps, register int type, char *str,
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000228 int lineno, int col_offset, int *expected_ret)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229{
230 register int ilabel;
Jeremy Hylton94988062000-06-20 19:10:44 +0000231 int err;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232
Guido van Rossum86bea461997-04-29 21:03:06 +0000233 D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234
235 /* Find out which label this token is */
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000236 ilabel = classify(ps, type, str);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237 if (ilabel < 0)
238 return E_SYNTAX;
239
240 /* Loop until the token is shifted or an error occurred */
241 for (;;) {
242 /* Fetch the current dfa and state */
243 register dfa *d = ps->p_stack.s_top->s_dfa;
244 register state *s = &d->d_state[ps->p_stack.s_top->s_state];
245
246 D(printf(" DFA '%s', state %d:",
247 d->d_name, ps->p_stack.s_top->s_state));
248
249 /* Check accelerator */
250 if (s->s_lower <= ilabel && ilabel < s->s_upper) {
251 register int x = s->s_accel[ilabel - s->s_lower];
252 if (x != -1) {
253 if (x & (1<<7)) {
254 /* Push non-terminal */
255 int nt = (x >> 8) + NT_OFFSET;
256 int arrow = x & ((1<<7)-1);
Guido van Rossum86bea461997-04-29 21:03:06 +0000257 dfa *d1 = PyGrammar_FindDFA(
258 ps->p_grammar, nt);
Jeremy Hylton94988062000-06-20 19:10:44 +0000259 if ((err = push(&ps->p_stack, nt, d1,
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000260 arrow, lineno, col_offset)) > 0) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000261 D(printf(" MemError: push\n"));
Jeremy Hylton94988062000-06-20 19:10:44 +0000262 return err;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263 }
264 D(printf(" Push ...\n"));
265 continue;
266 }
267
268 /* Shift the token */
Jeremy Hylton94988062000-06-20 19:10:44 +0000269 if ((err = shift(&ps->p_stack, type, str,
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000270 x, lineno, col_offset)) > 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 D(printf(" MemError: shift.\n"));
Jeremy Hylton94988062000-06-20 19:10:44 +0000272 return err;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273 }
274 D(printf(" Shift.\n"));
275 /* Pop while we are in an accept-only state */
276 while (s = &d->d_state
277 [ps->p_stack.s_top->s_state],
278 s->s_accept && s->s_narcs == 1) {
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000279 D(printf(" DFA '%s', state %d: "
280 "Direct pop.\n",
281 d->d_name,
282 ps->p_stack.s_top->s_state));
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000283#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000284 if (d->d_name[0] == 'i' &&
285 strcmp(d->d_name,
286 "import_stmt") == 0)
287 future_hack(ps);
Neil Schemenauerc155dd42002-03-22 23:38:11 +0000288#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 s_pop(&ps->p_stack);
290 if (s_empty(&ps->p_stack)) {
291 D(printf(" ACCEPT.\n"));
292 return E_DONE;
293 }
294 d = ps->p_stack.s_top->s_dfa;
295 }
296 return E_OK;
297 }
298 }
299
300 if (s->s_accept) {
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000301#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Guido van Rossumb09f7ed2001-07-15 21:08:29 +0000302 if (d->d_name[0] == 'i' &&
303 strcmp(d->d_name, "import_stmt") == 0)
304 future_hack(ps);
Neil Schemenauerc155dd42002-03-22 23:38:11 +0000305#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306 /* Pop this dfa and try again */
307 s_pop(&ps->p_stack);
308 D(printf(" Pop ...\n"));
309 if (s_empty(&ps->p_stack)) {
310 D(printf(" Error: bottom of stack.\n"));
311 return E_SYNTAX;
312 }
313 continue;
314 }
315
316 /* Stuck, report syntax error */
317 D(printf(" Error.\n"));
Fred Drake85f36392000-07-11 17:53:00 +0000318 if (expected_ret) {
319 if (s->s_lower == s->s_upper - 1) {
320 /* Only one possible expected token */
321 *expected_ret = ps->p_grammar->
322 g_ll.ll_label[s->s_lower].lb_type;
323 }
324 else
325 *expected_ret = -1;
326 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327 return E_SYNTAX;
328 }
329}
330
331
Guido van Rossum408027e1996-12-30 16:17:54 +0000332#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333
334/* DEBUG OUTPUT */
335
336void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000337dumptree(grammar *g, node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338{
339 int i;
340
341 if (n == NULL)
342 printf("NIL");
343 else {
344 label l;
345 l.lb_type = TYPE(n);
Guido van Rossumcf7448b1992-09-03 20:46:37 +0000346 l.lb_str = STR(n);
Guido van Rossum86bea461997-04-29 21:03:06 +0000347 printf("%s", PyGrammar_LabelRepr(&l));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348 if (ISNONTERMINAL(TYPE(n))) {
349 printf("(");
350 for (i = 0; i < NCH(n); i++) {
351 if (i > 0)
352 printf(",");
353 dumptree(g, CHILD(n, i));
354 }
355 printf(")");
356 }
357 }
358}
359
360void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000361showtree(grammar *g, node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362{
363 int i;
364
365 if (n == NULL)
366 return;
367 if (ISNONTERMINAL(TYPE(n))) {
368 for (i = 0; i < NCH(n); i++)
369 showtree(g, CHILD(n, i));
370 }
371 else if (ISTERMINAL(TYPE(n))) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000372 printf("%s", _PyParser_TokenNames[TYPE(n)]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373 if (TYPE(n) == NUMBER || TYPE(n) == NAME)
374 printf("(%s)", STR(n));
375 printf(" ");
376 }
377 else
378 printf("? ");
379}
380
381void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000382printtree(parser_state *ps)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383{
Guido van Rossum86bea461997-04-29 21:03:06 +0000384 if (Py_DebugFlag) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385 printf("Parse tree:\n");
386 dumptree(ps->p_grammar, ps->p_tree);
387 printf("\n");
388 printf("Tokens:\n");
389 showtree(ps->p_grammar, ps->p_tree);
390 printf("\n");
391 }
392 printf("Listing:\n");
Guido van Rossum86bea461997-04-29 21:03:06 +0000393 PyNode_ListTree(ps->p_tree);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 printf("\n");
395}
396
Guido van Rossum408027e1996-12-30 16:17:54 +0000397#endif /* Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398
399/*
400
401Description
402-----------
403
404The parser's interface is different than usual: the function addtoken()
405must be called for each token in the input. This makes it possible to
406turn it into an incremental parsing system later. The parsing system
407constructs a parse tree as it goes.
408
409A parsing rule is represented as a Deterministic Finite-state Automaton
410(DFA). A node in a DFA represents a state of the parser; an arc represents
411a transition. Transitions are either labeled with terminal symbols or
412with non-terminals. When the parser decides to follow an arc labeled
413with a non-terminal, it is invoked recursively with the DFA representing
414the parsing rule for that as its initial state; when that DFA accepts,
415the parser that invoked it continues. The parse tree constructed by the
416recursively called parser is inserted as a child in the current parse tree.
417
418The DFA's can be constructed automatically from a more conventional
419language description. An extended LL(1) grammar (ELL(1)) is suitable.
420Certain restrictions make the parser's life easier: rules that can produce
421the empty string should be outlawed (there are other ways to put loops
422or optional parts in the language). To avoid the need to construct
423FIRST sets, we can require that all but the last alternative of a rule
424(really: arc going out of a DFA's state) must begin with a terminal
425symbol.
426
427As an example, consider this grammar:
428
429expr: term (OP term)*
430term: CONSTANT | '(' expr ')'
431
432The DFA corresponding to the rule for expr is:
433
434------->.---term-->.------->
435 ^ |
436 | |
437 \----OP----/
438
439The parse tree generated for the input a+b is:
440
441(expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
442
443*/