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