]> git.saurik.com Git - bison.git/blob - src/closure.c
03222cac8cc34015d384d9cca692d8f926fceed8
[bison.git] / src / closure.c
1 /* Subroutines for bison
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include "system.h"
22 #include "getargs.h"
23 #include "symtab.h"
24 #include "gram.h"
25 #include "reader.h"
26 #include "closure.h"
27 #include "derives.h"
28 #include "warshall.h"
29 #include "bitset.h"
30
31 /* NITEMSET is the size of the array ITEMSET. */
32 short *itemset;
33 int nitemset;
34
35 static bitset ruleset;
36
37 /* internal data. See comments before set_fderives and set_firsts. */
38 static bitset *fderives;
39 static unsigned int *firsts;
40
41 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
42 #define FDERIVES(Var) fderives[(Var) - ntokens]
43 #define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
44
45 /* number of words required to hold a bit for each variable */
46 static int varsetsize;
47 \f
48
49 /*-----------------.
50 | Debugging code. |
51 `-----------------*/
52
53 static void
54 print_closure (const char *title, short *array, size_t size)
55 {
56 size_t i;
57 fprintf (stderr, "Closure: %s\n", title);
58 for (i = 0; i < size; ++i)
59 {
60 short *rp;
61 fprintf (stderr, " %2d: .", array[i]);
62 for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
63 fprintf (stderr, " %s", symbols[*rp]->tag);
64 fprintf (stderr, " (rule %d)\n", -*rp - 1);
65 }
66 fputs ("\n\n", stderr);
67 }
68
69
70 static void
71 print_firsts (void)
72 {
73 int i, j;
74
75 fprintf (stderr, "FIRSTS\n");
76 for (i = ntokens; i < nsyms; i++)
77 {
78 fprintf (stderr, "\t%s firsts\n", symbols[i]->tag);
79 for (j = 0; j < nvars; j++)
80 if (BITISSET (FIRSTS (i), j))
81 fprintf (stderr, "\t\t%d (%s)\n",
82 j + ntokens, symbols[j + ntokens]->tag);
83 }
84 fprintf (stderr, "\n\n");
85 }
86
87
88 static void
89 print_fderives (void)
90 {
91 int i;
92 int j;
93
94 fprintf (stderr, "FDERIVES\n");
95
96 for (i = ntokens; i < nsyms; i++)
97 {
98 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
99 for (j = 0; j <= nrules; j++)
100 if (bitset_test (FDERIVES (i), j))
101 {
102 short *rhsp;
103 fprintf (stderr, "\t\t%d:", j - 1);
104 for (rhsp = &ritem[rules[j].rhs]; *rhsp >= 0; ++rhsp)
105 fprintf (stderr, " %s", symbols[*rhsp]->tag);
106 fputc ('\n', stderr);
107 }
108 }
109 fprintf (stderr, "\n\n");
110 }
111 \f
112 /*-------------------------------------------------------------------.
113 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
114 | items can represent the beginning of the input corresponding to |
115 | which other items. |
116 | |
117 | For example, if some rule expands symbol 5 into the sequence of |
118 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
119 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
120 `-------------------------------------------------------------------*/
121
122 static void
123 set_firsts (void)
124 {
125 int i, j;
126
127 varsetsize = WORDSIZE (nvars);
128
129 firsts = XCALLOC (unsigned, nvars * varsetsize);
130
131 for (i = ntokens; i < nsyms; i++)
132 for (j = 0; derives[i][j] >= 0; ++j)
133 {
134 int symbol = ritem[rules[derives[i][j]].rhs];
135 if (ISVAR (symbol))
136 SETBIT (FIRSTS (i), symbol - ntokens);
137 }
138
139 RTC (firsts, nvars);
140
141 if (trace_flag)
142 print_firsts ();
143 }
144
145 /*-------------------------------------------------------------------.
146 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
147 | rules can help derive the beginning of the data for each |
148 | nonterminal. |
149 | |
150 | For example, if symbol 5 can be derived as the sequence of symbols |
151 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
152 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
153 `-------------------------------------------------------------------*/
154
155 static void
156 set_fderives (void)
157 {
158 int i, j, k;
159
160 fderives = XCALLOC (bitset, nvars);
161 bitset_stats_init ();
162 for (i = 0 ; i < nvars; ++i)
163 {
164 fderives[i] = bitset_create (nrules + 1, BITSET_FIXED);
165 bitset_zero (fderives[i]);
166 }
167
168 set_firsts ();
169
170 for (i = ntokens; i < nsyms; ++i)
171 for (j = ntokens; j < nsyms; ++j)
172 if (BITISSET (FIRSTS (i), j - ntokens))
173 for (k = 0; derives[j][k] > 0; ++k)
174 bitset_set (FDERIVES (i), derives[j][k]);
175
176 if (trace_flag)
177 print_fderives ();
178
179 XFREE (firsts);
180 }
181 \f
182
183 void
184 new_closure (int n)
185 {
186 itemset = XCALLOC (short, n);
187
188 ruleset = bitset_create (nrules + 1, BITSET_FIXED);
189 bitset_zero (ruleset);
190
191 set_fderives ();
192 }
193
194
195
196 void
197 closure (short *core, int n)
198 {
199 /* Index over CORE. */
200 int c;
201
202 /* An index over RULESET. */
203 int r;
204
205 /* A bit index over RULESET. */
206 int ruleno;
207
208 if (trace_flag)
209 print_closure ("input", core, n);
210
211 if (n == 0)
212 {
213 bitset_copy (ruleset, FDERIVES (start_symbol));
214 }
215 else
216 {
217 bitset_zero (ruleset);
218
219 for (c = 0; c < n; ++c)
220 if (ISVAR (ritem[core[c]]))
221 bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]]));
222 }
223
224 nitemset = 0;
225 c = 0;
226 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
227 if (bitset_test (ruleset, ruleno))
228 {
229 int itemno = rules[ruleno].rhs;
230 while (c < n && core[c] < itemno)
231 {
232 itemset[nitemset] = core[c];
233 nitemset++;
234 c++;
235 }
236 itemset[nitemset] = itemno;
237 nitemset++;
238 }
239
240 while (c < n)
241 {
242 itemset[nitemset] = core[c];
243 nitemset++;
244 c++;
245 }
246
247 if (trace_flag)
248 print_closure ("output", itemset, nitemset);
249 }
250
251
252 void
253 free_closure (void)
254 {
255 int i;
256 XFREE (itemset);
257
258 bitset_free (ruleset);
259
260 for (i = 0 ; i < nvars; ++i)
261 bitset_free (fderives[i]);
262 free (fderives);
263 }