]>
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
30 static unsigned *ruleset
;
32 /* internal data. See comments before set_fderives and set_firsts. */
33 static unsigned *fderives
;
34 static unsigned *firsts
;
36 /* number of words required to hold a bit for each rule */
37 static int rulesetsize
;
39 /* number of words required to hold a bit for each variable */
40 static int varsetsize
;
53 printf ("\n\nn = %d\n\n", n
);
54 for (isp
= itemset
; isp
< itemsetend
; isp
++)
55 printf (" %d\n", *isp
);
66 printf ("\n\n\nFIRSTS\n\n");
68 for (i
= ntokens
; i
< nsyms
; i
++)
70 printf ("\n\n%s firsts\n\n", tags
[i
]);
72 rowp
= firsts
+ ((i
- ntokens
) * varsetsize
);
74 for (j
= 0; j
< nvars
; j
++)
75 if (BITISSET (rowp
, j
))
76 printf (" %s\n", tags
[j
+ ntokens
]);
88 printf ("\n\n\nFDERIVES\n");
90 for (i
= ntokens
; i
< nsyms
; i
++)
92 printf ("\n\n%s derives\n\n", tags
[i
]);
93 rp
= fderives
+ i
* rulesetsize
;
95 for (j
= 0; j
<= nrules
; j
++)
104 /*-------------------------------------------------------------------.
105 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
106 | items can represent the beginning of the input corresponding to |
107 | which other items. |
109 | For example, if some rule expands symbol 5 into the sequence of |
110 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
111 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
112 `-------------------------------------------------------------------*/
124 varsetsize
= rowsize
= WORDSIZE (nvars
);
126 firsts
= XCALLOC (unsigned, nvars
* rowsize
);
129 for (i
= ntokens
; i
< nsyms
; i
++)
134 symbol
= ritem
[rrhs
[*sp
++]];
138 SETBIT (row
, symbol
);
152 /*-------------------------------------------------------------------.
153 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
154 | rules can help derive the beginning of the data for each |
157 | For example, if symbol 5 can be derived as the sequence of symbols |
158 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
159 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
160 `-------------------------------------------------------------------*/
175 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
) - ntokens
* rulesetsize
;
179 rrow
= fderives
+ ntokens
* rulesetsize
;
181 for (i
= ntokens
; i
< nsyms
; i
++)
183 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
186 for (j
= ntokens
; j
< nsyms
; j
++)
188 if (cword
& (1 << b
))
191 while ((ruleno
= *rp
++) > 0)
193 SETBIT (rrow
, ruleno
);
198 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
219 itemset
= XCALLOC (short, n
);
221 rulesetsize
= WORDSIZE (nrules
+ 1);
222 ruleset
= XCALLOC (unsigned, rulesetsize
);
230 closure (short *core
, int n
)
244 rsend
= ruleset
+ rulesetsize
;
249 dsp
= fderives
+ start_symbol
* rulesetsize
;
261 symbol
= ritem
[*csp
++];
264 dsp
= fderives
+ symbol
* rulesetsize
;
273 itemsetend
= itemset
;
281 ruleno
+= BITS_PER_WORD
;
287 for (b
= 0; b
< BITS_PER_WORD
; b
++)
291 itemno
= rrhs
[ruleno
];
292 while (csp
< csend
&& *csp
< itemno
)
293 *itemsetend
++ = *csp
++;
294 *itemsetend
++ = itemno
;
303 *itemsetend
++ = *csp
++;
316 XFREE (fderives
+ ntokens
* rulesetsize
);