blob: 831d870d0e3a74a07f239661cbe30323bd042381 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Parser implementation */
2
3/* For a description, see the comments at end of this file */
4
5/* XXX To do: error recovery */
6
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "assert.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009#include "token.h"
10#include "grammar.h"
11#include "node.h"
12#include "parser.h"
13#include "errcode.h"
14
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
16#ifdef DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000017extern int debugging;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018#define D(x) if (!debugging); else x
19#else
20#define D(x)
21#endif
22
23
24/* STACK DATA TYPE */
25
26static void s_reset PROTO((stack *));
27
28static void
29s_reset(s)
30 stack *s;
31{
32 s->s_top = &s->s_base[MAXSTACK];
33}
34
35#define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
36
37static int s_push PROTO((stack *, dfa *, node *));
38
39static int
40s_push(s, d, parent)
41 register stack *s;
42 dfa *d;
43 node *parent;
44{
45 register stackentry *top;
46 if (s->s_top == s->s_base) {
47 fprintf(stderr, "s_push: parser stack overflow\n");
48 return -1;
49 }
50 top = --s->s_top;
51 top->s_dfa = d;
52 top->s_parent = parent;
53 top->s_state = 0;
54 return 0;
55}
56
57#ifdef DEBUG
58
59static void s_pop PROTO((stack *));
60
61static void
62s_pop(s)
63 register stack *s;
64{
65 if (s_empty(s)) {
66 fprintf(stderr, "s_pop: parser stack underflow -- FATAL\n");
67 abort();
68 }
69 s->s_top++;
70}
71
72#else /* !DEBUG */
73
74#define s_pop(s) (s)->s_top++
75
76#endif
77
78
79/* PARSER CREATION */
80
81parser_state *
82newparser(g, start)
83 grammar *g;
84 int start;
85{
86 parser_state *ps;
87
88 if (!g->g_accel)
89 addaccelerators(g);
90 ps = NEW(parser_state, 1);
91 if (ps == NULL)
92 return NULL;
93 ps->p_grammar = g;
Guido van Rossum3f5da241990-12-20 15:06:42 +000094 ps->p_tree = newtree(start);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 if (ps->p_tree == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 DEL(ps);
97 return NULL;
98 }
99 s_reset(&ps->p_stack);
100 (void) s_push(&ps->p_stack, finddfa(g, start), ps->p_tree);
101 return ps;
102}
103
104void
105delparser(ps)
106 parser_state *ps;
107{
Guido van Rossum99f02d41990-11-18 17:38:42 +0000108 /* NB If you want to save the parse tree,
109 you must set p_tree to NULL before calling delparser! */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110 freetree(ps->p_tree);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 DEL(ps);
112}
113
114
115/* PARSER STACK OPERATIONS */
116
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117static int shift PROTO((stack *, int, char *, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118
119static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120shift(s, type, str, newstate, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121 register stack *s;
122 int type;
123 char *str;
124 int newstate;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126{
127 assert(!s_empty(s));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128 if (addchild(s->s_top->s_parent, type, str, lineno) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129 fprintf(stderr, "shift: no mem in addchild\n");
130 return -1;
131 }
132 s->s_top->s_state = newstate;
133 return 0;
134}
135
Guido van Rossum3f5da241990-12-20 15:06:42 +0000136static int push PROTO((stack *, int, dfa *, int, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137
138static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000139push(s, type, d, newstate, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 register stack *s;
141 int type;
142 dfa *d;
143 int newstate;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000144 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145{
146 register node *n;
147 n = s->s_top->s_parent;
148 assert(!s_empty(s));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000149 if (addchild(n, type, (char *)NULL, lineno) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 fprintf(stderr, "push: no mem in addchild\n");
151 return -1;
152 }
153 s->s_top->s_state = newstate;
154 return s_push(s, d, CHILD(n, NCH(n)-1));
155}
156
157
158/* PARSER PROPER */
159
160static int classify PROTO((grammar *, int, char *));
161
162static int
163classify(g, type, str)
164 grammar *g;
165 register int type;
166 char *str;
167{
168 register int n = g->g_ll.ll_nlabels;
169
170 if (type == NAME) {
171 register char *s = str;
172 register label *l = g->g_ll.ll_label;
173 register int i;
174 for (i = n; i > 0; i--, l++) {
175 if (l->lb_type == NAME && l->lb_str != NULL &&
176 l->lb_str[0] == s[0] &&
177 strcmp(l->lb_str, s) == 0) {
178 D(printf("It's a keyword\n"));
179 return n - i;
180 }
181 }
182 }
183
184 {
185 register label *l = g->g_ll.ll_label;
186 register int i;
187 for (i = n; i > 0; i--, l++) {
188 if (l->lb_type == type && l->lb_str == NULL) {
189 D(printf("It's a token we know\n"));
190 return n - i;
191 }
192 }
193 }
194
195 D(printf("Illegal token\n"));
196 return -1;
197}
198
199int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000200addtoken(ps, type, str, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201 register parser_state *ps;
202 register int type;
203 char *str;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000204 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205{
206 register int ilabel;
207
208 D(printf("Token %s/'%s' ... ", tok_name[type], str));
209
210 /* Find out which label this token is */
211 ilabel = classify(ps->p_grammar, type, str);
212 if (ilabel < 0)
213 return E_SYNTAX;
214
215 /* Loop until the token is shifted or an error occurred */
216 for (;;) {
217 /* Fetch the current dfa and state */
218 register dfa *d = ps->p_stack.s_top->s_dfa;
219 register state *s = &d->d_state[ps->p_stack.s_top->s_state];
220
221 D(printf(" DFA '%s', state %d:",
222 d->d_name, ps->p_stack.s_top->s_state));
223
224 /* Check accelerator */
225 if (s->s_lower <= ilabel && ilabel < s->s_upper) {
226 register int x = s->s_accel[ilabel - s->s_lower];
227 if (x != -1) {
228 if (x & (1<<7)) {
229 /* Push non-terminal */
230 int nt = (x >> 8) + NT_OFFSET;
231 int arrow = x & ((1<<7)-1);
232 dfa *d1 = finddfa(ps->p_grammar, nt);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000233 if (push(&ps->p_stack, nt, d1,
234 arrow, lineno) < 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235 D(printf(" MemError: push.\n"));
236 return E_NOMEM;
237 }
238 D(printf(" Push ...\n"));
239 continue;
240 }
241
242 /* Shift the token */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000243 if (shift(&ps->p_stack, type, str,
244 x, lineno) < 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245 D(printf(" MemError: shift.\n"));
246 return E_NOMEM;
247 }
248 D(printf(" Shift.\n"));
249 /* Pop while we are in an accept-only state */
250 while (s = &d->d_state
251 [ps->p_stack.s_top->s_state],
252 s->s_accept && s->s_narcs == 1) {
253 D(printf(" Direct pop.\n"));
254 s_pop(&ps->p_stack);
255 if (s_empty(&ps->p_stack)) {
256 D(printf(" ACCEPT.\n"));
257 return E_DONE;
258 }
259 d = ps->p_stack.s_top->s_dfa;
260 }
261 return E_OK;
262 }
263 }
264
265 if (s->s_accept) {
266 /* Pop this dfa and try again */
267 s_pop(&ps->p_stack);
268 D(printf(" Pop ...\n"));
269 if (s_empty(&ps->p_stack)) {
270 D(printf(" Error: bottom of stack.\n"));
271 return E_SYNTAX;
272 }
273 continue;
274 }
275
276 /* Stuck, report syntax error */
277 D(printf(" Error.\n"));
278 return E_SYNTAX;
279 }
280}
281
282
283#ifdef DEBUG
284
285/* DEBUG OUTPUT */
286
287void
288dumptree(g, n)
289 grammar *g;
290 node *n;
291{
292 int i;
293
294 if (n == NULL)
295 printf("NIL");
296 else {
297 label l;
298 l.lb_type = TYPE(n);
299 l.lb_str = TYPE(str);
300 printf("%s", labelrepr(&l));
301 if (ISNONTERMINAL(TYPE(n))) {
302 printf("(");
303 for (i = 0; i < NCH(n); i++) {
304 if (i > 0)
305 printf(",");
306 dumptree(g, CHILD(n, i));
307 }
308 printf(")");
309 }
310 }
311}
312
313void
314showtree(g, n)
315 grammar *g;
316 node *n;
317{
318 int i;
319
320 if (n == NULL)
321 return;
322 if (ISNONTERMINAL(TYPE(n))) {
323 for (i = 0; i < NCH(n); i++)
324 showtree(g, CHILD(n, i));
325 }
326 else if (ISTERMINAL(TYPE(n))) {
327 printf("%s", tok_name[TYPE(n)]);
328 if (TYPE(n) == NUMBER || TYPE(n) == NAME)
329 printf("(%s)", STR(n));
330 printf(" ");
331 }
332 else
333 printf("? ");
334}
335
336void
337printtree(ps)
338 parser_state *ps;
339{
340 if (debugging) {
341 printf("Parse tree:\n");
342 dumptree(ps->p_grammar, ps->p_tree);
343 printf("\n");
344 printf("Tokens:\n");
345 showtree(ps->p_grammar, ps->p_tree);
346 printf("\n");
347 }
348 printf("Listing:\n");
349 listtree(ps->p_tree);
350 printf("\n");
351}
352
353#endif /* DEBUG */
354
355/*
356
357Description
358-----------
359
360The parser's interface is different than usual: the function addtoken()
361must be called for each token in the input. This makes it possible to
362turn it into an incremental parsing system later. The parsing system
363constructs a parse tree as it goes.
364
365A parsing rule is represented as a Deterministic Finite-state Automaton
366(DFA). A node in a DFA represents a state of the parser; an arc represents
367a transition. Transitions are either labeled with terminal symbols or
368with non-terminals. When the parser decides to follow an arc labeled
369with a non-terminal, it is invoked recursively with the DFA representing
370the parsing rule for that as its initial state; when that DFA accepts,
371the parser that invoked it continues. The parse tree constructed by the
372recursively called parser is inserted as a child in the current parse tree.
373
374The DFA's can be constructed automatically from a more conventional
375language description. An extended LL(1) grammar (ELL(1)) is suitable.
376Certain restrictions make the parser's life easier: rules that can produce
377the empty string should be outlawed (there are other ways to put loops
378or optional parts in the language). To avoid the need to construct
379FIRST sets, we can require that all but the last alternative of a rule
380(really: arc going out of a DFA's state) must begin with a terminal
381symbol.
382
383As an example, consider this grammar:
384
385expr: term (OP term)*
386term: CONSTANT | '(' expr ')'
387
388The DFA corresponding to the rule for expr is:
389
390------->.---term-->.------->
391 ^ |
392 | |
393 \----OP----/
394
395The parse tree generated for the input a+b is:
396
397(expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
398
399*/