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