]> git.saurik.com Git - bison.git/blame - src/closure.c
* src/conflicts.c (set_conflicts): Use bitset_disjoint_p.
[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"
914feea9
AD
22#include "bitset.h"
23#include "bitsetv.h"
9bfe901c 24#include "getargs.h"
ad949da9 25#include "symtab.h"
d0fb370f 26#include "gram.h"
2c5f66ed 27#include "reader.h"
2fa6973e 28#include "closure.h"
340ef489 29#include "derives.h"
d8a0245c 30#include "warshall.h"
d0fb370f 31
b2872512 32/* NITEMSET is the size of the array ITEMSET. */
d0fb370f 33short *itemset;
b2872512 34int nitemset;
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
23cbcc6c 52print_closure (const char *title, short *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 {
58 short *rp;
59 fprintf (stderr, " %2d: .", array[i]);
75142d45 60 for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
ad949da9 61 fprintf (stderr, " %s", symbols[*rp]->tag);
29d29c8f 62 fprintf (stderr, " (rule %d)\n", -*rp - 1);
23cbcc6c
AD
63 }
64 fputs ("\n\n", stderr);
2fa6973e
AD
65}
66
67
68static void
69print_firsts (void)
d0fb370f 70{
84182270 71 int i, j;
2fa6973e 72
c87d4863 73 fprintf (stderr, "FIRSTS\n");
2fa6973e
AD
74 for (i = ntokens; i < nsyms; i++)
75 {
ad949da9 76 fprintf (stderr, "\t%s firsts\n", symbols[i]->tag);
2fa6973e 77 for (j = 0; j < nvars; j++)
d8a0245c 78 if (bitset_test (FIRSTS (i), j))
ad949da9
AD
79 fprintf (stderr, "\t\t%d (%s)\n",
80 j + ntokens, symbols[j + ntokens]->tag);
2fa6973e 81 }
c87d4863 82 fprintf (stderr, "\n\n");
d0fb370f
RS
83}
84
85
2fa6973e
AD
86static void
87print_fderives (void)
88{
d8a0245c 89 int i, j;
2fa6973e 90
c87d4863 91 fprintf (stderr, "FDERIVES\n");
2fa6973e
AD
92 for (i = ntokens; i < nsyms; i++)
93 {
ad949da9 94 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
2fa6973e 95 for (j = 0; j <= nrules; j++)
7086e707 96 if (bitset_test (FDERIVES (i), j))
720e5c1b
AD
97 {
98 short *rhsp;
29d29c8f 99 fprintf (stderr, "\t\t%d:", j - 1);
1a2b5d37 100 for (rhsp = &ritem[rules[j].rhs]; *rhsp >= 0; ++rhsp)
ad949da9 101 fprintf (stderr, " %s", symbols[*rhsp]->tag);
720e5c1b
AD
102 fputc ('\n', stderr);
103 }
2fa6973e 104 }
c87d4863 105 fprintf (stderr, "\n\n");
2fa6973e 106}
2fa6973e 107\f
d8a0245c
AD
108/*------------------------------------------------------------------.
109| Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
110| items can represent the beginning of the input corresponding to |
111| which other items. |
112| |
113| For example, if some rule expands symbol 5 into the sequence of |
114| symbols 8 3 20, the symbol 8 can be the beginning of the data for |
115| symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
116| (5)) is set. |
117`------------------------------------------------------------------*/
2fa6973e
AD
118
119static void
120set_firsts (void)
121{
3f6f053c 122 int i, j;
2fa6973e 123
914feea9
AD
124 firsts = bitsetv_create (nvars, nvars, BITSET_FIXED);
125
2fa6973e 126 for (i = ntokens; i < nsyms; i++)
914feea9
AD
127 for (j = 0; derives[i][j] >= 0; ++j)
128 {
129 int symbol = ritem[rules[derives[i][j]].rhs];
130 if (ISVAR (symbol))
131 bitset_set (FIRSTS (i), symbol - ntokens);
132 }
2fa6973e 133
914feea9 134 RTC (firsts);
2fa6973e 135
9bfe901c
AD
136 if (trace_flag)
137 print_firsts ();
2fa6973e
AD
138}
139
140/*-------------------------------------------------------------------.
141| Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
142| rules can help derive the beginning of the data for each |
143| nonterminal. |
144| |
145| For example, if symbol 5 can be derived as the sequence of symbols |
146| 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
147| the [5 - NTOKENS, 4] bit in FDERIVES is set. |
148`-------------------------------------------------------------------*/
d0fb370f 149
4a120d45 150static void
d2729d44 151set_fderives (void)
d0fb370f 152{
1cbcf2e7 153 int i, j, k;
d0fb370f 154
914feea9 155 fderives = bitsetv_create (nvars, nrules + 1, BITSET_FIXED);
d0fb370f 156
2fa6973e 157 set_firsts ();
d0fb370f 158
1cbcf2e7
AD
159 for (i = ntokens; i < nsyms; ++i)
160 for (j = ntokens; j < nsyms; ++j)
d8a0245c 161 if (bitset_test (FIRSTS (i), j - ntokens))
1cbcf2e7 162 for (k = 0; derives[j][k] > 0; ++k)
7086e707 163 bitset_set (FDERIVES (i), derives[j][k]);
d0fb370f 164
9bfe901c
AD
165 if (trace_flag)
166 print_fderives ();
d0fb370f 167
914feea9 168 bitsetv_free (firsts);
d0fb370f 169}
2fa6973e 170\f
d0fb370f 171
2fa6973e
AD
172void
173new_closure (int n)
d0fb370f 174{
d7913476 175 itemset = XCALLOC (short, n);
d0fb370f 176
dfdb1797 177 ruleset = bitset_create (nrules + 1, BITSET_FIXED);
d0fb370f 178
2fa6973e 179 set_fderives ();
d0fb370f
RS
180}
181
182
2fa6973e 183
d0fb370f 184void
d2729d44 185closure (short *core, int n)
d0fb370f 186{
576890b7
AD
187 /* Index over CORE. */
188 int c;
189
8e864447
AD
190 /* An index over RULESET. */
191 int r;
192
d2b04478 193 /* A bit index over RULESET. */
4b35e1c1 194 int ruleno;
d0fb370f 195
c87d4863 196 if (trace_flag)
23cbcc6c 197 print_closure ("input", core, n);
c87d4863 198
d0fb370f
RS
199 if (n == 0)
200 {
dfdb1797 201 bitset_copy (ruleset, FDERIVES (start_symbol));
d0fb370f
RS
202 }
203 else
204 {
dfdb1797 205 bitset_zero (ruleset);
d0fb370f 206
576890b7
AD
207 for (c = 0; c < n; ++c)
208 if (ISVAR (ritem[core[c]]))
dfdb1797 209 bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]]));
d0fb370f
RS
210 }
211
b2872512 212 nitemset = 0;
576890b7 213 c = 0;
cb581495 214 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
dfdb1797 215 if (bitset_test (ruleset, ruleno))
4b35e1c1 216 {
1a2b5d37 217 int itemno = rules[ruleno].rhs;
4b35e1c1
AD
218 while (c < n && core[c] < itemno)
219 {
b2872512
AD
220 itemset[nitemset] = core[c];
221 nitemset++;
4b35e1c1
AD
222 c++;
223 }
b2872512
AD
224 itemset[nitemset] = itemno;
225 nitemset++;
4b35e1c1 226 }
d0fb370f 227
576890b7
AD
228 while (c < n)
229 {
b2872512
AD
230 itemset[nitemset] = core[c];
231 nitemset++;
576890b7
AD
232 c++;
233 }
d0fb370f 234
9bfe901c 235 if (trace_flag)
b2872512 236 print_closure ("output", itemset, nitemset);
d0fb370f
RS
237}
238
239
240void
2fa6973e 241free_closure (void)
d0fb370f 242{
d7913476 243 XFREE (itemset);
dfdb1797 244 bitset_free (ruleset);
914feea9 245 bitsetv_free (fderives);
d0fb370f 246}