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