]>
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.
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
159 items can represent the beginning of the input corresponding to
162 For example, if some rule expands symbol 5 into the sequence of
163 symbols 8 3 20, the symbol 8 can be the beginning of the data for
164 symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is
170 register unsigned *row
;
171 /* register int done; JF unused */
174 register int rowsize
;
178 varsetsize
= rowsize
= WORDSIZE(nvars
);
180 firsts
= NEW2(nvars
* rowsize
, unsigned);
183 for (i
= ntokens
; i
< nsyms
; i
++)
188 symbol
= ritem
[rrhs
[*sp
++]];
208 closure (short *core
, int n
)
211 register unsigned word
;
213 register unsigned *dsp
;
214 register unsigned *rsp
;
222 rsend
= ruleset
+ rulesetsize
;
227 dsp
= fderives
+ start_symbol
* rulesetsize
;
239 symbol
= ritem
[*csp
++];
242 dsp
= fderives
+ symbol
* rulesetsize
;
251 itemsetend
= itemset
;
259 ruleno
+= BITS_PER_WORD
;
265 for (b
= 0; b
< BITS_PER_WORD
; b
++)
269 itemno
= rrhs
[ruleno
];
270 while (csp
< csend
&& *csp
< itemno
)
271 *itemsetend
++ = *csp
++;
272 *itemsetend
++ = itemno
;
281 *itemsetend
++ = *csp
++;
290 finalize_closure (void)
294 FREE(fderives
+ ntokens
* rulesetsize
);
306 printf ("\n\nn = %d\n\n", n
);
307 for (isp
= itemset
; isp
< itemsetend
; isp
++)
308 printf (" %d\n", *isp
);
317 register unsigned *rowp
;
319 printf ("\n\n\nFIRSTS\n\n");
321 for (i
= ntokens
; i
< nsyms
; i
++)
323 printf ("\n\n%s firsts\n\n", tags
[i
]);
325 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
327 for (j
= 0; j
< nvars
; j
++)
328 if (BITISSET (rowp
, j
))
329 printf (" %s\n", tags
[j
+ ntokens
]);
335 print_fderives (void)
339 register unsigned *rp
;
341 printf ("\n\n\nFDERIVES\n");
343 for (i
= ntokens
; i
< nsyms
; i
++)
345 printf ("\n\n%s derives\n\n", tags
[i
]);
346 rp
= fderives
+ i
* rulesetsize
;
348 for (j
= 0; j
<= nrules
; j
++)
349 if (BITISSET (rp
, j
))