Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b9f8d6e | 1995-01-04 19:08:09 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Computation of FIRST stets */ |
| 33 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | #include "grammar.h" |
| 36 | #include "token.h" |
| 37 | |
| 38 | extern int debugging; |
| 39 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | /* Forward */ |
| 41 | static void calcfirstset PROTO((grammar *, dfa *)); |
| 42 | |
| 43 | void |
| 44 | addfirstsets(g) |
| 45 | grammar *g; |
| 46 | { |
| 47 | int i; |
| 48 | dfa *d; |
| 49 | |
| 50 | printf("Adding FIRST sets ...\n"); |
| 51 | for (i = 0; i < g->g_ndfas; i++) { |
| 52 | d = &g->g_dfa[i]; |
| 53 | if (d->d_first == NULL) |
| 54 | calcfirstset(g, d); |
| 55 | } |
| 56 | } |
| 57 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | static void |
| 59 | calcfirstset(g, d) |
| 60 | grammar *g; |
| 61 | dfa *d; |
| 62 | { |
| 63 | int i, j; |
| 64 | state *s; |
| 65 | arc *a; |
| 66 | int nsyms; |
| 67 | int *sym; |
| 68 | int nbits; |
| 69 | static bitset dummy; |
| 70 | bitset result; |
| 71 | int type; |
| 72 | dfa *d1; |
| 73 | label *l0; |
| 74 | |
| 75 | if (debugging) |
| 76 | printf("Calculate FIRST set for '%s'\n", d->d_name); |
| 77 | |
| 78 | if (dummy == NULL) |
| 79 | dummy = newbitset(1); |
| 80 | if (d->d_first == dummy) { |
| 81 | fprintf(stderr, "Left-recursion for '%s'\n", d->d_name); |
| 82 | return; |
| 83 | } |
| 84 | if (d->d_first != NULL) { |
| 85 | fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n", |
| 86 | d->d_name); |
| 87 | } |
| 88 | d->d_first = dummy; |
| 89 | |
| 90 | l0 = g->g_ll.ll_label; |
| 91 | nbits = g->g_ll.ll_nlabels; |
| 92 | result = newbitset(nbits); |
| 93 | |
| 94 | sym = NEW(int, 1); |
| 95 | if (sym == NULL) |
| 96 | fatal("no mem for new sym in calcfirstset"); |
| 97 | nsyms = 1; |
| 98 | sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL); |
| 99 | |
| 100 | s = &d->d_state[d->d_initial]; |
| 101 | for (i = 0; i < s->s_narcs; i++) { |
| 102 | a = &s->s_arc[i]; |
| 103 | for (j = 0; j < nsyms; j++) { |
| 104 | if (sym[j] == a->a_lbl) |
| 105 | break; |
| 106 | } |
| 107 | if (j >= nsyms) { /* New label */ |
| 108 | RESIZE(sym, int, nsyms + 1); |
| 109 | if (sym == NULL) |
| 110 | fatal("no mem to resize sym in calcfirstset"); |
| 111 | sym[nsyms++] = a->a_lbl; |
| 112 | type = l0[a->a_lbl].lb_type; |
| 113 | if (ISNONTERMINAL(type)) { |
| 114 | d1 = finddfa(g, type); |
| 115 | if (d1->d_first == dummy) { |
| 116 | fprintf(stderr, |
| 117 | "Left-recursion below '%s'\n", |
| 118 | d->d_name); |
| 119 | } |
| 120 | else { |
| 121 | if (d1->d_first == NULL) |
| 122 | calcfirstset(g, d1); |
| 123 | mergebitset(result, d1->d_first, nbits); |
| 124 | } |
| 125 | } |
| 126 | else if (ISTERMINAL(type)) { |
| 127 | addbit(result, a->a_lbl); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | d->d_first = result; |
| 132 | if (debugging) { |
| 133 | printf("FIRST set for '%s': {", d->d_name); |
| 134 | for (i = 0; i < nbits; i++) { |
| 135 | if (testbit(result, i)) |
| 136 | printf(" %s", labelrepr(&l0[i])); |
| 137 | } |
| 138 | printf(" }\n"); |
| 139 | } |
| 140 | } |