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