]>
Commit | Line | Data |
---|---|---|
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" | |
1565b720 | 25 | #include "bitsetv-print.h" |
9bfe901c | 26 | #include "getargs.h" |
ad949da9 | 27 | #include "symtab.h" |
d0fb370f | 28 | #include "gram.h" |
2c5f66ed | 29 | #include "reader.h" |
2fa6973e | 30 | #include "closure.h" |
340ef489 | 31 | #include "derives.h" |
d0fb370f | 32 | |
b2872512 | 33 | /* NITEMSET is the size of the array ITEMSET. */ |
62a3e4f0 | 34 | item_number_t *itemset; |
5123689b | 35 | int nritemset; |
fb908786 | 36 | |
dfdb1797 | 37 | static bitset ruleset; |
d0fb370f RS |
38 | |
39 | /* internal data. See comments before set_fderives and set_firsts. */ | |
914feea9 AD |
40 | static bitsetv fderives = NULL; |
41 | static bitsetv firsts = NULL; | |
d0fb370f | 42 | |
03ec521c | 43 | /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */ |
7086e707 | 44 | #define FDERIVES(Var) fderives[(Var) - ntokens] |
d8a0245c | 45 | #define FIRSTS(Var) firsts[(Var) - ntokens] |
2fa6973e | 46 | \f |
d0fb370f | 47 | |
2fa6973e AD |
48 | /*-----------------. |
49 | | Debugging code. | | |
50 | `-----------------*/ | |
d0fb370f | 51 | |
2fa6973e | 52 | static void |
62a3e4f0 | 53 | print_closure (const char *title, item_number_t *array, size_t size) |
2fa6973e | 54 | { |
23cbcc6c AD |
55 | size_t i; |
56 | fprintf (stderr, "Closure: %s\n", title); | |
57 | for (i = 0; i < size; ++i) | |
58 | { | |
62a3e4f0 | 59 | item_number_t *rp; |
23cbcc6c | 60 | fprintf (stderr, " %2d: .", array[i]); |
75142d45 | 61 | for (rp = &ritem[array[i]]; *rp >= 0; ++rp) |
8b3df748 AD |
62 | fprintf (stderr, " %s", |
63 | quotearg_style (escape_quoting_style, symbols[*rp]->tag)); | |
29d29c8f | 64 | fprintf (stderr, " (rule %d)\n", -*rp - 1); |
23cbcc6c AD |
65 | } |
66 | fputs ("\n\n", stderr); | |
2fa6973e AD |
67 | } |
68 | ||
69 | ||
70 | static void | |
71 | print_firsts (void) | |
d0fb370f | 72 | { |
84182270 | 73 | int i, j; |
2fa6973e | 74 | |
c87d4863 | 75 | fprintf (stderr, "FIRSTS\n"); |
2fa6973e AD |
76 | for (i = ntokens; i < nsyms; i++) |
77 | { | |
8b3df748 AD |
78 | fprintf (stderr, "\t%s firsts\n", |
79 | quotearg_style (escape_quoting_style, symbols[i]->tag)); | |
2fa6973e | 80 | for (j = 0; j < nvars; j++) |
d8a0245c | 81 | if (bitset_test (FIRSTS (i), j)) |
1565b720 | 82 | fprintf (stderr, "\t\t%s\n", |
8b3df748 AD |
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 |
90 | static void |
91 | print_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 | } |
2fa6973e | 114 | \f |
d8a0245c AD |
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 | `------------------------------------------------------------------*/ | |
2fa6973e AD |
125 | |
126 | static void | |
127 | set_firsts (void) | |
128 | { | |
3f6f053c | 129 | int i, j; |
2fa6973e | 130 | |
914feea9 AD |
131 | firsts = bitsetv_create (nvars, nvars, BITSET_FIXED); |
132 | ||
2fa6973e | 133 | for (i = ntokens; i < nsyms; i++) |
914feea9 AD |
134 | for (j = 0; derives[i][j] >= 0; ++j) |
135 | { | |
99013900 | 136 | int symbol = rules[derives[i][j]].rhs[0]; |
914feea9 AD |
137 | if (ISVAR (symbol)) |
138 | bitset_set (FIRSTS (i), symbol - ntokens); | |
139 | } | |
2fa6973e | 140 | |
65ccf9fc | 141 | if (trace_flag) |
1565b720 | 142 | bitsetv_matrix_dump (stderr, "RTC: Firsts Input", firsts); |
65ccf9fc AD |
143 | bitsetv_reflexive_transitive_closure (firsts); |
144 | if (trace_flag) | |
1565b720 | 145 | bitsetv_matrix_dump (stderr, "RTC: Firsts Output", firsts); |
2fa6973e | 146 | |
9bfe901c AD |
147 | if (trace_flag) |
148 | print_firsts (); | |
2fa6973e AD |
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 | `-------------------------------------------------------------------*/ | |
d0fb370f | 160 | |
4a120d45 | 161 | static void |
d2729d44 | 162 | set_fderives (void) |
d0fb370f | 163 | { |
1cbcf2e7 | 164 | int i, j, k; |
d0fb370f | 165 | |
914feea9 | 166 | fderives = bitsetv_create (nvars, nrules + 1, BITSET_FIXED); |
d0fb370f | 167 | |
2fa6973e | 168 | set_firsts (); |
d0fb370f | 169 | |
1cbcf2e7 AD |
170 | for (i = ntokens; i < nsyms; ++i) |
171 | for (j = ntokens; j < nsyms; ++j) | |
d8a0245c | 172 | if (bitset_test (FIRSTS (i), j - ntokens)) |
1cbcf2e7 | 173 | for (k = 0; derives[j][k] > 0; ++k) |
7086e707 | 174 | bitset_set (FDERIVES (i), derives[j][k]); |
d0fb370f | 175 | |
9bfe901c AD |
176 | if (trace_flag) |
177 | print_fderives (); | |
d0fb370f | 178 | |
914feea9 | 179 | bitsetv_free (firsts); |
d0fb370f | 180 | } |
1565b720 | 181 | |
2fa6973e | 182 | \f |
d0fb370f | 183 | |
2fa6973e AD |
184 | void |
185 | new_closure (int n) | |
d0fb370f | 186 | { |
62a3e4f0 | 187 | itemset = XCALLOC (item_number_t, n); |
d0fb370f | 188 | |
dfdb1797 | 189 | ruleset = bitset_create (nrules + 1, BITSET_FIXED); |
d0fb370f | 190 | |
2fa6973e | 191 | set_fderives (); |
d0fb370f RS |
192 | } |
193 | ||
194 | ||
2fa6973e | 195 | |
d0fb370f | 196 | void |
62a3e4f0 | 197 | closure (item_number_t *core, int n) |
d0fb370f | 198 | { |
576890b7 AD |
199 | /* Index over CORE. */ |
200 | int c; | |
201 | ||
d2b04478 | 202 | /* A bit index over RULESET. */ |
4b35e1c1 | 203 | int ruleno; |
d0fb370f | 204 | |
c87d4863 | 205 | if (trace_flag) |
23cbcc6c | 206 | print_closure ("input", core, n); |
c87d4863 | 207 | |
643a5994 | 208 | bitset_zero (ruleset); |
d0fb370f | 209 | |
643a5994 AD |
210 | for (c = 0; c < n; ++c) |
211 | if (ISVAR (ritem[core[c]])) | |
212 | bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]])); | |
d0fb370f | 213 | |
5123689b | 214 | nritemset = 0; |
576890b7 | 215 | c = 0; |
cb581495 | 216 | for (ruleno = 0; ruleno < nrules + 1; ++ruleno) |
dfdb1797 | 217 | if (bitset_test (ruleset, ruleno)) |
4b35e1c1 | 218 | { |
62a3e4f0 | 219 | item_number_t itemno = rules[ruleno].rhs - ritem; |
4b35e1c1 AD |
220 | while (c < n && core[c] < itemno) |
221 | { | |
5123689b AD |
222 | itemset[nritemset] = core[c]; |
223 | nritemset++; | |
4b35e1c1 AD |
224 | c++; |
225 | } | |
5123689b AD |
226 | itemset[nritemset] = itemno; |
227 | nritemset++; | |
4b35e1c1 | 228 | } |
d0fb370f | 229 | |
576890b7 AD |
230 | while (c < n) |
231 | { | |
5123689b AD |
232 | itemset[nritemset] = core[c]; |
233 | nritemset++; | |
576890b7 AD |
234 | c++; |
235 | } | |
d0fb370f | 236 | |
9bfe901c | 237 | if (trace_flag) |
5123689b | 238 | print_closure ("output", itemset, nritemset); |
d0fb370f RS |
239 | } |
240 | ||
241 | ||
242 | void | |
2fa6973e | 243 | free_closure (void) |
d0fb370f | 244 | { |
d7913476 | 245 | XFREE (itemset); |
dfdb1797 | 246 | bitset_free (ruleset); |
914feea9 | 247 | bitsetv_free (fderives); |
d0fb370f | 248 | } |