blob: d6c4084d066815a92e20640a2513e3254200f87c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Computation of FIRST stets */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005#include "grammar.h"
6#include "token.h"
7
Guido van Rossum86bea461997-04-29 21:03:06 +00008extern int Py_DebugFlag;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Guido van Rossum3f5da241990-12-20 15:06:42 +000010/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000011static void calcfirstset(grammar *, dfa *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000012
13void
Thomas Wouters23c9e002000-07-22 19:20:54 +000014addfirstsets(grammar *g)
Guido van Rossum3f5da241990-12-20 15:06:42 +000015{
16 int i;
17 dfa *d;
18
19 printf("Adding FIRST sets ...\n");
20 for (i = 0; i < g->g_ndfas; i++) {
21 d = &g->g_dfa[i];
22 if (d->d_first == NULL)
23 calcfirstset(g, d);
24 }
25}
26
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000028calcfirstset(grammar *g, dfa *d)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029{
30 int i, j;
31 state *s;
32 arc *a;
33 int nsyms;
34 int *sym;
35 int nbits;
36 static bitset dummy;
37 bitset result;
38 int type;
39 dfa *d1;
40 label *l0;
41
Guido van Rossum86bea461997-04-29 21:03:06 +000042 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 printf("Calculate FIRST set for '%s'\n", d->d_name);
44
45 if (dummy == NULL)
46 dummy = newbitset(1);
47 if (d->d_first == dummy) {
48 fprintf(stderr, "Left-recursion for '%s'\n", d->d_name);
49 return;
50 }
51 if (d->d_first != NULL) {
52 fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n",
53 d->d_name);
54 }
55 d->d_first = dummy;
56
57 l0 = g->g_ll.ll_label;
58 nbits = g->g_ll.ll_nlabels;
59 result = newbitset(nbits);
60
Guido van Rossum86bea461997-04-29 21:03:06 +000061 sym = PyMem_NEW(int, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 if (sym == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000063 Py_FatalError("no mem for new sym in calcfirstset");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 nsyms = 1;
65 sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL);
66
67 s = &d->d_state[d->d_initial];
68 for (i = 0; i < s->s_narcs; i++) {
69 a = &s->s_arc[i];
70 for (j = 0; j < nsyms; j++) {
71 if (sym[j] == a->a_lbl)
72 break;
73 }
74 if (j >= nsyms) { /* New label */
Guido van Rossum86bea461997-04-29 21:03:06 +000075 PyMem_RESIZE(sym, int, nsyms + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 if (sym == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000077 Py_FatalError(
78 "no mem to resize sym in calcfirstset");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 sym[nsyms++] = a->a_lbl;
80 type = l0[a->a_lbl].lb_type;
81 if (ISNONTERMINAL(type)) {
Guido van Rossum86bea461997-04-29 21:03:06 +000082 d1 = PyGrammar_FindDFA(g, type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 if (d1->d_first == dummy) {
84 fprintf(stderr,
85 "Left-recursion below '%s'\n",
86 d->d_name);
87 }
88 else {
89 if (d1->d_first == NULL)
90 calcfirstset(g, d1);
Guido van Rossum86bea461997-04-29 21:03:06 +000091 mergebitset(result,
92 d1->d_first, nbits);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 }
94 }
95 else if (ISTERMINAL(type)) {
96 addbit(result, a->a_lbl);
97 }
98 }
99 }
100 d->d_first = result;
Guido van Rossum86bea461997-04-29 21:03:06 +0000101 if (Py_DebugFlag) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 printf("FIRST set for '%s': {", d->d_name);
103 for (i = 0; i < nbits; i++) {
104 if (testbit(result, i))
Guido van Rossum86bea461997-04-29 21:03:06 +0000105 printf(" %s", PyGrammar_LabelRepr(&l0[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 }
107 printf(" }\n");
108 }
109}