blob: b2f84709fc42c6cf7fa0800d459b1f0393ba9632 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Parser generator */
2
3/* For a description, see the comments at end of this file */
4
Tim Peters1ca12962001-12-04 03:18:48 +00005#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.h"
8#include "node.h"
9#include "grammar.h"
10#include "metagrammar.h"
11#include "pgen.h"
12
Guido van Rossum86bea461997-04-29 21:03:06 +000013extern int Py_DebugFlag;
Neal Norwitz5352d8c2002-05-31 13:11:40 +000014extern int Py_IgnoreEnvironmentFlag; /* needed by Py_GETENV */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
16
17/* PART ONE -- CONSTRUCT NFA -- Cf. Algorithm 3.2 from [Aho&Ullman 77] */
18
19typedef struct _nfaarc {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000020 int ar_label;
21 int ar_arrow;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022} nfaarc;
23
24typedef struct _nfastate {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000025 int st_narcs;
26 nfaarc *st_arc;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027} nfastate;
28
29typedef struct _nfa {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000030 int nf_type;
31 char *nf_name;
32 int nf_nstates;
33 nfastate *nf_state;
34 int nf_start, nf_finish;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035} nfa;
36
Guido van Rossum71f477c1991-04-03 19:09:02 +000037/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000038static void compile_rhs(labellist *ll,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000039 nfa *nf, node *n, int *pa, int *pb);
Tim Petersdbd9ba62000-07-09 03:09:57 +000040static void compile_alt(labellist *ll,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041 nfa *nf, node *n, int *pa, int *pb);
Tim Petersdbd9ba62000-07-09 03:09:57 +000042static void compile_item(labellist *ll,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043 nfa *nf, node *n, int *pa, int *pb);
Tim Petersdbd9ba62000-07-09 03:09:57 +000044static void compile_atom(labellist *ll,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000045 nfa *nf, node *n, int *pa, int *pb);
Guido van Rossum71f477c1991-04-03 19:09:02 +000046
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000048addnfastate(nfa *nf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000050 nfastate *st;
51
52 nf->nf_state = (nfastate *)PyObject_REALLOC(nf->nf_state,
53 sizeof(nfastate) * (nf->nf_nstates + 1));
54 if (nf->nf_state == NULL)
55 Py_FatalError("out of mem");
56 st = &nf->nf_state[nf->nf_nstates++];
57 st->st_narcs = 0;
58 st->st_arc = NULL;
59 return st - nf->nf_state;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 nfastate *st;
66 nfaarc *ar;
67
68 st = &nf->nf_state[from];
69 st->st_arc = (nfaarc *)PyObject_REALLOC(st->st_arc,
70 sizeof(nfaarc) * (st->st_narcs + 1));
71 if (st->st_arc == NULL)
72 Py_FatalError("out of mem");
73 ar = &st->st_arc[st->st_narcs++];
74 ar->ar_label = lbl;
75 ar->ar_arrow = to;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076}
77
78static nfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +000079newnfa(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 nfa *nf;
82 static int type = NT_OFFSET; /* All types will be disjunct */
83
84 nf = (nfa *)PyObject_MALLOC(sizeof(nfa));
85 if (nf == NULL)
86 Py_FatalError("no mem for new nfa");
87 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;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093}
94
95typedef struct _nfagrammar {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000096 int gr_nnfas;
97 nfa **gr_nfa;
98 labellist gr_ll;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099} 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000107 nfagrammar *gr;
108
109 gr = (nfagrammar *)PyObject_MALLOC(sizeof(nfagrammar));
110 if (gr == NULL)
111 Py_FatalError("no mem for new nfa grammar");
112 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;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118}
119
120static nfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000121addnfa(nfagrammar *gr, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000123 nfa *nf;
124
125 nf = newnfa(name);
126 gr->gr_nfa = (nfa **)PyObject_REALLOC(gr->gr_nfa,
127 sizeof(nfa*) * (gr->gr_nnfas + 1));
128 if (gr->gr_nfa == NULL)
129 Py_FatalError("out of mem");
130 gr->gr_nfa[gr->gr_nnfas++] = nf;
131 addlabel(&gr->gr_ll, NAME, nf->nf_name);
132 return nf;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Guido van Rossum408027e1996-12-30 16:17:54 +0000135#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136
137static char REQNFMT[] = "metacompile: less than %d children\n";
138
139#define REQN(i, count) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000140 if (i < count) { \
141 fprintf(stderr, REQNFMT, count); \
142 Py_FatalError("REQN"); \
143 } else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
145#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000146#define REQN(i, count) /* empty */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147#endif
148
149static nfagrammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000150metacompile(node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 nfagrammar *gr;
153 int i;
Guido van Rossum25dfe2c2001-09-11 16:43:16 +0000154
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000155 if (Py_DebugFlag)
156 printf("Compiling (meta-) parse tree into NFA grammar\n");
157 gr = newnfagrammar();
158 REQ(n, MSTART);
159 i = n->n_nchildren - 1; /* Last child is ENDMARKER */
160 n = n->n_child;
161 for (; --i >= 0; n++) {
162 if (n->n_type != NEWLINE)
163 compile_rule(gr, n);
164 }
165 return gr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
167
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000168static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000169compile_rule(nfagrammar *gr, node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000171 nfa *nf;
172
173 REQ(n, RULE);
174 REQN(n->n_nchildren, 4);
175 n = n->n_child;
176 REQ(n, NAME);
177 nf = addnfa(gr, n->n_str);
178 n++;
179 REQ(n, COLON);
180 n++;
181 REQ(n, RHS);
182 compile_rhs(&gr->gr_ll, nf, n, &nf->nf_start, &nf->nf_finish);
183 n++;
184 REQ(n, NEWLINE);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185}
186
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000187static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000188compile_rhs(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 int i;
191 int a, b;
192
193 REQ(n, RHS);
194 i = n->n_nchildren;
195 REQN(i, 1);
196 n = n->n_child;
197 REQ(n, ALT);
198 compile_alt(ll, nf, n, pa, pb);
199 if (--i <= 0)
200 return;
201 n++;
202 a = *pa;
203 b = *pb;
204 *pa = addnfastate(nf);
205 *pb = addnfastate(nf);
206 addnfaarc(nf, *pa, a, EMPTY);
207 addnfaarc(nf, b, *pb, EMPTY);
208 for (; --i >= 0; n++) {
209 REQ(n, VBAR);
210 REQN(i, 1);
211 --i;
212 n++;
213 REQ(n, ALT);
214 compile_alt(ll, nf, n, &a, &b);
215 addnfaarc(nf, *pa, a, EMPTY);
216 addnfaarc(nf, b, *pb, EMPTY);
217 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000220static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000221compile_alt(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 int i;
224 int a, b;
225
226 REQ(n, ALT);
227 i = n->n_nchildren;
228 REQN(i, 1);
229 n = n->n_child;
230 REQ(n, ITEM);
231 compile_item(ll, nf, n, pa, pb);
232 --i;
233 n++;
234 for (; --i >= 0; n++) {
235 REQ(n, ITEM);
236 compile_item(ll, nf, n, &a, &b);
237 addnfaarc(nf, *pb, a, EMPTY);
238 *pb = b;
239 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240}
241
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000242static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000243compile_item(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 int i;
246 int a, b;
247
248 REQ(n, ITEM);
249 i = n->n_nchildren;
250 REQN(i, 1);
251 n = n->n_child;
252 if (n->n_type == LSQB) {
253 REQN(i, 3);
254 n++;
255 REQ(n, RHS);
256 *pa = addnfastate(nf);
257 *pb = addnfastate(nf);
258 addnfaarc(nf, *pa, *pb, EMPTY);
259 compile_rhs(ll, nf, n, &a, &b);
260 addnfaarc(nf, *pa, a, EMPTY);
261 addnfaarc(nf, b, *pb, EMPTY);
262 REQN(i, 1);
263 n++;
264 REQ(n, RSQB);
265 }
266 else {
267 compile_atom(ll, nf, n, pa, pb);
268 if (--i <= 0)
269 return;
270 n++;
271 addnfaarc(nf, *pb, *pa, EMPTY);
272 if (n->n_type == STAR)
273 *pb = *pa;
274 else
275 REQ(n, PLUS);
276 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000279static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000280compile_atom(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000282 int i;
283
284 REQ(n, ATOM);
285 i = n->n_nchildren;
Martin Panter646b5282016-06-21 23:58:05 +0000286 (void)i; /* Don't warn about set but unused */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 REQN(i, 1);
288 n = n->n_child;
289 if (n->n_type == LPAR) {
290 REQN(i, 3);
291 n++;
292 REQ(n, RHS);
293 compile_rhs(ll, nf, n, pa, pb);
294 n++;
295 REQ(n, RPAR);
296 }
297 else if (n->n_type == NAME || n->n_type == STRING) {
298 *pa = addnfastate(nf);
299 *pb = addnfastate(nf);
300 addnfaarc(nf, *pa, *pb, addlabel(ll, n->n_type, n->n_str));
301 }
302 else
303 REQ(n, NAME);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304}
305
306static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000307dumpstate(labellist *ll, nfa *nf, int istate)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 nfastate *st;
310 int i;
311 nfaarc *ar;
312
313 printf("%c%2d%c",
314 istate == nf->nf_start ? '*' : ' ',
315 istate,
316 istate == nf->nf_finish ? '.' : ' ');
317 st = &nf->nf_state[istate];
318 ar = st->st_arc;
319 for (i = 0; i < st->st_narcs; i++) {
320 if (i > 0)
321 printf("\n ");
322 printf("-> %2d %s", ar->ar_arrow,
323 PyGrammar_LabelRepr(&ll->ll_label[ar->ar_label]));
324 ar++;
325 }
326 printf("\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327}
328
329static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000330dumpnfa(labellist *ll, nfa *nf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 int i;
333
334 printf("NFA '%s' has %d states; start %d, finish %d\n",
335 nf->nf_name, nf->nf_nstates, nf->nf_start, nf->nf_finish);
336 for (i = 0; i < nf->nf_nstates; i++)
337 dumpstate(ll, nf, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338}
339
340
341/* PART TWO -- CONSTRUCT DFA -- Algorithm 3.1 from [Aho&Ullman 77] */
342
Guido van Rossum588633d1994-12-30 15:46:02 +0000343static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000344addclosure(bitset ss, nfa *nf, int istate)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 if (addbit(ss, istate)) {
347 nfastate *st = &nf->nf_state[istate];
348 nfaarc *ar = st->st_arc;
349 int i;
350
351 for (i = st->st_narcs; --i >= 0; ) {
352 if (ar->ar_label == EMPTY)
353 addclosure(ss, nf, ar->ar_arrow);
354 ar++;
355 }
356 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357}
358
359typedef struct _ss_arc {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000360 bitset sa_bitset;
361 int sa_arrow;
362 int sa_label;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363} ss_arc;
364
365typedef struct _ss_state {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000366 bitset ss_ss;
367 int ss_narcs;
368 struct _ss_arc *ss_arc;
369 int ss_deleted;
370 int ss_finish;
371 int ss_rename;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372} ss_state;
373
374typedef struct _ss_dfa {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000375 int sd_nstates;
376 ss_state *sd_state;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377} ss_dfa;
378
Guido van Rossum71f477c1991-04-03 19:09:02 +0000379/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000380static void printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000381 labellist *ll, char *msg);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000382static void simplify(int xx_nstates, ss_state *xx_state);
383static void convert(dfa *d, int xx_nstates, ss_state *xx_state);
Guido van Rossum71f477c1991-04-03 19:09:02 +0000384
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000385static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000386makedfa(nfagrammar *gr, nfa *nf, dfa *d)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 int nbits = nf->nf_nstates;
389 bitset ss;
390 int xx_nstates;
391 ss_state *xx_state, *yy;
392 ss_arc *zz;
393 int istate, jstate, iarc, jarc, ibit;
394 nfastate *st;
395 nfaarc *ar;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 ss = newbitset(nbits);
398 addclosure(ss, nf, nf->nf_start);
399 xx_state = (ss_state *)PyObject_MALLOC(sizeof(ss_state));
400 if (xx_state == NULL)
401 Py_FatalError("no mem for xx_state in makedfa");
402 xx_nstates = 1;
403 yy = &xx_state[0];
404 yy->ss_ss = ss;
405 yy->ss_narcs = 0;
406 yy->ss_arc = NULL;
407 yy->ss_deleted = 0;
408 yy->ss_finish = testbit(ss, nf->nf_finish);
409 if (yy->ss_finish)
410 printf("Error: nonterminal '%s' may produce empty.\n",
411 nf->nf_name);
412
413 /* This algorithm is from a book written before
414 the invention of structured programming... */
415
416 /* For each unmarked state... */
417 for (istate = 0; istate < xx_nstates; ++istate) {
418 size_t size;
419 yy = &xx_state[istate];
420 ss = yy->ss_ss;
421 /* For all its states... */
422 for (ibit = 0; ibit < nf->nf_nstates; ++ibit) {
423 if (!testbit(ss, ibit))
424 continue;
425 st = &nf->nf_state[ibit];
426 /* For all non-empty arcs from this state... */
427 for (iarc = 0; iarc < st->st_narcs; iarc++) {
428 ar = &st->st_arc[iarc];
429 if (ar->ar_label == EMPTY)
430 continue;
431 /* Look up in list of arcs from this state */
432 for (jarc = 0; jarc < yy->ss_narcs; ++jarc) {
433 zz = &yy->ss_arc[jarc];
434 if (ar->ar_label == zz->sa_label)
435 goto found;
436 }
437 /* Add new arc for this state */
438 size = sizeof(ss_arc) * (yy->ss_narcs + 1);
439 yy->ss_arc = (ss_arc *)PyObject_REALLOC(
440 yy->ss_arc, size);
441 if (yy->ss_arc == NULL)
442 Py_FatalError("out of mem");
443 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 }
462 size = sizeof(ss_state) * (xx_nstates + 1);
463 xx_state = (ss_state *)PyObject_REALLOC(xx_state,
464 size);
465 if (xx_state == NULL)
466 Py_FatalError("out of mem");
467 zz->sa_arrow = xx_nstates;
468 yy = &xx_state[xx_nstates++];
469 yy->ss_ss = zz->sa_bitset;
470 yy->ss_narcs = 0;
471 yy->ss_arc = NULL;
472 yy->ss_deleted = 0;
473 yy->ss_finish = testbit(yy->ss_ss, nf->nf_finish);
474 done: ;
475 }
476 }
477
478 if (Py_DebugFlag)
479 printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
480 "before minimizing");
481
482 simplify(xx_nstates, xx_state);
483
484 if (Py_DebugFlag)
485 printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
486 "after minimizing");
487
488 convert(d, xx_nstates, xx_state);
489
490 /* XXX cleanup */
491 PyObject_FREE(xx_state);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000492}
493
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000494static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000495printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000496 labellist *ll, char *msg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000498 int i, ibit, iarc;
499 ss_state *yy;
500 ss_arc *zz;
501
502 printf("Subset DFA %s\n", msg);
503 for (i = 0; i < xx_nstates; i++) {
504 yy = &xx_state[i];
505 if (yy->ss_deleted)
506 continue;
507 printf(" Subset %d", i);
508 if (yy->ss_finish)
509 printf(" (finish)");
510 printf(" { ");
511 for (ibit = 0; ibit < nbits; ibit++) {
512 if (testbit(yy->ss_ss, ibit))
513 printf("%d ", ibit);
514 }
515 printf("}\n");
516 for (iarc = 0; iarc < yy->ss_narcs; iarc++) {
517 zz = &yy->ss_arc[iarc];
518 printf(" Arc to state %d, label %s\n",
519 zz->sa_arrow,
520 PyGrammar_LabelRepr(
521 &ll->ll_label[zz->sa_label]));
522 }
523 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524}
525
526
527/* PART THREE -- SIMPLIFY DFA */
528
529/* Simplify the DFA by repeatedly eliminating states that are
530 equivalent to another oner. This is NOT Algorithm 3.3 from
531 [Aho&Ullman 77]. It does not always finds the minimal DFA,
532 but it does usually make a much smaller one... (For an example
Thomas Wouters7e474022000-07-16 12:04:32 +0000533 of sub-optimal behavior, try S: x a b+ | y a b+.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534*/
535
536static int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000537samestate(ss_state *s1, ss_state *s2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000539 int i;
540
541 if (s1->ss_narcs != s2->ss_narcs || s1->ss_finish != s2->ss_finish)
542 return 0;
543 for (i = 0; i < s1->ss_narcs; i++) {
544 if (s1->ss_arc[i].sa_arrow != s2->ss_arc[i].sa_arrow ||
545 s1->ss_arc[i].sa_label != s2->ss_arc[i].sa_label)
546 return 0;
547 }
548 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549}
550
551static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000552renamestates(int xx_nstates, ss_state *xx_state, int from, int to)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000554 int i, j;
555
556 if (Py_DebugFlag)
557 printf("Rename state %d to %d.\n", from, to);
558 for (i = 0; i < xx_nstates; i++) {
559 if (xx_state[i].ss_deleted)
560 continue;
561 for (j = 0; j < xx_state[i].ss_narcs; j++) {
562 if (xx_state[i].ss_arc[j].sa_arrow == from)
563 xx_state[i].ss_arc[j].sa_arrow = to;
564 }
565 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566}
567
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000568static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000569simplify(int xx_nstates, ss_state *xx_state)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 int changes;
572 int i, j;
573
574 do {
575 changes = 0;
576 for (i = 1; i < xx_nstates; i++) {
577 if (xx_state[i].ss_deleted)
578 continue;
579 for (j = 0; j < i; j++) {
580 if (xx_state[j].ss_deleted)
581 continue;
582 if (samestate(&xx_state[i], &xx_state[j])) {
583 xx_state[i].ss_deleted++;
584 renamestates(xx_nstates, xx_state,
585 i, j);
586 changes++;
587 break;
588 }
589 }
590 }
591 } while (changes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592}
593
594
595/* PART FOUR -- GENERATE PARSING TABLES */
596
597/* Convert the DFA into a grammar that can be used by our parser */
598
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000599static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000600convert(dfa *d, int xx_nstates, ss_state *xx_state)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 int i, j;
603 ss_state *yy;
604 ss_arc *zz;
605
606 for (i = 0; i < xx_nstates; i++) {
607 yy = &xx_state[i];
608 if (yy->ss_deleted)
609 continue;
610 yy->ss_rename = addstate(d);
611 }
612
613 for (i = 0; i < xx_nstates; i++) {
614 yy = &xx_state[i];
615 if (yy->ss_deleted)
616 continue;
617 for (j = 0; j < yy->ss_narcs; j++) {
618 zz = &yy->ss_arc[j];
619 addarc(d, yy->ss_rename,
620 xx_state[zz->sa_arrow].ss_rename,
621 zz->sa_label);
622 }
623 if (yy->ss_finish)
624 addarc(d, yy->ss_rename, yy->ss_rename, 0);
625 }
626
627 d->d_initial = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628}
629
630
631/* PART FIVE -- GLUE IT ALL TOGETHER */
632
633static grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000634maketables(nfagrammar *gr)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000636 int i;
637 nfa *nf;
638 dfa *d;
639 grammar *g;
640
641 if (gr->gr_nnfas == 0)
642 return NULL;
643 g = newgrammar(gr->gr_nfa[0]->nf_type);
644 /* XXX first rule must be start rule */
645 g->g_ll = gr->gr_ll;
646
647 for (i = 0; i < gr->gr_nnfas; i++) {
648 nf = gr->gr_nfa[i];
649 if (Py_DebugFlag) {
650 printf("Dump of NFA for '%s' ...\n", nf->nf_name);
651 dumpnfa(&gr->gr_ll, nf);
652 printf("Making DFA for '%s' ...\n", nf->nf_name);
653 }
654 d = adddfa(g, nf->nf_type, nf->nf_name);
655 makedfa(gr, gr->gr_nfa[i], d);
656 }
657
658 return g;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000659}
660
661grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000662pgen(node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000663{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 nfagrammar *gr;
665 grammar *g;
666
667 gr = metacompile(n);
668 g = maketables(gr);
669 translatelabels(g);
670 addfirstsets(g);
671 PyObject_FREE(gr);
672 return g;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673}
674
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000675grammar *
676Py_pgen(node *n)
677{
678 return pgen(n);
679}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000680
681/*
682
683Description
684-----------
685
686Input is a grammar in extended BNF (using * for repetition, + for
687at-least-once repetition, [] for optional parts, | for alternatives and
688() for grouping). This has already been parsed and turned into a parse
689tree.
690
691Each rule is considered as a regular expression in its own right.
692It is turned into a Non-deterministic Finite Automaton (NFA), which
693is then turned into a Deterministic Finite Automaton (DFA), which is then
694optimized to reduce the number of states. See [Aho&Ullman 77] chapter 3,
695or similar compiler books (this technique is more often used for lexical
696analyzers).
697
698The DFA's are used by the parser as parsing tables in a special way
699that's probably unique. Before they are usable, the FIRST sets of all
700non-terminals are computed.
701
702Reference
703---------
704
705[Aho&Ullman 77]
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 Aho&Ullman, Principles of Compiler Design, Addison-Wesley 1977
707 (first edition)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000708
709*/