Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Computation of FIRST stets */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | #include "grammar.h" |
| 6 | #include "token.h" |
| 7 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 8 | extern int Py_DebugFlag; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 11 | static void calcfirstset(grammar *, dfa *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | |
| 13 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 14 | addfirstsets(grammar *g) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | { |
| 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 28 | calcfirstset(grammar *g, dfa *d) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | { |
| 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 Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 42 | if (Py_DebugFlag) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | 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 Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 61 | sym = PyMem_NEW(int, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | if (sym == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 63 | Py_FatalError("no mem for new sym in calcfirstset"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | 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 Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 75 | PyMem_RESIZE(sym, int, nsyms + 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | if (sym == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 77 | Py_FatalError( |
| 78 | "no mem to resize sym in calcfirstset"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | sym[nsyms++] = a->a_lbl; |
| 80 | type = l0[a->a_lbl].lb_type; |
| 81 | if (ISNONTERMINAL(type)) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 82 | d1 = PyGrammar_FindDFA(g, type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | 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 Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 91 | mergebitset(result, |
| 92 | d1->d_first, nbits); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | else if (ISTERMINAL(type)) { |
| 96 | addbit(result, a->a_lbl); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | d->d_first = result; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 101 | if (Py_DebugFlag) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | printf("FIRST set for '%s': {", d->d_name); |
| 103 | for (i = 0; i < nbits; i++) { |
| 104 | if (testbit(result, i)) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 105 | printf(" %s", PyGrammar_LabelRepr(&l0[i])); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | } |
| 107 | printf(" }\n"); |
| 108 | } |
| 109 | } |