blob: 47c817f3ff4ae99e0ecc35e80c422a7ec7a4be94 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Parser generator */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003/* XXX This file is not yet fully PROTOized */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004
5/* For a description, see the comments at end of this file */
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 "node.h"
11#include "grammar.h"
12#include "metagrammar.h"
13#include "pgen.h"
14
Guido van Rossum86bea461997-04-29 21:03:06 +000015extern int Py_DebugFlag;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016
17
18/* PART ONE -- CONSTRUCT NFA -- Cf. Algorithm 3.2 from [Aho&Ullman 77] */
19
20typedef struct _nfaarc {
21 int ar_label;
22 int ar_arrow;
23} nfaarc;
24
25typedef struct _nfastate {
26 int st_narcs;
27 nfaarc *st_arc;
28} nfastate;
29
30typedef struct _nfa {
31 int nf_type;
32 char *nf_name;
33 int nf_nstates;
34 nfastate *nf_state;
35 int nf_start, nf_finish;
36} nfa;
37
Guido van Rossum71f477c1991-04-03 19:09:02 +000038/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000039static void compile_rhs(labellist *ll,
40 nfa *nf, node *n, int *pa, int *pb);
41static void compile_alt(labellist *ll,
42 nfa *nf, node *n, int *pa, int *pb);
43static void compile_item(labellist *ll,
44 nfa *nf, node *n, int *pa, int *pb);
45static void compile_atom(labellist *ll,
46 nfa *nf, node *n, int *pa, int *pb);
Guido van Rossum71f477c1991-04-03 19:09:02 +000047
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000049addnfastate(nfa *nf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
51 nfastate *st;
52
Guido van Rossum86bea461997-04-29 21:03:06 +000053 PyMem_RESIZE(nf->nf_state, nfastate, nf->nf_nstates + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 if (nf->nf_state == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000055 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 st = &nf->nf_state[nf->nf_nstates++];
57 st->st_narcs = 0;
58 st->st_arc = NULL;
59 return st - nf->nf_state;
60}
61
62static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000063addnfaarc(nfa *nf, int from, int to, int lbl)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064{
65 nfastate *st;
66 nfaarc *ar;
67
68 st = &nf->nf_state[from];
Guido van Rossum86bea461997-04-29 21:03:06 +000069 PyMem_RESIZE(st->st_arc, nfaarc, st->st_narcs + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 if (st->st_arc == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000071 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 ar = &st->st_arc[st->st_narcs++];
73 ar->ar_label = lbl;
74 ar->ar_arrow = to;
75}
76
77static nfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +000078newnfa(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079{
80 nfa *nf;
Guido van Rossumbb3649e1998-04-10 22:09:39 +000081 static int type = NT_OFFSET; /* All types will be disjunct */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082
Guido van Rossum86bea461997-04-29 21:03:06 +000083 nf = PyMem_NEW(nfa, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 if (nf == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000085 Py_FatalError("no mem for new nfa");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 nf->nf_type = type++;
87 nf->nf_name = name; /* XXX strdup(name) ??? */
88 nf->nf_nstates = 0;
89 nf->nf_state = NULL;
90 nf->nf_start = nf->nf_finish = -1;
91 return nf;
92}
93
94typedef struct _nfagrammar {
95 int gr_nnfas;
96 nfa **gr_nfa;
97 labellist gr_ll;
98} nfagrammar;
99
Guido van Rossum71f477c1991-04-03 19:09:02 +0000100/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000101static void compile_rule(nfagrammar *gr, node *n);
Guido van Rossum71f477c1991-04-03 19:09:02 +0000102
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103static nfagrammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000104newnfagrammar(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105{
106 nfagrammar *gr;
107
Guido van Rossum86bea461997-04-29 21:03:06 +0000108 gr = PyMem_NEW(nfagrammar, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 if (gr == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000110 Py_FatalError("no mem for new nfa grammar");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 gr->gr_nnfas = 0;
112 gr->gr_nfa = NULL;
113 gr->gr_ll.ll_nlabels = 0;
114 gr->gr_ll.ll_label = NULL;
115 addlabel(&gr->gr_ll, ENDMARKER, "EMPTY");
116 return gr;
117}
118
119static nfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000120addnfa(nfagrammar *gr, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
122 nfa *nf;
123
124 nf = newnfa(name);
Guido van Rossum86bea461997-04-29 21:03:06 +0000125 PyMem_RESIZE(gr->gr_nfa, nfa *, gr->gr_nnfas + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 if (gr->gr_nfa == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000127 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 gr->gr_nfa[gr->gr_nnfas++] = nf;
129 addlabel(&gr->gr_ll, NAME, nf->nf_name);
130 return nf;
131}
132
Guido van Rossum408027e1996-12-30 16:17:54 +0000133#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134
135static char REQNFMT[] = "metacompile: less than %d children\n";
136
137#define REQN(i, count) \
138 if (i < count) { \
139 fprintf(stderr, REQNFMT, count); \
Guido van Rossum86bea461997-04-29 21:03:06 +0000140 Py_FatalError("REQN"); \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141 } else
142
143#else
144#define REQN(i, count) /* empty */
145#endif
146
147static nfagrammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000148metacompile(node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149{
150 nfagrammar *gr;
151 int i;
Guido van Rossum25dfe2c2001-09-11 16:43:16 +0000152
153 if (Py_DebugFlag)
154 printf("Compiling (meta-) parse tree into NFA grammar\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 gr = newnfagrammar();
156 REQ(n, MSTART);
157 i = n->n_nchildren - 1; /* Last child is ENDMARKER */
158 n = n->n_child;
159 for (; --i >= 0; n++) {
160 if (n->n_type != NEWLINE)
161 compile_rule(gr, n);
162 }
163 return gr;
164}
165
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000166static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000167compile_rule(nfagrammar *gr, node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168{
169 nfa *nf;
170
171 REQ(n, RULE);
172 REQN(n->n_nchildren, 4);
173 n = n->n_child;
174 REQ(n, NAME);
175 nf = addnfa(gr, n->n_str);
176 n++;
177 REQ(n, COLON);
178 n++;
179 REQ(n, RHS);
180 compile_rhs(&gr->gr_ll, nf, n, &nf->nf_start, &nf->nf_finish);
181 n++;
182 REQ(n, NEWLINE);
183}
184
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000185static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000186compile_rhs(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
188 int i;
189 int a, b;
190
191 REQ(n, RHS);
192 i = n->n_nchildren;
193 REQN(i, 1);
194 n = n->n_child;
195 REQ(n, ALT);
196 compile_alt(ll, nf, n, pa, pb);
197 if (--i <= 0)
198 return;
199 n++;
200 a = *pa;
201 b = *pb;
202 *pa = addnfastate(nf);
203 *pb = addnfastate(nf);
204 addnfaarc(nf, *pa, a, EMPTY);
205 addnfaarc(nf, b, *pb, EMPTY);
206 for (; --i >= 0; n++) {
207 REQ(n, VBAR);
208 REQN(i, 1);
209 --i;
210 n++;
211 REQ(n, ALT);
212 compile_alt(ll, nf, n, &a, &b);
213 addnfaarc(nf, *pa, a, EMPTY);
214 addnfaarc(nf, b, *pb, EMPTY);
215 }
216}
217
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000218static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000219compile_alt(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220{
221 int i;
222 int a, b;
223
224 REQ(n, ALT);
225 i = n->n_nchildren;
226 REQN(i, 1);
227 n = n->n_child;
228 REQ(n, ITEM);
229 compile_item(ll, nf, n, pa, pb);
230 --i;
231 n++;
232 for (; --i >= 0; n++) {
233 if (n->n_type == COMMA) { /* XXX Temporary */
234 REQN(i, 1);
235 --i;
236 n++;
237 }
238 REQ(n, ITEM);
239 compile_item(ll, nf, n, &a, &b);
240 addnfaarc(nf, *pb, a, EMPTY);
241 *pb = b;
242 }
243}
244
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000245static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000246compile_item(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247{
248 int i;
249 int a, b;
250
251 REQ(n, ITEM);
252 i = n->n_nchildren;
253 REQN(i, 1);
254 n = n->n_child;
255 if (n->n_type == LSQB) {
256 REQN(i, 3);
257 n++;
258 REQ(n, RHS);
259 *pa = addnfastate(nf);
260 *pb = addnfastate(nf);
261 addnfaarc(nf, *pa, *pb, EMPTY);
262 compile_rhs(ll, nf, n, &a, &b);
263 addnfaarc(nf, *pa, a, EMPTY);
264 addnfaarc(nf, b, *pb, EMPTY);
265 REQN(i, 1);
266 n++;
267 REQ(n, RSQB);
268 }
269 else {
270 compile_atom(ll, nf, n, pa, pb);
271 if (--i <= 0)
272 return;
273 n++;
274 addnfaarc(nf, *pb, *pa, EMPTY);
275 if (n->n_type == STAR)
276 *pb = *pa;
277 else
278 REQ(n, PLUS);
279 }
280}
281
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000282static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000283compile_atom(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284{
285 int i;
286
287 REQ(n, ATOM);
288 i = n->n_nchildren;
289 REQN(i, 1);
290 n = n->n_child;
291 if (n->n_type == LPAR) {
292 REQN(i, 3);
293 n++;
294 REQ(n, RHS);
295 compile_rhs(ll, nf, n, pa, pb);
296 n++;
297 REQ(n, RPAR);
298 }
299 else if (n->n_type == NAME || n->n_type == STRING) {
300 *pa = addnfastate(nf);
301 *pb = addnfastate(nf);
302 addnfaarc(nf, *pa, *pb, addlabel(ll, n->n_type, n->n_str));
303 }
304 else
305 REQ(n, NAME);
306}
307
308static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000309dumpstate(labellist *ll, nfa *nf, int istate)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310{
311 nfastate *st;
312 int i;
313 nfaarc *ar;
314
315 printf("%c%2d%c",
316 istate == nf->nf_start ? '*' : ' ',
317 istate,
318 istate == nf->nf_finish ? '.' : ' ');
319 st = &nf->nf_state[istate];
320 ar = st->st_arc;
321 for (i = 0; i < st->st_narcs; i++) {
322 if (i > 0)
323 printf("\n ");
324 printf("-> %2d %s", ar->ar_arrow,
Guido van Rossum86bea461997-04-29 21:03:06 +0000325 PyGrammar_LabelRepr(&ll->ll_label[ar->ar_label]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326 ar++;
327 }
328 printf("\n");
329}
330
331static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000332dumpnfa(labellist *ll, nfa *nf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333{
334 int i;
335
336 printf("NFA '%s' has %d states; start %d, finish %d\n",
337 nf->nf_name, nf->nf_nstates, nf->nf_start, nf->nf_finish);
338 for (i = 0; i < nf->nf_nstates; i++)
339 dumpstate(ll, nf, i);
340}
341
342
343/* PART TWO -- CONSTRUCT DFA -- Algorithm 3.1 from [Aho&Ullman 77] */
344
Guido van Rossum588633d1994-12-30 15:46:02 +0000345static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000346addclosure(bitset ss, nfa *nf, int istate)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347{
348 if (addbit(ss, istate)) {
349 nfastate *st = &nf->nf_state[istate];
350 nfaarc *ar = st->st_arc;
351 int i;
352
353 for (i = st->st_narcs; --i >= 0; ) {
354 if (ar->ar_label == EMPTY)
355 addclosure(ss, nf, ar->ar_arrow);
356 ar++;
357 }
358 }
359}
360
361typedef struct _ss_arc {
362 bitset sa_bitset;
363 int sa_arrow;
364 int sa_label;
365} ss_arc;
366
367typedef struct _ss_state {
368 bitset ss_ss;
369 int ss_narcs;
370 ss_arc *ss_arc;
371 int ss_deleted;
372 int ss_finish;
373 int ss_rename;
374} ss_state;
375
376typedef struct _ss_dfa {
377 int sd_nstates;
378 ss_state *sd_state;
379} ss_dfa;
380
Guido van Rossum71f477c1991-04-03 19:09:02 +0000381/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000382static void printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
383 labellist *ll, char *msg);
384static void simplify(int xx_nstates, ss_state *xx_state);
385static void convert(dfa *d, int xx_nstates, ss_state *xx_state);
Guido van Rossum71f477c1991-04-03 19:09:02 +0000386
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000387static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000388makedfa(nfagrammar *gr, nfa *nf, dfa *d)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389{
390 int nbits = nf->nf_nstates;
391 bitset ss;
392 int xx_nstates;
393 ss_state *xx_state, *yy;
394 ss_arc *zz;
395 int istate, jstate, iarc, jarc, ibit;
396 nfastate *st;
397 nfaarc *ar;
398
399 ss = newbitset(nbits);
400 addclosure(ss, nf, nf->nf_start);
Guido van Rossum86bea461997-04-29 21:03:06 +0000401 xx_state = PyMem_NEW(ss_state, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402 if (xx_state == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000403 Py_FatalError("no mem for xx_state in makedfa");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404 xx_nstates = 1;
405 yy = &xx_state[0];
406 yy->ss_ss = ss;
407 yy->ss_narcs = 0;
408 yy->ss_arc = NULL;
409 yy->ss_deleted = 0;
410 yy->ss_finish = testbit(ss, nf->nf_finish);
411 if (yy->ss_finish)
412 printf("Error: nonterminal '%s' may produce empty.\n",
413 nf->nf_name);
414
415 /* This algorithm is from a book written before
416 the invention of structured programming... */
417
418 /* For each unmarked state... */
419 for (istate = 0; istate < xx_nstates; ++istate) {
420 yy = &xx_state[istate];
421 ss = yy->ss_ss;
422 /* For all its states... */
423 for (ibit = 0; ibit < nf->nf_nstates; ++ibit) {
424 if (!testbit(ss, ibit))
425 continue;
426 st = &nf->nf_state[ibit];
427 /* For all non-empty arcs from this state... */
428 for (iarc = 0; iarc < st->st_narcs; iarc++) {
429 ar = &st->st_arc[iarc];
430 if (ar->ar_label == EMPTY)
431 continue;
432 /* Look up in list of arcs from this state */
433 for (jarc = 0; jarc < yy->ss_narcs; ++jarc) {
434 zz = &yy->ss_arc[jarc];
435 if (ar->ar_label == zz->sa_label)
436 goto found;
437 }
438 /* Add new arc for this state */
Guido van Rossum86bea461997-04-29 21:03:06 +0000439 PyMem_RESIZE(yy->ss_arc, ss_arc,
440 yy->ss_narcs + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441 if (yy->ss_arc == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000442 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443 zz = &yy->ss_arc[yy->ss_narcs++];
444 zz->sa_label = ar->ar_label;
445 zz->sa_bitset = newbitset(nbits);
446 zz->sa_arrow = -1;
447 found: ;
448 /* Add destination */
449 addclosure(zz->sa_bitset, nf, ar->ar_arrow);
450 }
451 }
452 /* Now look up all the arrow states */
453 for (jarc = 0; jarc < xx_state[istate].ss_narcs; jarc++) {
454 zz = &xx_state[istate].ss_arc[jarc];
455 for (jstate = 0; jstate < xx_nstates; jstate++) {
456 if (samebitset(zz->sa_bitset,
457 xx_state[jstate].ss_ss, nbits)) {
458 zz->sa_arrow = jstate;
459 goto done;
460 }
461 }
Guido van Rossum86bea461997-04-29 21:03:06 +0000462 PyMem_RESIZE(xx_state, ss_state, xx_nstates + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 if (xx_state == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000464 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 zz->sa_arrow = xx_nstates;
466 yy = &xx_state[xx_nstates++];
467 yy->ss_ss = zz->sa_bitset;
468 yy->ss_narcs = 0;
469 yy->ss_arc = NULL;
470 yy->ss_deleted = 0;
471 yy->ss_finish = testbit(yy->ss_ss, nf->nf_finish);
472 done: ;
473 }
474 }
475
Guido van Rossum86bea461997-04-29 21:03:06 +0000476 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477 printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
478 "before minimizing");
479
480 simplify(xx_nstates, xx_state);
481
Guido van Rossum86bea461997-04-29 21:03:06 +0000482 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483 printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
484 "after minimizing");
485
486 convert(d, xx_nstates, xx_state);
487
488 /* XXX cleanup */
489}
490
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000491static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000492printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
493 labellist *ll, char *msg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494{
495 int i, ibit, iarc;
496 ss_state *yy;
497 ss_arc *zz;
498
499 printf("Subset DFA %s\n", msg);
500 for (i = 0; i < xx_nstates; i++) {
501 yy = &xx_state[i];
502 if (yy->ss_deleted)
503 continue;
504 printf(" Subset %d", i);
505 if (yy->ss_finish)
506 printf(" (finish)");
507 printf(" { ");
508 for (ibit = 0; ibit < nbits; ibit++) {
509 if (testbit(yy->ss_ss, ibit))
510 printf("%d ", ibit);
511 }
512 printf("}\n");
513 for (iarc = 0; iarc < yy->ss_narcs; iarc++) {
514 zz = &yy->ss_arc[iarc];
515 printf(" Arc to state %d, label %s\n",
516 zz->sa_arrow,
Guido van Rossum86bea461997-04-29 21:03:06 +0000517 PyGrammar_LabelRepr(
518 &ll->ll_label[zz->sa_label]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519 }
520 }
521}
522
523
524/* PART THREE -- SIMPLIFY DFA */
525
526/* Simplify the DFA by repeatedly eliminating states that are
527 equivalent to another oner. This is NOT Algorithm 3.3 from
528 [Aho&Ullman 77]. It does not always finds the minimal DFA,
529 but it does usually make a much smaller one... (For an example
Thomas Wouters7e474022000-07-16 12:04:32 +0000530 of sub-optimal behavior, try S: x a b+ | y a b+.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531*/
532
533static int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000534samestate(ss_state *s1, ss_state *s2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535{
536 int i;
537
538 if (s1->ss_narcs != s2->ss_narcs || s1->ss_finish != s2->ss_finish)
539 return 0;
540 for (i = 0; i < s1->ss_narcs; i++) {
541 if (s1->ss_arc[i].sa_arrow != s2->ss_arc[i].sa_arrow ||
542 s1->ss_arc[i].sa_label != s2->ss_arc[i].sa_label)
543 return 0;
544 }
545 return 1;
546}
547
548static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000549renamestates(int xx_nstates, ss_state *xx_state, int from, int to)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550{
551 int i, j;
552
Guido van Rossum86bea461997-04-29 21:03:06 +0000553 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554 printf("Rename state %d to %d.\n", from, to);
555 for (i = 0; i < xx_nstates; i++) {
556 if (xx_state[i].ss_deleted)
557 continue;
558 for (j = 0; j < xx_state[i].ss_narcs; j++) {
559 if (xx_state[i].ss_arc[j].sa_arrow == from)
560 xx_state[i].ss_arc[j].sa_arrow = to;
561 }
562 }
563}
564
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000565static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000566simplify(int xx_nstates, ss_state *xx_state)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567{
568 int changes;
Guido van Rossum588633d1994-12-30 15:46:02 +0000569 int i, j;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570
571 do {
572 changes = 0;
573 for (i = 1; i < xx_nstates; i++) {
574 if (xx_state[i].ss_deleted)
575 continue;
576 for (j = 0; j < i; j++) {
577 if (xx_state[j].ss_deleted)
578 continue;
579 if (samestate(&xx_state[i], &xx_state[j])) {
580 xx_state[i].ss_deleted++;
Guido van Rossum86bea461997-04-29 21:03:06 +0000581 renamestates(xx_nstates, xx_state,
582 i, j);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583 changes++;
584 break;
585 }
586 }
587 }
588 } while (changes);
589}
590
591
592/* PART FOUR -- GENERATE PARSING TABLES */
593
594/* Convert the DFA into a grammar that can be used by our parser */
595
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000596static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000597convert(dfa *d, int xx_nstates, ss_state *xx_state)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598{
599 int i, j;
600 ss_state *yy;
601 ss_arc *zz;
602
603 for (i = 0; i < xx_nstates; i++) {
604 yy = &xx_state[i];
605 if (yy->ss_deleted)
606 continue;
607 yy->ss_rename = addstate(d);
608 }
609
610 for (i = 0; i < xx_nstates; i++) {
611 yy = &xx_state[i];
612 if (yy->ss_deleted)
613 continue;
614 for (j = 0; j < yy->ss_narcs; j++) {
615 zz = &yy->ss_arc[j];
616 addarc(d, yy->ss_rename,
617 xx_state[zz->sa_arrow].ss_rename,
618 zz->sa_label);
619 }
620 if (yy->ss_finish)
621 addarc(d, yy->ss_rename, yy->ss_rename, 0);
622 }
623
624 d->d_initial = 0;
625}
626
627
628/* PART FIVE -- GLUE IT ALL TOGETHER */
629
630static grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000631maketables(nfagrammar *gr)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632{
633 int i;
634 nfa *nf;
635 dfa *d;
636 grammar *g;
637
638 if (gr->gr_nnfas == 0)
639 return NULL;
640 g = newgrammar(gr->gr_nfa[0]->nf_type);
641 /* XXX first rule must be start rule */
642 g->g_ll = gr->gr_ll;
643
644 for (i = 0; i < gr->gr_nnfas; i++) {
645 nf = gr->gr_nfa[i];
Guido van Rossum86bea461997-04-29 21:03:06 +0000646 if (Py_DebugFlag) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647 printf("Dump of NFA for '%s' ...\n", nf->nf_name);
648 dumpnfa(&gr->gr_ll, nf);
Guido van Rossum25dfe2c2001-09-11 16:43:16 +0000649 printf("Making DFA for '%s' ...\n", nf->nf_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651 d = adddfa(g, nf->nf_type, nf->nf_name);
652 makedfa(gr, gr->gr_nfa[i], d);
653 }
654
655 return g;
656}
657
658grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000659pgen(node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660{
661 nfagrammar *gr;
662 grammar *g;
663
664 gr = metacompile(n);
665 g = maketables(gr);
666 translatelabels(g);
667 addfirstsets(g);
668 return g;
669}
670
671
672/*
673
674Description
675-----------
676
677Input is a grammar in extended BNF (using * for repetition, + for
678at-least-once repetition, [] for optional parts, | for alternatives and
679() for grouping). This has already been parsed and turned into a parse
680tree.
681
682Each rule is considered as a regular expression in its own right.
683It is turned into a Non-deterministic Finite Automaton (NFA), which
684is then turned into a Deterministic Finite Automaton (DFA), which is then
685optimized to reduce the number of states. See [Aho&Ullman 77] chapter 3,
686or similar compiler books (this technique is more often used for lexical
687analyzers).
688
689The DFA's are used by the parser as parsing tables in a special way
690that's probably unique. Before they are usable, the FIRST sets of all
691non-terminals are computed.
692
693Reference
694---------
695
696[Aho&Ullman 77]
697 Aho&Ullman, Principles of Compiler Design, Addison-Wesley 1977
698 (first edition)
699
700*/