]>
git.saurik.com Git - bison.git/blob - src/closure.c
b06d808fa27dd184d57f22a121e4be053eeab0d6
1 /* Subroutines for bison
2 Copyright 1984, 1989, 2000, 2001 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
31 static unsigned *ruleset
;
33 /* internal data. See comments before set_fderives and set_firsts. */
34 static unsigned *fderives
;
35 static unsigned *firsts
;
37 /* number of words required to hold a bit for each rule */
38 static int rulesetsize
;
40 /* number of words required to hold a bit for each variable */
41 static int varsetsize
;
53 fprintf (stderr
, "\n\nn = %d\n\n", n
);
54 for (isp
= itemset
; isp
< itemsetend
; isp
++)
55 fprintf (stderr
, " %d\n", *isp
);
66 fprintf (stderr
, "\n\n\nFIRSTS\n\n");
68 for (i
= ntokens
; i
< nsyms
; i
++)
70 fprintf (stderr
, "\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 fprintf (stderr
, " %s\n", tags
[j
+ ntokens
]);
88 fprintf (stderr
, "\n\n\nFDERIVES\n");
90 for (i
= ntokens
; i
< nsyms
; i
++)
92 fprintf (stderr
, "\n\n%s derives\n\n", tags
[i
]);
93 rp
= fderives
+ i
* rulesetsize
;
95 for (j
= 0; j
<= nrules
; j
++)
97 fprintf (stderr
, " %d\n", j
);
101 /*-------------------------------------------------------------------.
102 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
103 | items can represent the beginning of the input corresponding to |
104 | which other items. |
106 | For example, if some rule expands symbol 5 into the sequence of |
107 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
108 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
109 `-------------------------------------------------------------------*/
121 varsetsize
= rowsize
= WORDSIZE (nvars
);
123 firsts
= XCALLOC (unsigned, nvars
* rowsize
);
126 for (i
= ntokens
; i
< nsyms
; i
++)
131 symbol
= ritem
[rule_table
[*sp
++].rhs
];
135 SETBIT (row
, symbol
);
148 /*-------------------------------------------------------------------.
149 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
150 | rules can help derive the beginning of the data for each |
153 | For example, if symbol 5 can be derived as the sequence of symbols |
154 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
155 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
156 `-------------------------------------------------------------------*/
171 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
) - ntokens
* rulesetsize
;
175 rrow
= fderives
+ ntokens
* rulesetsize
;
177 for (i
= ntokens
; i
< nsyms
; i
++)
179 vrow
= firsts
+ ((i
- ntokens
) * varsetsize
);
182 for (j
= ntokens
; j
< nsyms
; j
++)
184 if (cword
& (1 << b
))
187 while ((ruleno
= *rp
++) > 0)
189 SETBIT (rrow
, ruleno
);
194 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
214 itemset
= XCALLOC (short, n
);
216 rulesetsize
= WORDSIZE (nrules
+ 1);
217 ruleset
= XCALLOC (unsigned, rulesetsize
);
225 closure (short *core
, int n
)
239 rsend
= ruleset
+ rulesetsize
;
244 dsp
= fderives
+ start_symbol
* rulesetsize
;
256 symbol
= ritem
[*csp
++];
259 dsp
= fderives
+ symbol
* rulesetsize
;
268 itemsetend
= itemset
;
276 ruleno
+= BITS_PER_WORD
;
282 for (b
= 0; b
< BITS_PER_WORD
; b
++)
286 itemno
= rule_table
[ruleno
].rhs
;
287 while (csp
< csend
&& *csp
< itemno
)
288 *itemsetend
++ = *csp
++;
289 *itemsetend
++ = itemno
;
298 *itemsetend
++ = *csp
++;
310 XFREE (fderives
+ ntokens
* rulesetsize
);