]>
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) |
6b98e4b5 | 62 | fprintf (stderr, " %s", symbol_tag_get (symbols[*rp])); |
29d29c8f | 63 | fprintf (stderr, " (rule %d)\n", -*rp - 1); |
23cbcc6c AD |
64 | } |
65 | fputs ("\n\n", stderr); | |
2fa6973e AD |
66 | } |
67 | ||
68 | ||
69 | static void | |
70 | print_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 | { | |
6b98e4b5 | 77 | fprintf (stderr, "\t%s firsts\n", symbol_tag_get (symbols[i])); |
2fa6973e | 78 | for (j = 0; j < nvars; j++) |
d8a0245c | 79 | if (bitset_test (FIRSTS (i), j)) |
1565b720 | 80 | fprintf (stderr, "\t\t%s\n", |
6b98e4b5 | 81 | symbol_tag_get (symbols[j + ntokens])); |
2fa6973e | 82 | } |
c87d4863 | 83 | fprintf (stderr, "\n\n"); |
d0fb370f RS |
84 | } |
85 | ||
86 | ||
2fa6973e AD |
87 | static void |
88 | print_fderives (void) | |
89 | { | |
d8a0245c | 90 | int i, j; |
2fa6973e | 91 | |
c87d4863 | 92 | fprintf (stderr, "FDERIVES\n"); |
2fa6973e AD |
93 | for (i = ntokens; i < nsyms; i++) |
94 | { | |
6b98e4b5 | 95 | fprintf (stderr, "\t%s derives\n", symbol_tag_get (symbols[i])); |
18bcecb0 | 96 | for (j = 0; j < nrules + 1; j++) |
7086e707 | 97 | if (bitset_test (FDERIVES (i), j)) |
720e5c1b | 98 | { |
62a3e4f0 | 99 | item_number_t *rhsp; |
29d29c8f | 100 | fprintf (stderr, "\t\t%d:", j - 1); |
99013900 | 101 | for (rhsp = rules[j].rhs; *rhsp >= 0; ++rhsp) |
6b98e4b5 | 102 | fprintf (stderr, " %s", symbol_tag_get (symbols[*rhsp])); |
720e5c1b AD |
103 | fputc ('\n', stderr); |
104 | } | |
2fa6973e | 105 | } |
c87d4863 | 106 | fprintf (stderr, "\n\n"); |
2fa6973e | 107 | } |
2fa6973e | 108 | \f |
d8a0245c AD |
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 | `------------------------------------------------------------------*/ | |
2fa6973e AD |
119 | |
120 | static void | |
121 | set_firsts (void) | |
122 | { | |
3f6f053c | 123 | int i, j; |
2fa6973e | 124 | |
914feea9 AD |
125 | firsts = bitsetv_create (nvars, nvars, BITSET_FIXED); |
126 | ||
2fa6973e | 127 | for (i = ntokens; i < nsyms; i++) |
914feea9 AD |
128 | for (j = 0; derives[i][j] >= 0; ++j) |
129 | { | |
99013900 | 130 | int symbol = rules[derives[i][j]].rhs[0]; |
914feea9 AD |
131 | if (ISVAR (symbol)) |
132 | bitset_set (FIRSTS (i), symbol - ntokens); | |
133 | } | |
2fa6973e | 134 | |
65ccf9fc | 135 | if (trace_flag) |
1565b720 | 136 | bitsetv_matrix_dump (stderr, "RTC: Firsts Input", firsts); |
65ccf9fc AD |
137 | bitsetv_reflexive_transitive_closure (firsts); |
138 | if (trace_flag) | |
1565b720 | 139 | bitsetv_matrix_dump (stderr, "RTC: Firsts Output", firsts); |
2fa6973e | 140 | |
9bfe901c AD |
141 | if (trace_flag) |
142 | print_firsts (); | |
2fa6973e AD |
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 | `-------------------------------------------------------------------*/ | |
d0fb370f | 154 | |
4a120d45 | 155 | static void |
d2729d44 | 156 | set_fderives (void) |
d0fb370f | 157 | { |
1cbcf2e7 | 158 | int i, j, k; |
d0fb370f | 159 | |
914feea9 | 160 | fderives = bitsetv_create (nvars, nrules + 1, BITSET_FIXED); |
d0fb370f | 161 | |
2fa6973e | 162 | set_firsts (); |
d0fb370f | 163 | |
1cbcf2e7 AD |
164 | for (i = ntokens; i < nsyms; ++i) |
165 | for (j = ntokens; j < nsyms; ++j) | |
d8a0245c | 166 | if (bitset_test (FIRSTS (i), j - ntokens)) |
1cbcf2e7 | 167 | for (k = 0; derives[j][k] > 0; ++k) |
7086e707 | 168 | bitset_set (FDERIVES (i), derives[j][k]); |
d0fb370f | 169 | |
9bfe901c AD |
170 | if (trace_flag) |
171 | print_fderives (); | |
d0fb370f | 172 | |
914feea9 | 173 | bitsetv_free (firsts); |
d0fb370f | 174 | } |
1565b720 | 175 | |
2fa6973e | 176 | \f |
d0fb370f | 177 | |
2fa6973e AD |
178 | void |
179 | new_closure (int n) | |
d0fb370f | 180 | { |
62a3e4f0 | 181 | itemset = XCALLOC (item_number_t, n); |
d0fb370f | 182 | |
dfdb1797 | 183 | ruleset = bitset_create (nrules + 1, BITSET_FIXED); |
d0fb370f | 184 | |
2fa6973e | 185 | set_fderives (); |
d0fb370f RS |
186 | } |
187 | ||
188 | ||
2fa6973e | 189 | |
d0fb370f | 190 | void |
62a3e4f0 | 191 | closure (item_number_t *core, int n) |
d0fb370f | 192 | { |
576890b7 AD |
193 | /* Index over CORE. */ |
194 | int c; | |
195 | ||
d2b04478 | 196 | /* A bit index over RULESET. */ |
4b35e1c1 | 197 | int ruleno; |
d0fb370f | 198 | |
c87d4863 | 199 | if (trace_flag) |
23cbcc6c | 200 | print_closure ("input", core, n); |
c87d4863 | 201 | |
643a5994 | 202 | bitset_zero (ruleset); |
d0fb370f | 203 | |
643a5994 AD |
204 | for (c = 0; c < n; ++c) |
205 | if (ISVAR (ritem[core[c]])) | |
206 | bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]])); | |
d0fb370f | 207 | |
5123689b | 208 | nritemset = 0; |
576890b7 | 209 | c = 0; |
cb581495 | 210 | for (ruleno = 0; ruleno < nrules + 1; ++ruleno) |
dfdb1797 | 211 | if (bitset_test (ruleset, ruleno)) |
4b35e1c1 | 212 | { |
62a3e4f0 | 213 | item_number_t itemno = rules[ruleno].rhs - ritem; |
4b35e1c1 AD |
214 | while (c < n && core[c] < itemno) |
215 | { | |
5123689b AD |
216 | itemset[nritemset] = core[c]; |
217 | nritemset++; | |
4b35e1c1 AD |
218 | c++; |
219 | } | |
5123689b AD |
220 | itemset[nritemset] = itemno; |
221 | nritemset++; | |
4b35e1c1 | 222 | } |
d0fb370f | 223 | |
576890b7 AD |
224 | while (c < n) |
225 | { | |
5123689b AD |
226 | itemset[nritemset] = core[c]; |
227 | nritemset++; | |
576890b7 AD |
228 | c++; |
229 | } | |
d0fb370f | 230 | |
9bfe901c | 231 | if (trace_flag) |
5123689b | 232 | print_closure ("output", itemset, nritemset); |
d0fb370f RS |
233 | } |
234 | ||
235 | ||
236 | void | |
2fa6973e | 237 | free_closure (void) |
d0fb370f | 238 | { |
d7913476 | 239 | XFREE (itemset); |
dfdb1797 | 240 | bitset_free (ruleset); |
914feea9 | 241 | bitsetv_free (fderives); |
d0fb370f | 242 | } |