]> git.saurik.com Git - bison.git/blob - src/closure.c
bb45ba026cae20fe99ed42852bc383c00f28555b
[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 "gram.h"
24 #include "reader.h"
25 #include "closure.h"
26 #include "derives.h"
27 #include "warshall.h"
28
29 /* ITEMSETSIZE is the size of the array ITEMSET. */
30 short *itemset;
31 int itemsetsize;
32
33 static unsigned *ruleset;
34
35 /* internal data. See comments before set_fderives and set_firsts. */
36 static unsigned *fderives;
37 static unsigned *firsts;
38
39 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
40 #define FDERIVES(Var) (fderives + ((Var) - ntokens) * rulesetsize)
41 #define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
42
43 /* number of words required to hold a bit for each rule */
44 static int rulesetsize;
45
46 /* number of words required to hold a bit for each variable */
47 static int varsetsize;
48 \f
49
50 /*-----------------.
51 | Debugging code. |
52 `-----------------*/
53
54 static void
55 print_closure (int n)
56 {
57 int i;
58 fprintf (stderr, "n = %d\n", n);
59 for (i = 0; i < itemsetsize; ++i)
60 fprintf (stderr, " %d\n", itemset[i]);
61 fprintf (stderr, "\n\n");
62 }
63
64
65 static void
66 print_firsts (void)
67 {
68 int i, j;
69
70 fprintf (stderr, "FIRSTS\n");
71 for (i = ntokens; i < nsyms; i++)
72 {
73 fprintf (stderr, "\t%s firsts\n", tags[i]);
74 for (j = 0; j < nvars; j++)
75 if (BITISSET (FIRSTS (i), j))
76 fprintf (stderr, "\t\t%d (%s)\n", j + ntokens, tags[j + ntokens]);
77 }
78 fprintf (stderr, "\n\n");
79 }
80
81
82 static void
83 print_fderives (void)
84 {
85 int i;
86 int j;
87
88 fprintf (stderr, "FDERIVES\n");
89
90 for (i = ntokens; i < nsyms; i++)
91 {
92 fprintf (stderr, "\t%s derives\n", tags[i]);
93 for (j = 0; j <= nrules; j++)
94 if (BITISSET (FDERIVES (i), j))
95 {
96 short *rhsp;
97 fprintf (stderr, "\t\t%d:", j);
98 for (rhsp = ritem + rule_table[j].rhs; *rhsp > 0; ++rhsp)
99 fprintf (stderr, " %s", tags[*rhsp]);
100 fputc ('\n', stderr);
101 }
102 }
103 fprintf (stderr, "\n\n");
104 }
105 \f
106 /*-------------------------------------------------------------------.
107 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
108 | items can represent the beginning of the input corresponding to |
109 | which other items. |
110 | |
111 | For example, if some rule expands symbol 5 into the sequence of |
112 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
113 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
114 `-------------------------------------------------------------------*/
115
116 static void
117 set_firsts (void)
118 {
119 int i, j;
120
121 varsetsize = WORDSIZE (nvars);
122
123 firsts = XCALLOC (unsigned, nvars * varsetsize);
124
125 for (i = ntokens; i < nsyms; i++)
126 for (j = 0; derives[i][j] >= 0; ++j)
127 {
128 int symbol = ritem[rule_table[derives[i][j]].rhs];
129 if (ISVAR (symbol))
130 SETBIT (FIRSTS (i), symbol - ntokens);
131 }
132
133 RTC (firsts, nvars);
134
135 if (trace_flag)
136 print_firsts ();
137 }
138
139 /*-------------------------------------------------------------------.
140 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
141 | rules can help derive the beginning of the data for each |
142 | nonterminal. |
143 | |
144 | For example, if symbol 5 can be derived as the sequence of symbols |
145 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
146 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
147 `-------------------------------------------------------------------*/
148
149 static void
150 set_fderives (void)
151 {
152 int i, j, k;
153
154 fderives = XCALLOC (unsigned, nvars * rulesetsize);
155
156 set_firsts ();
157
158 for (i = ntokens; i < nsyms; ++i)
159 for (j = ntokens; j < nsyms; ++j)
160 if (BITISSET (FIRSTS (i), j - ntokens))
161 for (k = 0; derives[j][k] > 0; ++k)
162 SETBIT (FDERIVES (i), derives[j][k]);
163
164 if (trace_flag)
165 print_fderives ();
166
167 XFREE (firsts);
168 }
169 \f
170
171 void
172 new_closure (int n)
173 {
174 itemset = XCALLOC (short, n);
175
176 rulesetsize = WORDSIZE (nrules + 1);
177 ruleset = XCALLOC (unsigned, rulesetsize);
178
179 set_fderives ();
180 }
181
182
183
184 void
185 closure (short *core, int n)
186 {
187 /* Index over CORE. */
188 int c;
189
190 /* Index over RULESET. */
191 int r;
192
193 /* A bit index over RULESET. */
194 int ruleno;
195
196 if (trace_flag)
197 {
198 fprintf (stderr, "Entering closure (items = {");
199 for (c = 0; c < n; ++c)
200 fprintf (stderr, " %d ", core[c]);
201 fprintf (stderr, "})\n");
202 }
203
204 if (n == 0)
205 {
206 for (r = 0; r < rulesetsize; ++r)
207 ruleset[r] = FDERIVES (start_symbol)[r];
208 }
209 else
210 {
211 for (r = 0; r < rulesetsize; ++r)
212 ruleset[r] = 0;
213
214 for (c = 0; c < n; ++c)
215 if (ISVAR (ritem[core[c]]))
216 for (r = 0; r < rulesetsize; ++r)
217 ruleset[r] |= FDERIVES (ritem[core[c]])[r];
218 }
219
220 itemsetsize = 0;
221 c = 0;
222 for (ruleno = 0; ruleno < rulesetsize * BITS_PER_WORD; ++ruleno)
223 if (BITISSET (ruleset, ruleno))
224 {
225 int itemno = rule_table[ruleno].rhs;
226 while (c < n && core[c] < itemno)
227 {
228 itemset[itemsetsize] = core[c];
229 itemsetsize++;
230 c++;
231 }
232 itemset[itemsetsize] = itemno;
233 itemsetsize++;
234 }
235
236 while (c < n)
237 {
238 itemset[itemsetsize] = core[c];
239 itemsetsize++;
240 c++;
241 }
242
243 if (trace_flag)
244 print_closure (n);
245 }
246
247
248 void
249 free_closure (void)
250 {
251 XFREE (itemset);
252 XFREE (ruleset);
253 XFREE (fderives);
254 }