]>
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 extern void initialize_closure
PARAMS((int));
63 extern void closure
PARAMS((short *, int));
64 extern void finalize_closure
PARAMS((void));
66 static void set_fderives
PARAMS((void));
67 static void set_firsts
PARAMS((void));
69 extern void RTC
PARAMS((unsigned *, int));
73 static unsigned *ruleset
;
75 /* internal data. See comments before set_fderives and set_firsts. */
76 static unsigned *fderives
;
77 static unsigned *firsts
;
79 /* number of words required to hold a bit for each rule */
80 static int rulesetsize
;
82 /* number of words required to hold a bit for each variable */
83 static int varsetsize
;
86 static void print_closure
PARAMS((int));
87 static void print_fderives
PARAMS((void));
88 static void print_firsts
PARAMS((void));
92 initialize_closure (int n
)
94 itemset
= NEW2(n
, short);
96 rulesetsize
= WORDSIZE(nrules
+ 1);
97 ruleset
= NEW2(rulesetsize
, unsigned);
104 /* set fderives to an nvars by nrules matrix of bits
105 indicating which rules can help derive the beginning of the data
106 for each nonterminal. For example, if symbol 5 can be derived as
107 the sequence of symbols 8 3 20, and one of the rules for deriving
108 symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
112 register unsigned *rrow
;
113 register unsigned *vrow
;
115 register unsigned cword
;
122 fderives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
126 rrow
= fderives
+ ntokens
* rulesetsize
;
128 for (i
= ntokens
; i
< nsyms
; i
++)
130 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
133 for (j
= ntokens
; j
< nsyms
; j
++)
135 if (cword
& (1 << b
))
138 while ((ruleno
= *rp
++) > 0)
140 SETBIT(rrow
, ruleno
);
145 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
164 /* set firsts to be an nvars by nvars bit matrix indicating which
165 items can represent the beginning of the input corresponding to
168 For example, if some rule expands symbol 5 into the sequence of
169 symbols 8 3 20, the symbol 8 can be the beginning of the data for
170 symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is
176 register unsigned *row
;
177 /* register int done; JF unused */
180 register int rowsize
;
184 varsetsize
= rowsize
= WORDSIZE(nvars
);
186 firsts
= NEW2(nvars
* rowsize
, unsigned);
189 for (i
= ntokens
; i
< nsyms
; i
++)
194 symbol
= ritem
[rrhs
[*sp
++]];
214 closure (short *core
, int n
)
217 register unsigned word
;
219 register unsigned *dsp
;
220 register unsigned *rsp
;
228 rsend
= ruleset
+ rulesetsize
;
233 dsp
= fderives
+ start_symbol
* rulesetsize
;
245 symbol
= ritem
[*csp
++];
248 dsp
= fderives
+ symbol
* rulesetsize
;
257 itemsetend
= itemset
;
265 ruleno
+= BITS_PER_WORD
;
271 for (b
= 0; b
< BITS_PER_WORD
; b
++)
275 itemno
= rrhs
[ruleno
];
276 while (csp
< csend
&& *csp
< itemno
)
277 *itemsetend
++ = *csp
++;
278 *itemsetend
++ = itemno
;
287 *itemsetend
++ = *csp
++;
296 finalize_closure (void)
300 FREE(fderives
+ ntokens
* rulesetsize
);
312 printf ("\n\nn = %d\n\n", n
);
313 for (isp
= itemset
; isp
< itemsetend
; isp
++)
314 printf (" %d\n", *isp
);
323 register unsigned *rowp
;
325 printf ("\n\n\nFIRSTS\n\n");
327 for (i
= ntokens
; i
< nsyms
; i
++)
329 printf ("\n\n%s firsts\n\n", tags
[i
]);
331 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
333 for (j
= 0; j
< nvars
; j
++)
334 if (BITISSET (rowp
, j
))
335 printf (" %s\n", tags
[j
+ ntokens
]);
341 print_fderives (void)
345 register unsigned *rp
;
347 printf ("\n\n\nFDERIVES\n");
349 for (i
= ntokens
; i
< nsyms
; i
++)
351 printf ("\n\n%s derives\n\n", tags
[i
]);
352 rp
= fderives
+ i
* rulesetsize
;
354 for (j
= 0; j
<= nrules
; j
++)
355 if (BITISSET (rp
, j
))