blob: 141710a2952f774309445a7b07d918ca86cf1c5a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
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{
89 if (s_empty(s)) {
90 fprintf(stderr, "s_pop: parser stack underflow -- FATAL\n");
91 abort();
92 }
93 s->s_top++;
94}
95
96#else /* !DEBUG */
97
98#define s_pop(s) (s)->s_top++
99
100#endif
101
102
103/* PARSER CREATION */
104
105parser_state *
106newparser(g, start)
107 grammar *g;
108 int start;
109{
110 parser_state *ps;
111
112 if (!g->g_accel)
113 addaccelerators(g);
114 ps = NEW(parser_state, 1);
115 if (ps == NULL)
116 return NULL;
117 ps->p_grammar = g;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000118 ps->p_tree = newtree(start);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 if (ps->p_tree == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 DEL(ps);
121 return NULL;
122 }
123 s_reset(&ps->p_stack);
124 (void) s_push(&ps->p_stack, finddfa(g, start), ps->p_tree);
125 return ps;
126}
127
128void
129delparser(ps)
130 parser_state *ps;
131{
Guido van Rossum99f02d41990-11-18 17:38:42 +0000132 /* NB If you want to save the parse tree,
133 you must set p_tree to NULL before calling delparser! */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134 freetree(ps->p_tree);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135 DEL(ps);
136}
137
138
139/* PARSER STACK OPERATIONS */
140
Guido van Rossum3f5da241990-12-20 15:06:42 +0000141static int shift PROTO((stack *, int, char *, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142
143static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000144shift(s, type, str, newstate, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 register stack *s;
146 int type;
147 char *str;
148 int newstate;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000149 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
151 assert(!s_empty(s));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000152 if (addchild(s->s_top->s_parent, type, str, lineno) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 fprintf(stderr, "shift: no mem in addchild\n");
154 return -1;
155 }
156 s->s_top->s_state = newstate;
157 return 0;
158}
159
Guido van Rossum3f5da241990-12-20 15:06:42 +0000160static int push PROTO((stack *, int, dfa *, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161
162static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000163push(s, type, d, newstate, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164 register stack *s;
165 int type;
166 dfa *d;
167 int newstate;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000168 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169{
170 register node *n;
171 n = s->s_top->s_parent;
172 assert(!s_empty(s));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173 if (addchild(n, type, (char *)NULL, lineno) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174 fprintf(stderr, "push: no mem in addchild\n");
175 return -1;
176 }
177 s->s_top->s_state = newstate;
178 return s_push(s, d, CHILD(n, NCH(n)-1));
179}
180
181
182/* PARSER PROPER */
183
184static int classify PROTO((grammar *, int, char *));
185
186static int
187classify(g, type, str)
188 grammar *g;
189 register int type;
190 char *str;
191{
192 register int n = g->g_ll.ll_nlabels;
193
194 if (type == NAME) {
195 register char *s = str;
196 register label *l = g->g_ll.ll_label;
197 register int i;
198 for (i = n; i > 0; i--, l++) {
199 if (l->lb_type == NAME && l->lb_str != NULL &&
200 l->lb_str[0] == s[0] &&
201 strcmp(l->lb_str, s) == 0) {
202 D(printf("It's a keyword\n"));
203 return n - i;
204 }
205 }
206 }
207
208 {
209 register label *l = g->g_ll.ll_label;
210 register int i;
211 for (i = n; i > 0; i--, l++) {
212 if (l->lb_type == type && l->lb_str == NULL) {
213 D(printf("It's a token we know\n"));
214 return n - i;
215 }
216 }
217 }
218
219 D(printf("Illegal token\n"));
220 return -1;
221}
222
223int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224addtoken(ps, type, str, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225 register parser_state *ps;
226 register int type;
227 char *str;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229{
230 register int ilabel;
231
232 D(printf("Token %s/'%s' ... ", tok_name[type], str));
233
234 /* Find out which label this token is */
235 ilabel = classify(ps->p_grammar, type, str);
236 if (ilabel < 0)
237 return E_SYNTAX;
238
239 /* Loop until the token is shifted or an error occurred */
240 for (;;) {
241 /* Fetch the current dfa and state */
242 register dfa *d = ps->p_stack.s_top->s_dfa;
243 register state *s = &d->d_state[ps->p_stack.s_top->s_state];
244
245 D(printf(" DFA '%s', state %d:",
246 d->d_name, ps->p_stack.s_top->s_state));
247
248 /* Check accelerator */
249 if (s->s_lower <= ilabel && ilabel < s->s_upper) {
250 register int x = s->s_accel[ilabel - s->s_lower];
251 if (x != -1) {
252 if (x & (1<<7)) {
253 /* Push non-terminal */
254 int nt = (x >> 8) + NT_OFFSET;
255 int arrow = x & ((1<<7)-1);
256 dfa *d1 = finddfa(ps->p_grammar, nt);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257 if (push(&ps->p_stack, nt, d1,
258 arrow, lineno) < 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259 D(printf(" MemError: push.\n"));
260 return E_NOMEM;
261 }
262 D(printf(" Push ...\n"));
263 continue;
264 }
265
266 /* Shift the token */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267 if (shift(&ps->p_stack, type, str,
268 x, lineno) < 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269 D(printf(" MemError: shift.\n"));
270 return E_NOMEM;
271 }
272 D(printf(" Shift.\n"));
273 /* Pop while we are in an accept-only state */
274 while (s = &d->d_state
275 [ps->p_stack.s_top->s_state],
276 s->s_accept && s->s_narcs == 1) {
277 D(printf(" Direct pop.\n"));
278 s_pop(&ps->p_stack);
279 if (s_empty(&ps->p_stack)) {
280 D(printf(" ACCEPT.\n"));
281 return E_DONE;
282 }
283 d = ps->p_stack.s_top->s_dfa;
284 }
285 return E_OK;
286 }
287 }
288
289 if (s->s_accept) {
290 /* Pop this dfa and try again */
291 s_pop(&ps->p_stack);
292 D(printf(" Pop ...\n"));
293 if (s_empty(&ps->p_stack)) {
294 D(printf(" Error: bottom of stack.\n"));
295 return E_SYNTAX;
296 }
297 continue;
298 }
299
300 /* Stuck, report syntax error */
301 D(printf(" Error.\n"));
302 return E_SYNTAX;
303 }
304}
305
306
307#ifdef DEBUG
308
309/* DEBUG OUTPUT */
310
311void
312dumptree(g, n)
313 grammar *g;
314 node *n;
315{
316 int i;
317
318 if (n == NULL)
319 printf("NIL");
320 else {
321 label l;
322 l.lb_type = TYPE(n);
323 l.lb_str = TYPE(str);
324 printf("%s", labelrepr(&l));
325 if (ISNONTERMINAL(TYPE(n))) {
326 printf("(");
327 for (i = 0; i < NCH(n); i++) {
328 if (i > 0)
329 printf(",");
330 dumptree(g, CHILD(n, i));
331 }
332 printf(")");
333 }
334 }
335}
336
337void
338showtree(g, n)
339 grammar *g;
340 node *n;
341{
342 int i;
343
344 if (n == NULL)
345 return;
346 if (ISNONTERMINAL(TYPE(n))) {
347 for (i = 0; i < NCH(n); i++)
348 showtree(g, CHILD(n, i));
349 }
350 else if (ISTERMINAL(TYPE(n))) {
351 printf("%s", tok_name[TYPE(n)]);
352 if (TYPE(n) == NUMBER || TYPE(n) == NAME)
353 printf("(%s)", STR(n));
354 printf(" ");
355 }
356 else
357 printf("? ");
358}
359
360void
361printtree(ps)
362 parser_state *ps;
363{
364 if (debugging) {
365 printf("Parse tree:\n");
366 dumptree(ps->p_grammar, ps->p_tree);
367 printf("\n");
368 printf("Tokens:\n");
369 showtree(ps->p_grammar, ps->p_tree);
370 printf("\n");
371 }
372 printf("Listing:\n");
373 listtree(ps->p_tree);
374 printf("\n");
375}
376
377#endif /* DEBUG */
378
379/*
380
381Description
382-----------
383
384The parser's interface is different than usual: the function addtoken()
385must be called for each token in the input. This makes it possible to
386turn it into an incremental parsing system later. The parsing system
387constructs a parse tree as it goes.
388
389A parsing rule is represented as a Deterministic Finite-state Automaton
390(DFA). A node in a DFA represents a state of the parser; an arc represents
391a transition. Transitions are either labeled with terminal symbols or
392with non-terminals. When the parser decides to follow an arc labeled
393with a non-terminal, it is invoked recursively with the DFA representing
394the parsing rule for that as its initial state; when that DFA accepts,
395the parser that invoked it continues. The parse tree constructed by the
396recursively called parser is inserted as a child in the current parse tree.
397
398The DFA's can be constructed automatically from a more conventional
399language description. An extended LL(1) grammar (ELL(1)) is suitable.
400Certain restrictions make the parser's life easier: rules that can produce
401the empty string should be outlawed (there are other ways to put loops
402or optional parts in the language). To avoid the need to construct
403FIRST sets, we can require that all but the last alternative of a rule
404(really: arc going out of a DFA's state) must begin with a terminal
405symbol.
406
407As an example, consider this grammar:
408
409expr: term (OP term)*
410term: CONSTANT | '(' expr ')'
411
412The DFA corresponding to the rule for expr is:
413
414------->.---term-->.------->
415 ^ |
416 | |
417 \----OP----/
418
419The parse tree generated for the input a+b is:
420
421(expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
422
423*/