Mercurial > cpython-lru_cache-weakref
annotate Parser/firstsets.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | 1c9a84e0981d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2060
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Computation of FIRST stets */ |
| 33 | |
| 86 | 34 #include "pgenheaders.h" |
| 6 | 35 #include "grammar.h" |
| 36 #include "token.h" | |
| 37 | |
| 38 extern int debugging; | |
| 39 | |
| 86 | 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 | |
| 6 | 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 } |
