]>
git.saurik.com Git - bison.git/blob - src/closure.c
ac37b2c018021e162b9520e5d84670f585618bed
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
29 /* ITEMSETSIZE is the size of the array ITEMSET. */
33 static unsigned *ruleset
;
35 /* internal data. See comments before set_fderives and set_firsts. */
36 static unsigned *fderives
;
37 static unsigned *firsts
;
39 #define FDERIVES(Symbol) (fderives + (Symbol) * rulesetsize)
40 #define FIRSTS(Symbol) (firsts + (Symbol) * varsetsize)
42 /* number of words required to hold a bit for each rule */
43 static int rulesetsize
;
45 /* number of words required to hold a bit for each variable */
46 static int varsetsize
;
57 fprintf (stderr
, "n = %d\n", n
);
58 for (i
= 0; i
< itemsetsize
; ++i
)
59 fprintf (stderr
, " %d\n", itemset
[i
]);
60 fprintf (stderr
, "\n\n");
71 fprintf (stderr
, "FIRSTS\n");
73 for (i
= ntokens
; i
< nsyms
; i
++)
75 fprintf (stderr
, "\t%s firsts\n", tags
[i
]);
77 rowp
= FIRSTS (i
- ntokens
);
79 for (j
= 0; j
< nvars
; j
++)
80 if (BITISSET (rowp
, j
))
81 fprintf (stderr
, "\t\t%d (%s)\n", j
+ ntokens
, tags
[j
+ ntokens
]);
83 fprintf (stderr
, "\n\n");
94 fprintf (stderr
, "FDERIVES\n");
96 for (i
= ntokens
; i
< nsyms
; i
++)
98 fprintf (stderr
, "\t%s derives\n", tags
[i
]);
101 for (j
= 0; j
<= nrules
; j
++)
102 if (BITISSET (rp
, j
))
103 fprintf (stderr
, "\t\t%d (%s)\n", j
, tags
[j
]);
105 fprintf (stderr
, "\n\n");
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
= XCALLOC (unsigned, nvars
* rowsize
);
133 for (i
= ntokens
; i
< nsyms
; i
++)
138 symbol
= ritem
[rule_table
[*sp
++].rhs
];
142 SETBIT (row
, symbol
);
155 /*-------------------------------------------------------------------.
156 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
157 | rules can help derive the beginning of the data for each |
160 | For example, if symbol 5 can be derived as the sequence of symbols |
161 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
162 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
163 `-------------------------------------------------------------------*/
178 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
) - ntokens
* rulesetsize
;
182 rrow
= FDERIVES (ntokens
);
184 for (i
= ntokens
; i
< nsyms
; i
++)
186 vrow
= FIRSTS (i
- ntokens
);
189 for (j
= ntokens
; j
< nsyms
; j
++)
191 if (cword
& (1 << b
))
194 while ((ruleno
= *rp
++) > 0)
195 SETBIT (rrow
, ruleno
);
199 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
)
240 fprintf (stderr
, "Entering closure (items = {");
241 for (i
= 0; i
< n
; ++i
)
242 fprintf (stderr
, " %d ", core
[i
]);
243 fprintf (stderr
, "}, nitems = %d)\n", n
);
248 for (i
= 0; i
< rulesetsize
; ++i
)
249 ruleset
[i
] = FDERIVES (start_symbol
)[i
];
253 for (i
= 0; i
< rulesetsize
; ++i
)
256 for (i
= 0; i
< n
; ++i
)
258 int symbol
= ritem
[core
[i
]];
262 for (j
= 0; j
< rulesetsize
; ++j
)
263 ruleset
[j
] |= FDERIVES (symbol
)[j
];
271 for (i
= 0; i
< rulesetsize
; ++i
)
273 int word
= ruleset
[i
];
276 ruleno
+= BITS_PER_WORD
;
282 for (b
= 0; b
< BITS_PER_WORD
; b
++)
286 itemno
= rule_table
[ruleno
].rhs
;
287 while (csp
< (core
+ n
) && *csp
< itemno
)
288 itemset
[itemsetsize
++] = *csp
++;
289 itemset
[itemsetsize
++] = itemno
;
297 while (csp
< (core
+ n
))
298 itemset
[itemsetsize
++] = *csp
++;
310 XFREE (fderives
+ ntokens
* rulesetsize
);