blob: e643d330b4d7f1f9e38a83b9dceba255e896b9df [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 {
20 int ar_label;
21 int ar_arrow;
22} nfaarc;
23
24typedef struct _nfastate {
25 int st_narcs;
26 nfaarc *st_arc;
27} nfastate;
28
29typedef struct _nfa {
30 int nf_type;
31 char *nf_name;
32 int nf_nstates;
33 nfastate *nf_state;
34 int nf_start, nf_finish;
35} 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,
39 nfa *nf, node *n, int *pa, int *pb);
40static void compile_alt(labellist *ll,
41 nfa *nf, node *n, int *pa, int *pb);
42static void compile_item(labellist *ll,
43 nfa *nf, node *n, int *pa, int *pb);
44static void compile_atom(labellist *ll,
45 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{
50 nfastate *st;
51
Guido van Rossum86bea461997-04-29 21:03:06 +000052 PyMem_RESIZE(nf->nf_state, nfastate, nf->nf_nstates + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053 if (nf->nf_state == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000054 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055 st = &nf->nf_state[nf->nf_nstates++];
56 st->st_narcs = 0;
57 st->st_arc = NULL;
58 return st - nf->nf_state;
59}
60
61static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000062addnfaarc(nfa *nf, int from, int to, int lbl)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
64 nfastate *st;
65 nfaarc *ar;
66
67 st = &nf->nf_state[from];
Guido van Rossum86bea461997-04-29 21:03:06 +000068 PyMem_RESIZE(st->st_arc, nfaarc, st->st_narcs + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 if (st->st_arc == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000070 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 ar = &st->st_arc[st->st_narcs++];
72 ar->ar_label = lbl;
73 ar->ar_arrow = to;
74}
75
76static nfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +000077newnfa(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078{
79 nfa *nf;
Guido van Rossumbb3649e1998-04-10 22:09:39 +000080 static int type = NT_OFFSET; /* All types will be disjunct */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081
Guido van Rossum86bea461997-04-29 21:03:06 +000082 nf = PyMem_NEW(nfa, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 if (nf == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000084 Py_FatalError("no mem for new nfa");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 nf->nf_type = type++;
86 nf->nf_name = name; /* XXX strdup(name) ??? */
87 nf->nf_nstates = 0;
88 nf->nf_state = NULL;
89 nf->nf_start = nf->nf_finish = -1;
90 return nf;
91}
92
93typedef struct _nfagrammar {
94 int gr_nnfas;
95 nfa **gr_nfa;
96 labellist gr_ll;
97} nfagrammar;
98
Guido van Rossum71f477c1991-04-03 19:09:02 +000099/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000100static void compile_rule(nfagrammar *gr, node *n);
Guido van Rossum71f477c1991-04-03 19:09:02 +0000101
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102static nfagrammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000103newnfagrammar(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104{
105 nfagrammar *gr;
106
Guido van Rossum86bea461997-04-29 21:03:06 +0000107 gr = PyMem_NEW(nfagrammar, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108 if (gr == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000109 Py_FatalError("no mem for new nfa grammar");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 gr->gr_nnfas = 0;
111 gr->gr_nfa = NULL;
112 gr->gr_ll.ll_nlabels = 0;
113 gr->gr_ll.ll_label = NULL;
114 addlabel(&gr->gr_ll, ENDMARKER, "EMPTY");
115 return gr;
116}
117
118static nfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000119addnfa(nfagrammar *gr, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120{
121 nfa *nf;
122
123 nf = newnfa(name);
Guido van Rossum86bea461997-04-29 21:03:06 +0000124 PyMem_RESIZE(gr->gr_nfa, nfa *, gr->gr_nnfas + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125 if (gr->gr_nfa == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000126 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 gr->gr_nfa[gr->gr_nnfas++] = nf;
128 addlabel(&gr->gr_ll, NAME, nf->nf_name);
129 return nf;
130}
131
Guido van Rossum408027e1996-12-30 16:17:54 +0000132#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133
134static char REQNFMT[] = "metacompile: less than %d children\n";
135
136#define REQN(i, count) \
137 if (i < count) { \
138 fprintf(stderr, REQNFMT, count); \
Guido van Rossum86bea461997-04-29 21:03:06 +0000139 Py_FatalError("REQN"); \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 } else
141
142#else
143#define REQN(i, count) /* empty */
144#endif
145
146static nfagrammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000147metacompile(node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
149 nfagrammar *gr;
150 int i;
Guido van Rossum25dfe2c2001-09-11 16:43:16 +0000151
152 if (Py_DebugFlag)
153 printf("Compiling (meta-) parse tree into NFA grammar\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154 gr = newnfagrammar();
155 REQ(n, MSTART);
156 i = n->n_nchildren - 1; /* Last child is ENDMARKER */
157 n = n->n_child;
158 for (; --i >= 0; n++) {
159 if (n->n_type != NEWLINE)
160 compile_rule(gr, n);
161 }
162 return gr;
163}
164
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000165static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000166compile_rule(nfagrammar *gr, node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167{
168 nfa *nf;
169
170 REQ(n, RULE);
171 REQN(n->n_nchildren, 4);
172 n = n->n_child;
173 REQ(n, NAME);
174 nf = addnfa(gr, n->n_str);
175 n++;
176 REQ(n, COLON);
177 n++;
178 REQ(n, RHS);
179 compile_rhs(&gr->gr_ll, nf, n, &nf->nf_start, &nf->nf_finish);
180 n++;
181 REQ(n, NEWLINE);
182}
183
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000184static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000185compile_rhs(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186{
187 int i;
188 int a, b;
189
190 REQ(n, RHS);
191 i = n->n_nchildren;
192 REQN(i, 1);
193 n = n->n_child;
194 REQ(n, ALT);
195 compile_alt(ll, nf, n, pa, pb);
196 if (--i <= 0)
197 return;
198 n++;
199 a = *pa;
200 b = *pb;
201 *pa = addnfastate(nf);
202 *pb = addnfastate(nf);
203 addnfaarc(nf, *pa, a, EMPTY);
204 addnfaarc(nf, b, *pb, EMPTY);
205 for (; --i >= 0; n++) {
206 REQ(n, VBAR);
207 REQN(i, 1);
208 --i;
209 n++;
210 REQ(n, ALT);
211 compile_alt(ll, nf, n, &a, &b);
212 addnfaarc(nf, *pa, a, EMPTY);
213 addnfaarc(nf, b, *pb, EMPTY);
214 }
215}
216
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000217static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000218compile_alt(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219{
220 int i;
221 int a, b;
222
223 REQ(n, ALT);
224 i = n->n_nchildren;
225 REQN(i, 1);
226 n = n->n_child;
227 REQ(n, ITEM);
228 compile_item(ll, nf, n, pa, pb);
229 --i;
230 n++;
231 for (; --i >= 0; n++) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232 REQ(n, ITEM);
233 compile_item(ll, nf, n, &a, &b);
234 addnfaarc(nf, *pb, a, EMPTY);
235 *pb = b;
236 }
237}
238
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000239static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000240compile_item(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241{
242 int i;
243 int a, b;
244
245 REQ(n, ITEM);
246 i = n->n_nchildren;
247 REQN(i, 1);
248 n = n->n_child;
249 if (n->n_type == LSQB) {
250 REQN(i, 3);
251 n++;
252 REQ(n, RHS);
253 *pa = addnfastate(nf);
254 *pb = addnfastate(nf);
255 addnfaarc(nf, *pa, *pb, EMPTY);
256 compile_rhs(ll, nf, n, &a, &b);
257 addnfaarc(nf, *pa, a, EMPTY);
258 addnfaarc(nf, b, *pb, EMPTY);
259 REQN(i, 1);
260 n++;
261 REQ(n, RSQB);
262 }
263 else {
264 compile_atom(ll, nf, n, pa, pb);
265 if (--i <= 0)
266 return;
267 n++;
268 addnfaarc(nf, *pb, *pa, EMPTY);
269 if (n->n_type == STAR)
270 *pb = *pa;
271 else
272 REQ(n, PLUS);
273 }
274}
275
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000276static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000277compile_atom(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278{
Barry Warsawedaa0712003-02-28 15:27:40 +0000279 int i;
280
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 REQ(n, ATOM);
Barry Warsawedaa0712003-02-28 15:27:40 +0000282 i = n->n_nchildren;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283 REQN(i, 1);
284 n = n->n_child;
285 if (n->n_type == LPAR) {
286 REQN(i, 3);
287 n++;
288 REQ(n, RHS);
289 compile_rhs(ll, nf, n, pa, pb);
290 n++;
291 REQ(n, RPAR);
292 }
293 else if (n->n_type == NAME || n->n_type == STRING) {
294 *pa = addnfastate(nf);
295 *pb = addnfastate(nf);
296 addnfaarc(nf, *pa, *pb, addlabel(ll, n->n_type, n->n_str));
297 }
298 else
299 REQ(n, NAME);
300}
301
302static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000303dumpstate(labellist *ll, nfa *nf, int istate)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304{
305 nfastate *st;
306 int i;
307 nfaarc *ar;
308
309 printf("%c%2d%c",
310 istate == nf->nf_start ? '*' : ' ',
311 istate,
312 istate == nf->nf_finish ? '.' : ' ');
313 st = &nf->nf_state[istate];
314 ar = st->st_arc;
315 for (i = 0; i < st->st_narcs; i++) {
316 if (i > 0)
317 printf("\n ");
318 printf("-> %2d %s", ar->ar_arrow,
Guido van Rossum86bea461997-04-29 21:03:06 +0000319 PyGrammar_LabelRepr(&ll->ll_label[ar->ar_label]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320 ar++;
321 }
322 printf("\n");
323}
324
325static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000326dumpnfa(labellist *ll, nfa *nf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327{
328 int i;
329
330 printf("NFA '%s' has %d states; start %d, finish %d\n",
331 nf->nf_name, nf->nf_nstates, nf->nf_start, nf->nf_finish);
332 for (i = 0; i < nf->nf_nstates; i++)
333 dumpstate(ll, nf, i);
334}
335
336
337/* PART TWO -- CONSTRUCT DFA -- Algorithm 3.1 from [Aho&Ullman 77] */
338
Guido van Rossum588633d1994-12-30 15:46:02 +0000339static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000340addclosure(bitset ss, nfa *nf, int istate)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341{
342 if (addbit(ss, istate)) {
343 nfastate *st = &nf->nf_state[istate];
344 nfaarc *ar = st->st_arc;
345 int i;
346
347 for (i = st->st_narcs; --i >= 0; ) {
348 if (ar->ar_label == EMPTY)
349 addclosure(ss, nf, ar->ar_arrow);
350 ar++;
351 }
352 }
353}
354
355typedef struct _ss_arc {
356 bitset sa_bitset;
357 int sa_arrow;
358 int sa_label;
359} ss_arc;
360
361typedef struct _ss_state {
362 bitset ss_ss;
363 int ss_narcs;
364 ss_arc *ss_arc;
365 int ss_deleted;
366 int ss_finish;
367 int ss_rename;
368} ss_state;
369
370typedef struct _ss_dfa {
371 int sd_nstates;
372 ss_state *sd_state;
373} ss_dfa;
374
Guido van Rossum71f477c1991-04-03 19:09:02 +0000375/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000376static void printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
377 labellist *ll, char *msg);
378static void simplify(int xx_nstates, ss_state *xx_state);
379static void convert(dfa *d, int xx_nstates, ss_state *xx_state);
Guido van Rossum71f477c1991-04-03 19:09:02 +0000380
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000381static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000382makedfa(nfagrammar *gr, nfa *nf, dfa *d)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383{
384 int nbits = nf->nf_nstates;
385 bitset ss;
386 int xx_nstates;
387 ss_state *xx_state, *yy;
388 ss_arc *zz;
389 int istate, jstate, iarc, jarc, ibit;
390 nfastate *st;
391 nfaarc *ar;
392
393 ss = newbitset(nbits);
394 addclosure(ss, nf, nf->nf_start);
Guido van Rossum86bea461997-04-29 21:03:06 +0000395 xx_state = PyMem_NEW(ss_state, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396 if (xx_state == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000397 Py_FatalError("no mem for xx_state in makedfa");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 xx_nstates = 1;
399 yy = &xx_state[0];
400 yy->ss_ss = ss;
401 yy->ss_narcs = 0;
402 yy->ss_arc = NULL;
403 yy->ss_deleted = 0;
404 yy->ss_finish = testbit(ss, nf->nf_finish);
405 if (yy->ss_finish)
406 printf("Error: nonterminal '%s' may produce empty.\n",
407 nf->nf_name);
408
409 /* This algorithm is from a book written before
410 the invention of structured programming... */
411
412 /* For each unmarked state... */
413 for (istate = 0; istate < xx_nstates; ++istate) {
414 yy = &xx_state[istate];
415 ss = yy->ss_ss;
416 /* For all its states... */
417 for (ibit = 0; ibit < nf->nf_nstates; ++ibit) {
418 if (!testbit(ss, ibit))
419 continue;
420 st = &nf->nf_state[ibit];
421 /* For all non-empty arcs from this state... */
422 for (iarc = 0; iarc < st->st_narcs; iarc++) {
423 ar = &st->st_arc[iarc];
424 if (ar->ar_label == EMPTY)
425 continue;
426 /* Look up in list of arcs from this state */
427 for (jarc = 0; jarc < yy->ss_narcs; ++jarc) {
428 zz = &yy->ss_arc[jarc];
429 if (ar->ar_label == zz->sa_label)
430 goto found;
431 }
432 /* Add new arc for this state */
Guido van Rossum86bea461997-04-29 21:03:06 +0000433 PyMem_RESIZE(yy->ss_arc, ss_arc,
434 yy->ss_narcs + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435 if (yy->ss_arc == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000436 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437 zz = &yy->ss_arc[yy->ss_narcs++];
438 zz->sa_label = ar->ar_label;
439 zz->sa_bitset = newbitset(nbits);
440 zz->sa_arrow = -1;
441 found: ;
442 /* Add destination */
443 addclosure(zz->sa_bitset, nf, ar->ar_arrow);
444 }
445 }
446 /* Now look up all the arrow states */
447 for (jarc = 0; jarc < xx_state[istate].ss_narcs; jarc++) {
448 zz = &xx_state[istate].ss_arc[jarc];
449 for (jstate = 0; jstate < xx_nstates; jstate++) {
450 if (samebitset(zz->sa_bitset,
451 xx_state[jstate].ss_ss, nbits)) {
452 zz->sa_arrow = jstate;
453 goto done;
454 }
455 }
Guido van Rossum86bea461997-04-29 21:03:06 +0000456 PyMem_RESIZE(xx_state, ss_state, xx_nstates + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457 if (xx_state == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000458 Py_FatalError("out of mem");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459 zz->sa_arrow = xx_nstates;
460 yy = &xx_state[xx_nstates++];
461 yy->ss_ss = zz->sa_bitset;
462 yy->ss_narcs = 0;
463 yy->ss_arc = NULL;
464 yy->ss_deleted = 0;
465 yy->ss_finish = testbit(yy->ss_ss, nf->nf_finish);
466 done: ;
467 }
468 }
469
Guido van Rossum86bea461997-04-29 21:03:06 +0000470 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471 printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
472 "before minimizing");
473
474 simplify(xx_nstates, xx_state);
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 "after minimizing");
479
480 convert(d, xx_nstates, xx_state);
481
482 /* XXX cleanup */
483}
484
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000485static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000486printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
487 labellist *ll, char *msg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488{
489 int i, ibit, iarc;
490 ss_state *yy;
491 ss_arc *zz;
492
493 printf("Subset DFA %s\n", msg);
494 for (i = 0; i < xx_nstates; i++) {
495 yy = &xx_state[i];
496 if (yy->ss_deleted)
497 continue;
498 printf(" Subset %d", i);
499 if (yy->ss_finish)
500 printf(" (finish)");
501 printf(" { ");
502 for (ibit = 0; ibit < nbits; ibit++) {
503 if (testbit(yy->ss_ss, ibit))
504 printf("%d ", ibit);
505 }
506 printf("}\n");
507 for (iarc = 0; iarc < yy->ss_narcs; iarc++) {
508 zz = &yy->ss_arc[iarc];
509 printf(" Arc to state %d, label %s\n",
510 zz->sa_arrow,
Guido van Rossum86bea461997-04-29 21:03:06 +0000511 PyGrammar_LabelRepr(
512 &ll->ll_label[zz->sa_label]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513 }
514 }
515}
516
517
518/* PART THREE -- SIMPLIFY DFA */
519
520/* Simplify the DFA by repeatedly eliminating states that are
521 equivalent to another oner. This is NOT Algorithm 3.3 from
522 [Aho&Ullman 77]. It does not always finds the minimal DFA,
523 but it does usually make a much smaller one... (For an example
Thomas Wouters7e474022000-07-16 12:04:32 +0000524 of sub-optimal behavior, try S: x a b+ | y a b+.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525*/
526
527static int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000528samestate(ss_state *s1, ss_state *s2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529{
530 int i;
531
532 if (s1->ss_narcs != s2->ss_narcs || s1->ss_finish != s2->ss_finish)
533 return 0;
534 for (i = 0; i < s1->ss_narcs; i++) {
535 if (s1->ss_arc[i].sa_arrow != s2->ss_arc[i].sa_arrow ||
536 s1->ss_arc[i].sa_label != s2->ss_arc[i].sa_label)
537 return 0;
538 }
539 return 1;
540}
541
542static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000543renamestates(int xx_nstates, ss_state *xx_state, int from, int to)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544{
545 int i, j;
546
Guido van Rossum86bea461997-04-29 21:03:06 +0000547 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548 printf("Rename state %d to %d.\n", from, to);
549 for (i = 0; i < xx_nstates; i++) {
550 if (xx_state[i].ss_deleted)
551 continue;
552 for (j = 0; j < xx_state[i].ss_narcs; j++) {
553 if (xx_state[i].ss_arc[j].sa_arrow == from)
554 xx_state[i].ss_arc[j].sa_arrow = to;
555 }
556 }
557}
558
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000559static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000560simplify(int xx_nstates, ss_state *xx_state)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561{
562 int changes;
Guido van Rossum588633d1994-12-30 15:46:02 +0000563 int i, j;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564
565 do {
566 changes = 0;
567 for (i = 1; i < xx_nstates; i++) {
568 if (xx_state[i].ss_deleted)
569 continue;
570 for (j = 0; j < i; j++) {
571 if (xx_state[j].ss_deleted)
572 continue;
573 if (samestate(&xx_state[i], &xx_state[j])) {
574 xx_state[i].ss_deleted++;
Guido van Rossum86bea461997-04-29 21:03:06 +0000575 renamestates(xx_nstates, xx_state,
576 i, j);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577 changes++;
578 break;
579 }
580 }
581 }
582 } while (changes);
583}
584
585
586/* PART FOUR -- GENERATE PARSING TABLES */
587
588/* Convert the DFA into a grammar that can be used by our parser */
589
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000590static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000591convert(dfa *d, int xx_nstates, ss_state *xx_state)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592{
593 int i, j;
594 ss_state *yy;
595 ss_arc *zz;
596
597 for (i = 0; i < xx_nstates; i++) {
598 yy = &xx_state[i];
599 if (yy->ss_deleted)
600 continue;
601 yy->ss_rename = addstate(d);
602 }
603
604 for (i = 0; i < xx_nstates; i++) {
605 yy = &xx_state[i];
606 if (yy->ss_deleted)
607 continue;
608 for (j = 0; j < yy->ss_narcs; j++) {
609 zz = &yy->ss_arc[j];
610 addarc(d, yy->ss_rename,
611 xx_state[zz->sa_arrow].ss_rename,
612 zz->sa_label);
613 }
614 if (yy->ss_finish)
615 addarc(d, yy->ss_rename, yy->ss_rename, 0);
616 }
617
618 d->d_initial = 0;
619}
620
621
622/* PART FIVE -- GLUE IT ALL TOGETHER */
623
624static grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000625maketables(nfagrammar *gr)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626{
627 int i;
628 nfa *nf;
629 dfa *d;
630 grammar *g;
631
632 if (gr->gr_nnfas == 0)
633 return NULL;
634 g = newgrammar(gr->gr_nfa[0]->nf_type);
635 /* XXX first rule must be start rule */
636 g->g_ll = gr->gr_ll;
637
638 for (i = 0; i < gr->gr_nnfas; i++) {
639 nf = gr->gr_nfa[i];
Guido van Rossum86bea461997-04-29 21:03:06 +0000640 if (Py_DebugFlag) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641 printf("Dump of NFA for '%s' ...\n", nf->nf_name);
642 dumpnfa(&gr->gr_ll, nf);
Guido van Rossum25dfe2c2001-09-11 16:43:16 +0000643 printf("Making DFA for '%s' ...\n", nf->nf_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645 d = adddfa(g, nf->nf_type, nf->nf_name);
646 makedfa(gr, gr->gr_nfa[i], d);
647 }
648
649 return g;
650}
651
652grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000653pgen(node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654{
655 nfagrammar *gr;
656 grammar *g;
657
658 gr = metacompile(n);
659 g = maketables(gr);
660 translatelabels(g);
661 addfirstsets(g);
662 return g;
663}
664
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000665grammar *
666Py_pgen(node *n)
667{
668 return pgen(n);
669}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670
671/*
672
673Description
674-----------
675
676Input is a grammar in extended BNF (using * for repetition, + for
677at-least-once repetition, [] for optional parts, | for alternatives and
678() for grouping). This has already been parsed and turned into a parse
679tree.
680
681Each rule is considered as a regular expression in its own right.
682It is turned into a Non-deterministic Finite Automaton (NFA), which
683is then turned into a Deterministic Finite Automaton (DFA), which is then
684optimized to reduce the number of states. See [Aho&Ullman 77] chapter 3,
685or similar compiler books (this technique is more often used for lexical
686analyzers).
687
688The DFA's are used by the parser as parsing tables in a special way
689that's probably unique. Before they are usable, the FIRST sets of all
690non-terminals are computed.
691
692Reference
693---------
694
695[Aho&Ullman 77]
696 Aho&Ullman, Principles of Compiler Design, Addison-Wesley 1977
697 (first edition)
698
699*/