blob: 54cc9ee33cd0c857c1cbcc809f33479e4e6b105d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumb9f8d6e1995-01-04 19:08:09 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Parser implementation */
26
27/* For a description, see the comments at end of this file */
28
29/* XXX To do: error recovery */
30
Guido van Rossum3f5da241990-12-20 15:06:42 +000031#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032#include "assert.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033#include "token.h"
34#include "grammar.h"
35#include "node.h"
36#include "parser.h"
37#include "errcode.h"
38
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
40#ifdef DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000041extern int debugging;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042#define D(x) if (!debugging); else x
43#else
44#define D(x)
45#endif
46
47
48/* STACK DATA TYPE */
49
50static void s_reset PROTO((stack *));
51
52static void
53s_reset(s)
54 stack *s;
55{
56 s->s_top = &s->s_base[MAXSTACK];
57}
58
59#define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
60
61static int s_push PROTO((stack *, dfa *, node *));
62
63static int
64s_push(s, d, parent)
65 register stack *s;
66 dfa *d;
67 node *parent;
68{
69 register stackentry *top;
70 if (s->s_top == s->s_base) {
71 fprintf(stderr, "s_push: parser stack overflow\n");
72 return -1;
73 }
74 top = --s->s_top;
75 top->s_dfa = d;
76 top->s_parent = parent;
77 top->s_state = 0;
78 return 0;
79}
80
81#ifdef DEBUG
82
83static void s_pop PROTO((stack *));
84
85static void
86s_pop(s)
87 register stack *s;
88{
Guido van Rossum588633d1994-12-30 15:46:02 +000089 if (s_empty(s))
90 fatal("s_pop: parser stack underflow -- FATAL");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091 s->s_top++;
92}
93
94#else /* !DEBUG */
95
96#define s_pop(s) (s)->s_top++
97
98#endif
99
100
101/* PARSER CREATION */
102
103parser_state *
104newparser(g, start)
105 grammar *g;
106 int start;
107{
108 parser_state *ps;
109
110 if (!g->g_accel)
111 addaccelerators(g);
112 ps = NEW(parser_state, 1);
113 if (ps == NULL)
114 return NULL;
115 ps->p_grammar = g;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000116 ps->p_tree = newtree(start);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 if (ps->p_tree == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118 DEL(ps);
119 return NULL;
120 }
121 s_reset(&ps->p_stack);
122 (void) s_push(&ps->p_stack, finddfa(g, start), ps->p_tree);
123 return ps;
124}
125
126void
127delparser(ps)
128 parser_state *ps;
129{
Guido van Rossum99f02d41990-11-18 17:38:42 +0000130 /* NB If you want to save the parse tree,
131 you must set p_tree to NULL before calling delparser! */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132 freetree(ps->p_tree);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133 DEL(ps);
134}
135
136
137/* PARSER STACK OPERATIONS */
138
Guido van Rossum3f5da241990-12-20 15:06:42 +0000139static int shift PROTO((stack *, int, char *, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140
141static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142shift(s, type, str, newstate, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143 register stack *s;
144 int type;
145 char *str;
146 int newstate;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
149 assert(!s_empty(s));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000150 if (addchild(s->s_top->s_parent, type, str, lineno) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 fprintf(stderr, "shift: no mem in addchild\n");
152 return -1;
153 }
154 s->s_top->s_state = newstate;
155 return 0;
156}
157
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158static int push PROTO((stack *, int, dfa *, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159
160static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161push(s, type, d, newstate, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162 register stack *s;
163 int type;
164 dfa *d;
165 int newstate;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000166 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167{
168 register node *n;
169 n = s->s_top->s_parent;
170 assert(!s_empty(s));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000171 if (addchild(n, type, (char *)NULL, lineno) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172 fprintf(stderr, "push: no mem in addchild\n");
173 return -1;
174 }
175 s->s_top->s_state = newstate;
176 return s_push(s, d, CHILD(n, NCH(n)-1));
177}
178
179
180/* PARSER PROPER */
181
182static int classify PROTO((grammar *, int, char *));
183
184static int
185classify(g, type, str)
186 grammar *g;
187 register int type;
188 char *str;
189{
190 register int n = g->g_ll.ll_nlabels;
191
192 if (type == NAME) {
193 register char *s = str;
194 register label *l = g->g_ll.ll_label;
195 register int i;
196 for (i = n; i > 0; i--, l++) {
197 if (l->lb_type == NAME && l->lb_str != NULL &&
198 l->lb_str[0] == s[0] &&
199 strcmp(l->lb_str, s) == 0) {
200 D(printf("It's a keyword\n"));
201 return n - i;
202 }
203 }
204 }
205
206 {
207 register label *l = g->g_ll.ll_label;
208 register int i;
209 for (i = n; i > 0; i--, l++) {
210 if (l->lb_type == type && l->lb_str == NULL) {
211 D(printf("It's a token we know\n"));
212 return n - i;
213 }
214 }
215 }
216
217 D(printf("Illegal token\n"));
218 return -1;
219}
220
221int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000222addtoken(ps, type, str, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 register parser_state *ps;
224 register int type;
225 char *str;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000226 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227{
228 register int ilabel;
229
230 D(printf("Token %s/'%s' ... ", tok_name[type], str));
231
232 /* Find out which label this token is */
233 ilabel = classify(ps->p_grammar, type, str);
234 if (ilabel < 0)
235 return E_SYNTAX;
236
237 /* Loop until the token is shifted or an error occurred */
238 for (;;) {
239 /* Fetch the current dfa and state */
240 register dfa *d = ps->p_stack.s_top->s_dfa;
241 register state *s = &d->d_state[ps->p_stack.s_top->s_state];
242
243 D(printf(" DFA '%s', state %d:",
244 d->d_name, ps->p_stack.s_top->s_state));
245
246 /* Check accelerator */
247 if (s->s_lower <= ilabel && ilabel < s->s_upper) {
248 register int x = s->s_accel[ilabel - s->s_lower];
249 if (x != -1) {
250 if (x & (1<<7)) {
251 /* Push non-terminal */
252 int nt = (x >> 8) + NT_OFFSET;
253 int arrow = x & ((1<<7)-1);
254 dfa *d1 = finddfa(ps->p_grammar, nt);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000255 if (push(&ps->p_stack, nt, d1,
256 arrow, lineno) < 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 D(printf(" MemError: push.\n"));
258 return E_NOMEM;
259 }
260 D(printf(" Push ...\n"));
261 continue;
262 }
263
264 /* Shift the token */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000265 if (shift(&ps->p_stack, type, str,
266 x, lineno) < 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 D(printf(" MemError: shift.\n"));
268 return E_NOMEM;
269 }
270 D(printf(" Shift.\n"));
271 /* Pop while we are in an accept-only state */
272 while (s = &d->d_state
273 [ps->p_stack.s_top->s_state],
274 s->s_accept && s->s_narcs == 1) {
275 D(printf(" Direct pop.\n"));
276 s_pop(&ps->p_stack);
277 if (s_empty(&ps->p_stack)) {
278 D(printf(" ACCEPT.\n"));
279 return E_DONE;
280 }
281 d = ps->p_stack.s_top->s_dfa;
282 }
283 return E_OK;
284 }
285 }
286
287 if (s->s_accept) {
288 /* Pop this dfa and try again */
289 s_pop(&ps->p_stack);
290 D(printf(" Pop ...\n"));
291 if (s_empty(&ps->p_stack)) {
292 D(printf(" Error: bottom of stack.\n"));
293 return E_SYNTAX;
294 }
295 continue;
296 }
297
298 /* Stuck, report syntax error */
299 D(printf(" Error.\n"));
300 return E_SYNTAX;
301 }
302}
303
304
305#ifdef DEBUG
306
307/* DEBUG OUTPUT */
308
309void
310dumptree(g, n)
311 grammar *g;
312 node *n;
313{
314 int i;
315
316 if (n == NULL)
317 printf("NIL");
318 else {
319 label l;
320 l.lb_type = TYPE(n);
Guido van Rossumcf7448b1992-09-03 20:46:37 +0000321 l.lb_str = STR(n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 printf("%s", labelrepr(&l));
323 if (ISNONTERMINAL(TYPE(n))) {
324 printf("(");
325 for (i = 0; i < NCH(n); i++) {
326 if (i > 0)
327 printf(",");
328 dumptree(g, CHILD(n, i));
329 }
330 printf(")");
331 }
332 }
333}
334
335void
336showtree(g, n)
337 grammar *g;
338 node *n;
339{
340 int i;
341
342 if (n == NULL)
343 return;
344 if (ISNONTERMINAL(TYPE(n))) {
345 for (i = 0; i < NCH(n); i++)
346 showtree(g, CHILD(n, i));
347 }
348 else if (ISTERMINAL(TYPE(n))) {
349 printf("%s", tok_name[TYPE(n)]);
350 if (TYPE(n) == NUMBER || TYPE(n) == NAME)
351 printf("(%s)", STR(n));
352 printf(" ");
353 }
354 else
355 printf("? ");
356}
357
358void
359printtree(ps)
360 parser_state *ps;
361{
362 if (debugging) {
363 printf("Parse tree:\n");
364 dumptree(ps->p_grammar, ps->p_tree);
365 printf("\n");
366 printf("Tokens:\n");
367 showtree(ps->p_grammar, ps->p_tree);
368 printf("\n");
369 }
370 printf("Listing:\n");
371 listtree(ps->p_tree);
372 printf("\n");
373}
374
375#endif /* DEBUG */
376
377/*
378
379Description
380-----------
381
382The parser's interface is different than usual: the function addtoken()
383must be called for each token in the input. This makes it possible to
384turn it into an incremental parsing system later. The parsing system
385constructs a parse tree as it goes.
386
387A parsing rule is represented as a Deterministic Finite-state Automaton
388(DFA). A node in a DFA represents a state of the parser; an arc represents
389a transition. Transitions are either labeled with terminal symbols or
390with non-terminals. When the parser decides to follow an arc labeled
391with a non-terminal, it is invoked recursively with the DFA representing
392the parsing rule for that as its initial state; when that DFA accepts,
393the parser that invoked it continues. The parse tree constructed by the
394recursively called parser is inserted as a child in the current parse tree.
395
396The DFA's can be constructed automatically from a more conventional
397language description. An extended LL(1) grammar (ELL(1)) is suitable.
398Certain restrictions make the parser's life easier: rules that can produce
399the empty string should be outlawed (there are other ways to put loops
400or optional parts in the language). To avoid the need to construct
401FIRST sets, we can require that all but the last alternative of a rule
402(really: arc going out of a DFA's state) must begin with a terminal
403symbol.
404
405As an example, consider this grammar:
406
407expr: term (OP term)*
408term: CONSTANT | '(' expr ')'
409
410The DFA corresponding to the rule for expr is:
411
412------->.---term-->.------->
413 ^ |
414 | |
415 \----OP----/
416
417The parse tree generated for the input a+b is:
418
419(expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
420
421*/