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