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