]>
git.saurik.com Git - bison.git/blob - src/closure.c
68b646909f8801560b28759700ac9ccb2e934138
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
29 static unsigned *ruleset
;
31 /* internal data. See comments before set_fderives and set_firsts. */
32 static unsigned *fderives
;
33 static unsigned *firsts
;
35 /* number of words required to hold a bit for each rule */
36 static int rulesetsize
;
38 /* number of words required to hold a bit for each variable */
39 static int varsetsize
;
52 printf ("\n\nn = %d\n\n", n
);
53 for (isp
= itemset
; isp
< itemsetend
; isp
++)
54 printf (" %d\n", *isp
);
65 printf ("\n\n\nFIRSTS\n\n");
67 for (i
= ntokens
; i
< nsyms
; i
++)
69 printf ("\n\n%s firsts\n\n", tags
[i
]);
71 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
73 for (j
= 0; j
< nvars
; j
++)
74 if (BITISSET (rowp
, j
))
75 printf (" %s\n", tags
[j
+ ntokens
]);
87 printf ("\n\n\nFDERIVES\n");
89 for (i
= ntokens
; i
< nsyms
; i
++)
91 printf ("\n\n%s derives\n\n", tags
[i
]);
92 rp
= fderives
+ i
* rulesetsize
;
94 for (j
= 0; j
<= nrules
; j
++)
103 /*-------------------------------------------------------------------.
104 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
105 | items can represent the beginning of the input corresponding to |
106 | which other items. |
108 | For example, if some rule expands symbol 5 into the sequence of |
109 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
110 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
111 `-------------------------------------------------------------------*/
123 varsetsize
= rowsize
= WORDSIZE (nvars
);
125 firsts
= NEW2 (nvars
* rowsize
, unsigned);
128 for (i
= ntokens
; i
< nsyms
; i
++)
133 symbol
= ritem
[rrhs
[*sp
++]];
137 SETBIT (row
, symbol
);
151 /*-------------------------------------------------------------------.
152 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
153 | rules can help derive the beginning of the data for each |
156 | For example, if symbol 5 can be derived as the sequence of symbols |
157 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
158 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
159 `-------------------------------------------------------------------*/
174 fderives
= NEW2 (nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
178 rrow
= fderives
+ ntokens
* rulesetsize
;
180 for (i
= ntokens
; i
< nsyms
; i
++)
182 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
185 for (j
= ntokens
; j
< nsyms
; j
++)
187 if (cword
& (1 << b
))
190 while ((ruleno
= *rp
++) > 0)
192 SETBIT (rrow
, ruleno
);
197 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
218 itemset
= NEW2 (n
, short);
220 rulesetsize
= WORDSIZE (nrules
+ 1);
221 ruleset
= NEW2 (rulesetsize
, unsigned);
229 closure (short *core
, int n
)
243 rsend
= ruleset
+ rulesetsize
;
248 dsp
= fderives
+ start_symbol
* rulesetsize
;
260 symbol
= ritem
[*csp
++];
263 dsp
= fderives
+ symbol
* rulesetsize
;
272 itemsetend
= itemset
;
280 ruleno
+= BITS_PER_WORD
;
286 for (b
= 0; b
< BITS_PER_WORD
; b
++)
290 itemno
= rrhs
[ruleno
];
291 while (csp
< csend
&& *csp
< itemno
)
292 *itemsetend
++ = *csp
++;
293 *itemsetend
++ = itemno
;
302 *itemsetend
++ = *csp
++;
315 FREE (fderives
+ ntokens
* rulesetsize
);