]>
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
27 extern short **derives
;
30 extern void RTC
PARAMS ((unsigned *, int));
34 static unsigned *ruleset
;
36 /* internal data. See comments before set_fderives and set_firsts. */
37 static unsigned *fderives
;
38 static unsigned *firsts
;
40 /* number of words required to hold a bit for each rule */
41 static int rulesetsize
;
43 /* number of words required to hold a bit for each variable */
44 static int varsetsize
;
57 printf ("\n\nn = %d\n\n", n
);
58 for (isp
= itemset
; isp
< itemsetend
; isp
++)
59 printf (" %d\n", *isp
);
70 printf ("\n\n\nFIRSTS\n\n");
72 for (i
= ntokens
; i
< nsyms
; i
++)
74 printf ("\n\n%s firsts\n\n", tags
[i
]);
76 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
78 for (j
= 0; j
< nvars
; j
++)
79 if (BITISSET (rowp
, j
))
80 printf (" %s\n", tags
[j
+ ntokens
]);
92 printf ("\n\n\nFDERIVES\n");
94 for (i
= ntokens
; i
< nsyms
; i
++)
96 printf ("\n\n%s derives\n\n", tags
[i
]);
97 rp
= fderives
+ i
* rulesetsize
;
99 for (j
= 0; j
<= nrules
; j
++)
100 if (BITISSET (rp
, j
))
108 /*-------------------------------------------------------------------.
109 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
110 | items can represent the beginning of the input corresponding to |
111 | which other items. |
113 | For example, if some rule expands symbol 5 into the sequence of |
114 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
115 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
116 `-------------------------------------------------------------------*/
128 varsetsize
= rowsize
= WORDSIZE (nvars
);
130 firsts
= NEW2 (nvars
* rowsize
, unsigned);
133 for (i
= ntokens
; i
< nsyms
; i
++)
138 symbol
= ritem
[rrhs
[*sp
++]];
142 SETBIT (row
, symbol
);
156 /*-------------------------------------------------------------------.
157 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
158 | rules can help derive the beginning of the data for each |
161 | For example, if symbol 5 can be derived as the sequence of symbols |
162 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
163 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
164 `-------------------------------------------------------------------*/
179 fderives
= NEW2 (nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
183 rrow
= fderives
+ ntokens
* rulesetsize
;
185 for (i
= ntokens
; i
< nsyms
; i
++)
187 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
190 for (j
= ntokens
; j
< nsyms
; j
++)
192 if (cword
& (1 << b
))
195 while ((ruleno
= *rp
++) > 0)
197 SETBIT (rrow
, ruleno
);
202 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
223 itemset
= NEW2 (n
, short);
225 rulesetsize
= WORDSIZE (nrules
+ 1);
226 ruleset
= NEW2 (rulesetsize
, unsigned);
234 closure (short *core
, int n
)
248 rsend
= ruleset
+ rulesetsize
;
253 dsp
= fderives
+ start_symbol
* rulesetsize
;
265 symbol
= ritem
[*csp
++];
268 dsp
= fderives
+ symbol
* rulesetsize
;
277 itemsetend
= itemset
;
285 ruleno
+= BITS_PER_WORD
;
291 for (b
= 0; b
< BITS_PER_WORD
; b
++)
295 itemno
= rrhs
[ruleno
];
296 while (csp
< csend
&& *csp
< itemno
)
297 *itemsetend
++ = *csp
++;
298 *itemsetend
++ = itemno
;
307 *itemsetend
++ = *csp
++;
320 FREE (fderives
+ ntokens
* rulesetsize
);