Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Computation of FIRST stets */ |
| 12 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | #include "grammar.h" |
| 15 | #include "token.h" |
| 16 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 17 | extern int Py_DebugFlag; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 20 | static void calcfirstset(grammar *, dfa *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | |
| 22 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 23 | addfirstsets(grammar *g) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 24 | { |
| 25 | int i; |
| 26 | dfa *d; |
| 27 | |
| 28 | printf("Adding FIRST sets ...\n"); |
| 29 | for (i = 0; i < g->g_ndfas; i++) { |
| 30 | d = &g->g_dfa[i]; |
| 31 | if (d->d_first == NULL) |
| 32 | calcfirstset(g, d); |
| 33 | } |
| 34 | } |
| 35 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 37 | calcfirstset(grammar *g, dfa *d) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | { |
| 39 | int i, j; |
| 40 | state *s; |
| 41 | arc *a; |
| 42 | int nsyms; |
| 43 | int *sym; |
| 44 | int nbits; |
| 45 | static bitset dummy; |
| 46 | bitset result; |
| 47 | int type; |
| 48 | dfa *d1; |
| 49 | label *l0; |
| 50 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 51 | if (Py_DebugFlag) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | printf("Calculate FIRST set for '%s'\n", d->d_name); |
| 53 | |
| 54 | if (dummy == NULL) |
| 55 | dummy = newbitset(1); |
| 56 | if (d->d_first == dummy) { |
| 57 | fprintf(stderr, "Left-recursion for '%s'\n", d->d_name); |
| 58 | return; |
| 59 | } |
| 60 | if (d->d_first != NULL) { |
| 61 | fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n", |
| 62 | d->d_name); |
| 63 | } |
| 64 | d->d_first = dummy; |
| 65 | |
| 66 | l0 = g->g_ll.ll_label; |
| 67 | nbits = g->g_ll.ll_nlabels; |
| 68 | result = newbitset(nbits); |
| 69 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 70 | sym = PyMem_NEW(int, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | if (sym == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 72 | Py_FatalError("no mem for new sym in calcfirstset"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | nsyms = 1; |
| 74 | sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL); |
| 75 | |
| 76 | s = &d->d_state[d->d_initial]; |
| 77 | for (i = 0; i < s->s_narcs; i++) { |
| 78 | a = &s->s_arc[i]; |
| 79 | for (j = 0; j < nsyms; j++) { |
| 80 | if (sym[j] == a->a_lbl) |
| 81 | break; |
| 82 | } |
| 83 | if (j >= nsyms) { /* New label */ |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 84 | PyMem_RESIZE(sym, int, nsyms + 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | if (sym == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 86 | Py_FatalError( |
| 87 | "no mem to resize sym in calcfirstset"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | sym[nsyms++] = a->a_lbl; |
| 89 | type = l0[a->a_lbl].lb_type; |
| 90 | if (ISNONTERMINAL(type)) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 91 | d1 = PyGrammar_FindDFA(g, type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | if (d1->d_first == dummy) { |
| 93 | fprintf(stderr, |
| 94 | "Left-recursion below '%s'\n", |
| 95 | d->d_name); |
| 96 | } |
| 97 | else { |
| 98 | if (d1->d_first == NULL) |
| 99 | calcfirstset(g, d1); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 100 | mergebitset(result, |
| 101 | d1->d_first, nbits); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | else if (ISTERMINAL(type)) { |
| 105 | addbit(result, a->a_lbl); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | d->d_first = result; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 110 | if (Py_DebugFlag) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | printf("FIRST set for '%s': {", d->d_name); |
| 112 | for (i = 0; i < nbits; i++) { |
| 113 | if (testbit(result, i)) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 114 | printf(" %s", PyGrammar_LabelRepr(&l0[i])); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | } |
| 116 | printf(" }\n"); |
| 117 | } |
| 118 | } |