]> git.saurik.com Git - bison.git/blob - src/closure.c
All the hacks using a final pseudo state are now useless.
[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 /* NITEMSET is the size of the array ITEMSET. */
30 short *itemset;
31 int nitemset;
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 (const char *title, short *array, size_t size)
56 {
57 size_t i;
58 fprintf (stderr, "Closure: %s\n", title);
59 for (i = 0; i < size; ++i)
60 {
61 short *rp;
62 fprintf (stderr, " %2d: .", array[i]);
63 for (rp = &ritem[array[i]]; *rp > 0; ++rp)
64 fprintf (stderr, " %s", tags[*rp]);
65 fprintf (stderr, " (rule %d)\n", -*rp);
66 }
67 fputs ("\n\n", stderr);
68 }
69
70
71 static void
72 print_firsts (void)
73 {
74 int i, j;
75
76 fprintf (stderr, "FIRSTS\n");
77 for (i = ntokens; i < nsyms; i++)
78 {
79 fprintf (stderr, "\t%s firsts\n", tags[i]);
80 for (j = 0; j < nvars; j++)
81 if (BITISSET (FIRSTS (i), j))
82 fprintf (stderr, "\t\t%d (%s)\n", j + ntokens, tags[j + ntokens]);
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", tags[i]);
99 for (j = 0; j <= nrules; j++)
100 if (BITISSET (FDERIVES (i), j))
101 {
102 short *rhsp;
103 fprintf (stderr, "\t\t%d:", j);
104 for (rhsp = ritem + rule_table[j].rhs; *rhsp > 0; ++rhsp)
105 fprintf (stderr, " %s", tags[*rhsp]);
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[rule_table[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 (unsigned, nvars * rulesetsize);
161
162 set_firsts ();
163
164 for (i = ntokens; i < nsyms; ++i)
165 for (j = ntokens; j < nsyms; ++j)
166 if (BITISSET (FIRSTS (i), j - ntokens))
167 for (k = 0; derives[j][k] > 0; ++k)
168 SETBIT (FDERIVES (i), derives[j][k]);
169
170 if (trace_flag)
171 print_fderives ();
172
173 XFREE (firsts);
174 }
175 \f
176
177 void
178 new_closure (int n)
179 {
180 itemset = XCALLOC (short, n);
181
182 rulesetsize = WORDSIZE (nrules + 1);
183 ruleset = XCALLOC (unsigned, rulesetsize);
184
185 set_fderives ();
186 }
187
188
189
190 void
191 closure (short *core, int n)
192 {
193 /* Index over CORE. */
194 int c;
195
196 /* Index over RULESET. */
197 int r;
198
199 /* A bit index over RULESET. */
200 int ruleno;
201
202 if (trace_flag)
203 print_closure ("input", core, n);
204
205 if (n == 0)
206 {
207 for (r = 0; r < rulesetsize; ++r)
208 ruleset[r] = FDERIVES (start_symbol)[r];
209 }
210 else
211 {
212 for (r = 0; r < rulesetsize; ++r)
213 ruleset[r] = 0;
214
215 for (c = 0; c < n; ++c)
216 if (ISVAR (ritem[core[c]]))
217 for (r = 0; r < rulesetsize; ++r)
218 ruleset[r] |= FDERIVES (ritem[core[c]])[r];
219 }
220
221 nitemset = 0;
222 c = 0;
223 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
224 if (BITISSET (ruleset, ruleno))
225 {
226 int itemno = rule_table[ruleno].rhs;
227 while (c < n && core[c] < itemno)
228 {
229 itemset[nitemset] = core[c];
230 nitemset++;
231 c++;
232 }
233 itemset[nitemset] = itemno;
234 nitemset++;
235 }
236
237 while (c < n)
238 {
239 itemset[nitemset] = core[c];
240 nitemset++;
241 c++;
242 }
243
244 if (trace_flag)
245 print_closure ("output", itemset, nitemset);
246 }
247
248
249 void
250 free_closure (void)
251 {
252 XFREE (itemset);
253 XFREE (ruleset);
254 XFREE (fderives);
255 }