]>
git.saurik.com Git - bison.git/blob - src/closure.c
1 /* Subroutines for bison
2 Copyright (C) 1984, 1989 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* subroutines of file LR0.c.
27 Given a vector of item numbers items, of length n,
28 set up ruleset and itemset to indicate what rules could be run
29 and which items could be accepted when those items are the active ones.
31 ruleset contains a bit for each rule. closure sets the bits
32 for all rules which could potentially describe the next input to be read.
34 itemset is a vector of item numbers; itemsetend points to just beyond the end
35 of the part of it that is significant.
36 closure places there the indices of all items which represent units of
37 input that could arrive next.
39 initialize_closure (n)
41 Allocates the itemset and ruleset vectors,
42 and precomputes useful data so that closure can be called.
43 n is the number of elements to allocate for itemset.
47 Frees itemset, ruleset and internal data.
58 extern short **derives
;
68 static unsigned *ruleset
;
70 /* internal data. See comments before set_fderives and set_firsts. */
71 static unsigned *fderives
;
72 static unsigned *firsts
;
74 /* number of words required to hold a bit for each rule */
75 static int rulesetsize
;
77 /* number of words required to hold a bit for each variable */
78 static int varsetsize
;
85 itemset
= NEW2(n
, short);
87 rulesetsize
= WORDSIZE(nrules
+ 1);
88 ruleset
= NEW2(rulesetsize
, unsigned);
95 /* set fderives to an nvars by nrules matrix of bits
96 indicating which rules can help derive the beginning of the data
97 for each nonterminal. For example, if symbol 5 can be derived as
98 the sequence of symbols 8 3 20, and one of the rules for deriving
99 symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
103 register unsigned *rrow
;
104 register unsigned *vrow
;
106 register unsigned cword
;
113 fderives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
117 rrow
= fderives
+ ntokens
* rulesetsize
;
119 for (i
= ntokens
; i
< nsyms
; i
++)
121 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
124 for (j
= ntokens
; j
< nsyms
; j
++)
126 if (cword
& (1 << b
))
129 while ((ruleno
= *rp
++) > 0)
131 SETBIT(rrow
, ruleno
);
136 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
155 /* set firsts to be an nvars by nvars bit matrix indicating which items
156 can represent the beginning of the input corresponding to which other items.
157 For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20,
158 the symbol 8 can be the beginning of the data for symbol 5,
159 so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */
163 register unsigned *row
;
164 /* register int done; JF unused */
167 register int rowsize
;
171 varsetsize
= rowsize
= WORDSIZE(nvars
);
173 firsts
= NEW2(nvars
* rowsize
, unsigned);
176 for (i
= ntokens
; i
< nsyms
; i
++)
181 symbol
= ritem
[rrhs
[*sp
++]];
206 register unsigned word
;
208 register unsigned *dsp
;
209 register unsigned *rsp
;
217 rsend
= ruleset
+ rulesetsize
;
222 dsp
= fderives
+ start_symbol
* rulesetsize
;
234 symbol
= ritem
[*csp
++];
237 dsp
= fderives
+ symbol
* rulesetsize
;
246 itemsetend
= itemset
;
254 ruleno
+= BITS_PER_WORD
;
260 for (b
= 0; b
< BITS_PER_WORD
; b
++)
264 itemno
= rrhs
[ruleno
];
265 while (csp
< csend
&& *csp
< itemno
)
266 *itemsetend
++ = *csp
++;
267 *itemsetend
++ = itemno
;
276 *itemsetend
++ = *csp
++;
289 FREE(fderives
+ ntokens
* rulesetsize
);
301 printf("\n\nn = %d\n\n", n
);
302 for (isp
= itemset
; isp
< itemsetend
; isp
++)
303 printf(" %d\n", *isp
);
312 register unsigned *rowp
;
314 printf(_("\n\n\nFIRSTS\n\n"));
316 for (i
= ntokens
; i
< nsyms
; i
++)
318 printf(_("\n\n%s firsts\n\n"), tags
[i
]);
320 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
322 for (j
= 0; j
< nvars
; j
++)
323 if (BITISSET (rowp
, j
))
324 printf(" %s\n", tags
[j
+ ntokens
]);
334 register unsigned *rp
;
336 printf(_("\n\n\nFDERIVES\n"));
338 for (i
= ntokens
; i
< nsyms
; i
++)
340 printf(_("\n\n%s derives\n\n"), tags
[i
]);
341 rp
= fderives
+ i
* rulesetsize
;
343 for (j
= 0; j
<= nrules
; j
++)
344 if (BITISSET (rp
, j
))