]>
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
;
61 void initialize_closure
PARAMS((int));
62 void set_fderives
PARAMS((void));
63 void set_firsts
PARAMS((void));
64 void closure
PARAMS((short *, int));
65 void finalize_closure
PARAMS((void));
67 extern void RTC
PARAMS((unsigned *, int));
71 static unsigned *ruleset
;
73 /* internal data. See comments before set_fderives and set_firsts. */
74 static unsigned *fderives
;
75 static unsigned *firsts
;
77 /* number of words required to hold a bit for each rule */
78 static int rulesetsize
;
80 /* number of words required to hold a bit for each variable */
81 static int varsetsize
;
85 initialize_closure (int n
)
87 itemset
= NEW2(n
, short);
89 rulesetsize
= WORDSIZE(nrules
+ 1);
90 ruleset
= NEW2(rulesetsize
, unsigned);
97 /* set fderives to an nvars by nrules matrix of bits
98 indicating which rules can help derive the beginning of the data
99 for each nonterminal. For example, if symbol 5 can be derived as
100 the sequence of symbols 8 3 20, and one of the rules for deriving
101 symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
105 register unsigned *rrow
;
106 register unsigned *vrow
;
108 register unsigned cword
;
115 fderives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
119 rrow
= fderives
+ ntokens
* rulesetsize
;
121 for (i
= ntokens
; i
< nsyms
; i
++)
123 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
126 for (j
= ntokens
; j
< nsyms
; j
++)
128 if (cword
& (1 << b
))
131 while ((ruleno
= *rp
++) > 0)
133 SETBIT(rrow
, ruleno
);
138 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
157 /* set firsts to be an nvars by nvars bit matrix indicating which items
158 can represent the beginning of the input corresponding to which other items.
159 For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20,
160 the symbol 8 can be the beginning of the data for symbol 5,
161 so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */
165 register unsigned *row
;
166 /* register int done; JF unused */
169 register int rowsize
;
173 varsetsize
= rowsize
= WORDSIZE(nvars
);
175 firsts
= NEW2(nvars
* rowsize
, unsigned);
178 for (i
= ntokens
; i
< nsyms
; i
++)
183 symbol
= ritem
[rrhs
[*sp
++]];
203 closure (short *core
, int n
)
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
++;
285 finalize_closure (void)
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
]);
330 print_fderives (void)
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
))