]> git.saurik.com Git - bison.git/blame - src/closure.c
More.
[bison.git] / src / closure.c
CommitLineData
d0fb370f 1/* Subroutines for bison
914feea9 2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
d0fb370f 3
8dc26b76 4 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 5
8dc26b76
AD
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.
d0fb370f 10
8dc26b76
AD
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.
d0fb370f 15
8dc26b76
AD
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. */
d0fb370f 20
d0fb370f 21#include "system.h"
8b3df748 22#include "quotearg.h"
914feea9
AD
23#include "bitset.h"
24#include "bitsetv.h"
9bfe901c 25#include "getargs.h"
ad949da9 26#include "symtab.h"
d0fb370f 27#include "gram.h"
2c5f66ed 28#include "reader.h"
2fa6973e 29#include "closure.h"
340ef489 30#include "derives.h"
d0fb370f 31
b2872512 32/* NITEMSET is the size of the array ITEMSET. */
62a3e4f0 33item_number_t *itemset;
5123689b 34int nritemset;
fb908786 35
dfdb1797 36static bitset ruleset;
d0fb370f
RS
37
38/* internal data. See comments before set_fderives and set_firsts. */
914feea9
AD
39static bitsetv fderives = NULL;
40static bitsetv firsts = NULL;
d0fb370f 41
03ec521c 42/* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
7086e707 43#define FDERIVES(Var) fderives[(Var) - ntokens]
d8a0245c 44#define FIRSTS(Var) firsts[(Var) - ntokens]
2fa6973e 45\f
d0fb370f 46
2fa6973e
AD
47/*-----------------.
48| Debugging code. |
49`-----------------*/
d0fb370f 50
2fa6973e 51static void
62a3e4f0 52print_closure (const char *title, item_number_t *array, size_t size)
2fa6973e 53{
23cbcc6c
AD
54 size_t i;
55 fprintf (stderr, "Closure: %s\n", title);
56 for (i = 0; i < size; ++i)
57 {
62a3e4f0 58 item_number_t *rp;
23cbcc6c 59 fprintf (stderr, " %2d: .", array[i]);
75142d45 60 for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
8b3df748
AD
61 fprintf (stderr, " %s",
62 quotearg_style (escape_quoting_style, symbols[*rp]->tag));
29d29c8f 63 fprintf (stderr, " (rule %d)\n", -*rp - 1);
23cbcc6c
AD
64 }
65 fputs ("\n\n", stderr);
2fa6973e
AD
66}
67
68
69static void
70print_firsts (void)
d0fb370f 71{
84182270 72 int i, j;
2fa6973e 73
c87d4863 74 fprintf (stderr, "FIRSTS\n");
2fa6973e
AD
75 for (i = ntokens; i < nsyms; i++)
76 {
8b3df748
AD
77 fprintf (stderr, "\t%s firsts\n",
78 quotearg_style (escape_quoting_style, symbols[i]->tag));
2fa6973e 79 for (j = 0; j < nvars; j++)
d8a0245c 80 if (bitset_test (FIRSTS (i), j))
ad949da9 81 fprintf (stderr, "\t\t%d (%s)\n",
8b3df748
AD
82 j + ntokens,
83 quotearg_style (escape_quoting_style,
84 symbols[j + ntokens]->tag));
2fa6973e 85 }
c87d4863 86 fprintf (stderr, "\n\n");
d0fb370f
RS
87}
88
89
2fa6973e
AD
90static void
91print_fderives (void)
92{
d8a0245c 93 int i, j;
2fa6973e 94
c87d4863 95 fprintf (stderr, "FDERIVES\n");
2fa6973e
AD
96 for (i = ntokens; i < nsyms; i++)
97 {
8b3df748
AD
98 fprintf (stderr, "\t%s derives\n",
99 quotearg_style (escape_quoting_style, symbols[i]->tag));
18bcecb0 100 for (j = 0; j < nrules + 1; j++)
7086e707 101 if (bitset_test (FDERIVES (i), j))
720e5c1b 102 {
62a3e4f0 103 item_number_t *rhsp;
29d29c8f 104 fprintf (stderr, "\t\t%d:", j - 1);
99013900 105 for (rhsp = rules[j].rhs; *rhsp >= 0; ++rhsp)
8b3df748
AD
106 fprintf (stderr, " %s",
107 quotearg_style (escape_quoting_style,
108 symbols[*rhsp]->tag));
720e5c1b
AD
109 fputc ('\n', stderr);
110 }
2fa6973e 111 }
c87d4863 112 fprintf (stderr, "\n\n");
2fa6973e 113}
65ccf9fc
AD
114
115/*--------------------------------------------------------.
116| Display the MATRIX array of SIZE bitsets of size SIZE. |
117`--------------------------------------------------------*/
118
119static void
120bitmatrix_print (const char *title, bitsetv matrix)
121{
122 size_t i, j;
123 size_t size = bitset_size (matrix[0]);
124
125 /* Title. */
126 fprintf (stderr, "%s BEGIN\n", title);
127
128 /* Column numbers. */
129 fputs (" ", stderr);
130 for (i = 0; i < size; ++i)
131 putc (i / 10 ? '0' + i / 10 : ' ', stderr);
132 putc ('\n', stderr);
133 fputs (" ", stderr);
134 for (i = 0; i < size; ++i)
135 fprintf (stderr, "%d", i % 10);
136 putc ('\n', stderr);
137
138 /* Bar. */
139 fputs (" .", stderr);
140 for (i = 0; i < size; ++i)
141 putc ('-', stderr);
142 fputs (".\n", stderr);
143
144 /* Contents. */
145 for (i = 0; i < size; ++i)
146 {
147 fprintf (stderr, "%2d|", i);
148 for (j = 0; j < size; ++j)
149 fputs (bitset_test (matrix[i], j) ? "1" : " ", stderr);
150 fputs ("|\n", stderr);
151 }
152
153 /* Bar. */
154 fputs (" `", stderr);
155 for (i = 0; i < size; ++i)
156 putc ('-', stderr);
157 fputs ("'\n", stderr);
158
159 /* End title. */
160 fprintf (stderr, "%s END\n\n", title);
161}
2fa6973e 162\f
d8a0245c
AD
163/*------------------------------------------------------------------.
164| Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
165| items can represent the beginning of the input corresponding to |
166| which other items. |
167| |
168| For example, if some rule expands symbol 5 into the sequence of |
169| symbols 8 3 20, the symbol 8 can be the beginning of the data for |
170| symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
171| (5)) is set. |
172`------------------------------------------------------------------*/
2fa6973e
AD
173
174static void
175set_firsts (void)
176{
3f6f053c 177 int i, j;
2fa6973e 178
914feea9
AD
179 firsts = bitsetv_create (nvars, nvars, BITSET_FIXED);
180
2fa6973e 181 for (i = ntokens; i < nsyms; i++)
914feea9
AD
182 for (j = 0; derives[i][j] >= 0; ++j)
183 {
99013900 184 int symbol = rules[derives[i][j]].rhs[0];
914feea9
AD
185 if (ISVAR (symbol))
186 bitset_set (FIRSTS (i), symbol - ntokens);
187 }
2fa6973e 188
65ccf9fc
AD
189 if (trace_flag)
190 bitmatrix_print ("RTC: Input", firsts);
191 bitsetv_reflexive_transitive_closure (firsts);
192 if (trace_flag)
193 bitmatrix_print ("RTC: Output", firsts);
2fa6973e 194
9bfe901c
AD
195 if (trace_flag)
196 print_firsts ();
2fa6973e
AD
197}
198
199/*-------------------------------------------------------------------.
200| Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
201| rules can help derive the beginning of the data for each |
202| nonterminal. |
203| |
204| For example, if symbol 5 can be derived as the sequence of symbols |
205| 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
206| the [5 - NTOKENS, 4] bit in FDERIVES is set. |
207`-------------------------------------------------------------------*/
d0fb370f 208
4a120d45 209static void
d2729d44 210set_fderives (void)
d0fb370f 211{
1cbcf2e7 212 int i, j, k;
d0fb370f 213
914feea9 214 fderives = bitsetv_create (nvars, nrules + 1, BITSET_FIXED);
d0fb370f 215
2fa6973e 216 set_firsts ();
d0fb370f 217
1cbcf2e7
AD
218 for (i = ntokens; i < nsyms; ++i)
219 for (j = ntokens; j < nsyms; ++j)
d8a0245c 220 if (bitset_test (FIRSTS (i), j - ntokens))
1cbcf2e7 221 for (k = 0; derives[j][k] > 0; ++k)
7086e707 222 bitset_set (FDERIVES (i), derives[j][k]);
d0fb370f 223
9bfe901c
AD
224 if (trace_flag)
225 print_fderives ();
d0fb370f 226
914feea9 227 bitsetv_free (firsts);
d0fb370f 228}
2fa6973e 229\f
d0fb370f 230
2fa6973e
AD
231void
232new_closure (int n)
d0fb370f 233{
62a3e4f0 234 itemset = XCALLOC (item_number_t, n);
d0fb370f 235
dfdb1797 236 ruleset = bitset_create (nrules + 1, BITSET_FIXED);
d0fb370f 237
2fa6973e 238 set_fderives ();
d0fb370f
RS
239}
240
241
2fa6973e 242
d0fb370f 243void
62a3e4f0 244closure (item_number_t *core, int n)
d0fb370f 245{
576890b7
AD
246 /* Index over CORE. */
247 int c;
248
d2b04478 249 /* A bit index over RULESET. */
4b35e1c1 250 int ruleno;
d0fb370f 251
c87d4863 252 if (trace_flag)
23cbcc6c 253 print_closure ("input", core, n);
c87d4863 254
643a5994 255 bitset_zero (ruleset);
d0fb370f 256
643a5994
AD
257 for (c = 0; c < n; ++c)
258 if (ISVAR (ritem[core[c]]))
259 bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]]));
d0fb370f 260
5123689b 261 nritemset = 0;
576890b7 262 c = 0;
cb581495 263 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
dfdb1797 264 if (bitset_test (ruleset, ruleno))
4b35e1c1 265 {
62a3e4f0 266 item_number_t itemno = rules[ruleno].rhs - ritem;
4b35e1c1
AD
267 while (c < n && core[c] < itemno)
268 {
5123689b
AD
269 itemset[nritemset] = core[c];
270 nritemset++;
4b35e1c1
AD
271 c++;
272 }
5123689b
AD
273 itemset[nritemset] = itemno;
274 nritemset++;
4b35e1c1 275 }
d0fb370f 276
576890b7
AD
277 while (c < n)
278 {
5123689b
AD
279 itemset[nritemset] = core[c];
280 nritemset++;
576890b7
AD
281 c++;
282 }
d0fb370f 283
9bfe901c 284 if (trace_flag)
5123689b 285 print_closure ("output", itemset, nritemset);
d0fb370f
RS
286}
287
288
289void
2fa6973e 290free_closure (void)
d0fb370f 291{
d7913476 292 XFREE (itemset);
dfdb1797 293 bitset_free (ruleset);
914feea9 294 bitsetv_free (fderives);
d0fb370f 295}