]> git.saurik.com Git - bison.git/blob - src/closure.c
* src/LR0.c (state_list_t, state_list_append): New.
[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", symbol_tag_get (symbols[*rp]));
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 int i, j;
73
74 fprintf (stderr, "FIRSTS\n");
75 for (i = ntokens; i < nsyms; i++)
76 {
77 fprintf (stderr, "\t%s firsts\n", symbol_tag_get (symbols[i]));
78 for (j = 0; j < nvars; j++)
79 if (bitset_test (FIRSTS (i), j))
80 fprintf (stderr, "\t\t%s\n",
81 symbol_tag_get (symbols[j + ntokens]));
82 }
83 fprintf (stderr, "\n\n");
84 }
85
86
87 static void
88 print_fderives (void)
89 {
90 int i, j;
91
92 fprintf (stderr, "FDERIVES\n");
93 for (i = ntokens; i < nsyms; i++)
94 {
95 fprintf (stderr, "\t%s derives\n", symbol_tag_get (symbols[i]));
96 for (j = 0; j < nrules + 1; j++)
97 if (bitset_test (FDERIVES (i), j))
98 {
99 item_number_t *rhsp;
100 fprintf (stderr, "\t\t%d:", j - 1);
101 for (rhsp = rules[j].rhs; *rhsp >= 0; ++rhsp)
102 fprintf (stderr, " %s", symbol_tag_get (symbols[*rhsp]));
103 fputc ('\n', stderr);
104 }
105 }
106 fprintf (stderr, "\n\n");
107 }
108 \f
109 /*------------------------------------------------------------------.
110 | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
111 | items can represent the beginning of the input corresponding to |
112 | which other items. |
113 | |
114 | For example, if some rule expands symbol 5 into the sequence of |
115 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
116 | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
117 | (5)) is set. |
118 `------------------------------------------------------------------*/
119
120 static void
121 set_firsts (void)
122 {
123 int i, j;
124
125 firsts = bitsetv_create (nvars, nvars, BITSET_FIXED);
126
127 for (i = ntokens; i < nsyms; i++)
128 for (j = 0; derives[i][j] >= 0; ++j)
129 {
130 int symbol = rules[derives[i][j]].rhs[0];
131 if (ISVAR (symbol))
132 bitset_set (FIRSTS (i), symbol - ntokens);
133 }
134
135 if (trace_flag)
136 bitsetv_matrix_dump (stderr, "RTC: Firsts Input", firsts);
137 bitsetv_reflexive_transitive_closure (firsts);
138 if (trace_flag)
139 bitsetv_matrix_dump (stderr, "RTC: Firsts Output", firsts);
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 = bitsetv_create (nvars, nrules + 1, BITSET_FIXED);
161
162 set_firsts ();
163
164 for (i = ntokens; i < nsyms; ++i)
165 for (j = ntokens; j < nsyms; ++j)
166 if (bitset_test (FIRSTS (i), j - ntokens))
167 for (k = 0; derives[j][k] > 0; ++k)
168 bitset_set (FDERIVES (i), derives[j][k]);
169
170 if (trace_flag)
171 print_fderives ();
172
173 bitsetv_free (firsts);
174 }
175
176 \f
177
178 void
179 new_closure (int n)
180 {
181 itemset = XCALLOC (item_number_t, n);
182
183 ruleset = bitset_create (nrules + 1, BITSET_FIXED);
184
185 set_fderives ();
186 }
187
188
189
190 void
191 closure (item_number_t *core, int n)
192 {
193 /* Index over CORE. */
194 int c;
195
196 /* A bit index over RULESET. */
197 int ruleno;
198
199 if (trace_flag)
200 print_closure ("input", core, n);
201
202 bitset_zero (ruleset);
203
204 for (c = 0; c < n; ++c)
205 if (ISVAR (ritem[core[c]]))
206 bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]]));
207
208 nritemset = 0;
209 c = 0;
210 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
211 if (bitset_test (ruleset, ruleno))
212 {
213 item_number_t itemno = rules[ruleno].rhs - ritem;
214 while (c < n && core[c] < itemno)
215 {
216 itemset[nritemset] = core[c];
217 nritemset++;
218 c++;
219 }
220 itemset[nritemset] = itemno;
221 nritemset++;
222 }
223
224 while (c < n)
225 {
226 itemset[nritemset] = core[c];
227 nritemset++;
228 c++;
229 }
230
231 if (trace_flag)
232 print_closure ("output", itemset, nritemset);
233 }
234
235
236 void
237 free_closure (void)
238 {
239 XFREE (itemset);
240 bitset_free (ruleset);
241 bitsetv_free (fderives);
242 }