]>
git.saurik.com Git - bison.git/blob - src/closure.c
1 /* Subroutines for bison
2 Copyright (C) 1984, 1989, 2000 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 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)
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.
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
22 /* Subroutines of file LR0.c.
28 Given a vector of item numbers items, of length n, set up ruleset and
29 itemset to indicate what rules could be run and which items could be
30 accepted when those items are the active ones.
32 ruleset contains a bit for each rule. closure sets the bits for all
33 rules which could potentially describe the next input to be read.
35 itemset is a vector of item numbers; itemsetend points to just beyond
36 the end of the part of it that is significant. closure places there
37 the indices of all items which represent units of input that could
40 initialize_closure (n)
42 Allocates the itemset and ruleset vectors, and precomputes useful data
43 so that closure can be called. n is the number of elements to
48 Frees itemset, ruleset and internal data.
57 extern short **derives
;
60 extern void initialize_closure
PARAMS((int));
61 extern void closure
PARAMS((short *, int));
62 extern void finalize_closure
PARAMS((void));
64 static void set_fderives
PARAMS((void));
65 static void set_firsts
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
;
84 static void print_closure
PARAMS((int));
85 static void print_fderives
PARAMS((void));
86 static void print_firsts
PARAMS((void));
90 initialize_closure (int n
)
92 itemset
= NEW2(n
, short);
94 rulesetsize
= WORDSIZE(nrules
+ 1);
95 ruleset
= NEW2(rulesetsize
, unsigned);
102 /* set fderives to an nvars by nrules matrix of bits
103 indicating which rules can help derive the beginning of the data
104 for each nonterminal. For example, if symbol 5 can be derived as
105 the sequence of symbols 8 3 20, and one of the rules for deriving
106 symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
110 register unsigned *rrow
;
111 register unsigned *vrow
;
113 register unsigned cword
;
120 fderives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
124 rrow
= fderives
+ ntokens
* rulesetsize
;
126 for (i
= ntokens
; i
< nsyms
; i
++)
128 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
131 for (j
= ntokens
; j
< nsyms
; j
++)
133 if (cword
& (1 << b
))
136 while ((ruleno
= *rp
++) > 0)
138 SETBIT(rrow
, ruleno
);
143 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
162 /* set firsts to be an nvars by nvars bit matrix indicating which
163 items can represent the beginning of the input corresponding to
166 For example, if some rule expands symbol 5 into the sequence of
167 symbols 8 3 20, the symbol 8 can be the beginning of the data for
168 symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is
174 register unsigned *row
;
175 /* register int done; JF unused */
178 register int rowsize
;
182 varsetsize
= rowsize
= WORDSIZE(nvars
);
184 firsts
= NEW2(nvars
* rowsize
, unsigned);
187 for (i
= ntokens
; i
< nsyms
; i
++)
192 symbol
= ritem
[rrhs
[*sp
++]];
212 closure (short *core
, int n
)
215 register unsigned word
;
217 register unsigned *dsp
;
218 register unsigned *rsp
;
226 rsend
= ruleset
+ rulesetsize
;
231 dsp
= fderives
+ start_symbol
* rulesetsize
;
243 symbol
= ritem
[*csp
++];
246 dsp
= fderives
+ symbol
* rulesetsize
;
255 itemsetend
= itemset
;
263 ruleno
+= BITS_PER_WORD
;
269 for (b
= 0; b
< BITS_PER_WORD
; b
++)
273 itemno
= rrhs
[ruleno
];
274 while (csp
< csend
&& *csp
< itemno
)
275 *itemsetend
++ = *csp
++;
276 *itemsetend
++ = itemno
;
285 *itemsetend
++ = *csp
++;
294 finalize_closure (void)
298 FREE(fderives
+ ntokens
* rulesetsize
);
310 printf ("\n\nn = %d\n\n", n
);
311 for (isp
= itemset
; isp
< itemsetend
; isp
++)
312 printf (" %d\n", *isp
);
321 register unsigned *rowp
;
323 printf ("\n\n\nFIRSTS\n\n");
325 for (i
= ntokens
; i
< nsyms
; i
++)
327 printf ("\n\n%s firsts\n\n", tags
[i
]);
329 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
331 for (j
= 0; j
< nvars
; j
++)
332 if (BITISSET (rowp
, j
))
333 printf (" %s\n", tags
[j
+ ntokens
]);
339 print_fderives (void)
343 register unsigned *rp
;
345 printf ("\n\n\nFDERIVES\n");
347 for (i
= ntokens
; i
< nsyms
; i
++)
349 printf ("\n\n%s derives\n\n", tags
[i
]);
350 rp
= fderives
+ i
* rulesetsize
;
352 for (j
= 0; j
<= nrules
; j
++)
353 if (BITISSET (rp
, j
))