]>
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* subroutines of file LR0.c.
28 Given a vector of item numbers items, of length n,
29 set up ruleset and itemset to indicate what rules could be run
30 and which items could be accepted when those items are the active ones.
32 ruleset contains a bit for each rule. closure sets the bits
33 for all rules which could potentially describe the next input to be read.
35 itemset is a vector of item numbers; itemsetend points to just beyond the end
36 of the part of it that is significant.
37 closure places there the indices of all items which represent units of
38 input that could arrive next.
40 initialize_closure (n)
42 Allocates the itemset and ruleset vectors,
43 and precomputes useful data so that closure can be called.
44 n is the number of elements to allocate for itemset.
48 Frees itemset, ruleset and internal data.
59 extern short **derives
;
62 void initialize_closure
PARAMS((int));
63 void set_fderives
PARAMS((void));
64 void set_firsts
PARAMS((void));
65 void closure
PARAMS((short *, int));
66 void finalize_closure
PARAMS((void));
68 extern void RTC
PARAMS((unsigned *, int));
72 static unsigned *ruleset
;
74 /* internal data. See comments before set_fderives and set_firsts. */
75 static unsigned *fderives
;
76 static unsigned *firsts
;
78 /* number of words required to hold a bit for each rule */
79 static int rulesetsize
;
81 /* number of words required to hold a bit for each variable */
82 static int varsetsize
;
86 initialize_closure (int n
)
88 itemset
= NEW2(n
, short);
90 rulesetsize
= WORDSIZE(nrules
+ 1);
91 ruleset
= NEW2(rulesetsize
, unsigned);
98 /* set fderives to an nvars by nrules matrix of bits
99 indicating which rules can help derive the beginning of the data
100 for each nonterminal. For example, if symbol 5 can be derived as
101 the sequence of symbols 8 3 20, and one of the rules for deriving
102 symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
106 register unsigned *rrow
;
107 register unsigned *vrow
;
109 register unsigned cword
;
116 fderives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
120 rrow
= fderives
+ ntokens
* rulesetsize
;
122 for (i
= ntokens
; i
< nsyms
; i
++)
124 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
127 for (j
= ntokens
; j
< nsyms
; j
++)
129 if (cword
& (1 << b
))
132 while ((ruleno
= *rp
++) > 0)
134 SETBIT(rrow
, ruleno
);
139 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
158 /* set firsts to be an nvars by nvars bit matrix indicating which items
159 can represent the beginning of the input corresponding to which other items.
160 For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20,
161 the symbol 8 can be the beginning of the data for symbol 5,
162 so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */
166 register unsigned *row
;
167 /* register int done; JF unused */
170 register int rowsize
;
174 varsetsize
= rowsize
= WORDSIZE(nvars
);
176 firsts
= NEW2(nvars
* rowsize
, unsigned);
179 for (i
= ntokens
; i
< nsyms
; i
++)
184 symbol
= ritem
[rrhs
[*sp
++]];
204 closure (short *core
, int n
)
207 register unsigned word
;
209 register unsigned *dsp
;
210 register unsigned *rsp
;
218 rsend
= ruleset
+ rulesetsize
;
223 dsp
= fderives
+ start_symbol
* rulesetsize
;
235 symbol
= ritem
[*csp
++];
238 dsp
= fderives
+ symbol
* rulesetsize
;
247 itemsetend
= itemset
;
255 ruleno
+= BITS_PER_WORD
;
261 for (b
= 0; b
< BITS_PER_WORD
; b
++)
265 itemno
= rrhs
[ruleno
];
266 while (csp
< csend
&& *csp
< itemno
)
267 *itemsetend
++ = *csp
++;
268 *itemsetend
++ = itemno
;
277 *itemsetend
++ = *csp
++;
286 finalize_closure (void)
290 FREE(fderives
+ ntokens
* rulesetsize
);
302 printf("\n\nn = %d\n\n", n
);
303 for (isp
= itemset
; isp
< itemsetend
; isp
++)
304 printf(" %d\n", *isp
);
313 register unsigned *rowp
;
315 printf(_("\n\n\nFIRSTS\n\n"));
317 for (i
= ntokens
; i
< nsyms
; i
++)
319 printf(_("\n\n%s firsts\n\n"), tags
[i
]);
321 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
323 for (j
= 0; j
< nvars
; j
++)
324 if (BITISSET (rowp
, j
))
325 printf(" %s\n", tags
[j
+ ntokens
]);
331 print_fderives (void)
335 register unsigned *rp
;
337 printf(_("\n\n\nFDERIVES\n"));
339 for (i
= ntokens
; i
< nsyms
; i
++)
341 printf(_("\n\n%s derives\n\n"), tags
[i
]);
342 rp
= fderives
+ i
* rulesetsize
;
344 for (j
= 0; j
<= nrules
; j
++)
345 if (BITISSET (rp
, j
))