]>
git.saurik.com Git - bison.git/blob - src/closure.c
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 #define FDERIVES(Symbol) (fderives + (Symbol) * rulesetsize)
38 #define FIRSTS(Symbol) (firsts + (Symbol) * varsetsize)
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
;
56 fprintf (stderr
, "n = %d\n", n
);
57 for (isp
= itemset
; isp
< itemsetend
; isp
++)
58 fprintf (stderr
, " %d\n", *isp
);
59 fprintf (stderr
, "\n\n");
70 fprintf (stderr
, "FIRSTS\n");
72 for (i
= ntokens
; i
< nsyms
; i
++)
74 fprintf (stderr
, "\t%s firsts\n", tags
[i
]);
76 rowp
= FIRSTS (i
- ntokens
);
78 for (j
= 0; j
< nvars
; j
++)
79 if (BITISSET (rowp
, j
))
80 fprintf (stderr
, "\t\t%d (%s)\n", j
+ ntokens
, tags
[j
+ ntokens
]);
82 fprintf (stderr
, "\n\n");
93 fprintf (stderr
, "FDERIVES\n");
95 for (i
= ntokens
; i
< nsyms
; i
++)
97 fprintf (stderr
, "\t%s derives\n", tags
[i
]);
100 for (j
= 0; j
<= nrules
; j
++)
101 if (BITISSET (rp
, j
))
102 fprintf (stderr
, "\t\t%d (%s)\n", j
, tags
[j
]);
104 fprintf (stderr
, "\n\n");
107 /*-------------------------------------------------------------------.
108 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
109 | items can represent the beginning of the input corresponding to |
110 | which other items. |
112 | For example, if some rule expands symbol 5 into the sequence of |
113 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
114 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
115 `-------------------------------------------------------------------*/
127 varsetsize
= rowsize
= WORDSIZE (nvars
);
129 firsts
= XCALLOC (unsigned, nvars
* rowsize
);
132 for (i
= ntokens
; i
< nsyms
; i
++)
137 symbol
= ritem
[rule_table
[*sp
++].rhs
];
141 SETBIT (row
, symbol
);
154 /*-------------------------------------------------------------------.
155 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
156 | rules can help derive the beginning of the data for each |
159 | For example, if symbol 5 can be derived as the sequence of symbols |
160 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
161 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
162 `-------------------------------------------------------------------*/
177 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
) - ntokens
* rulesetsize
;
181 rrow
= FDERIVES (ntokens
);
183 for (i
= ntokens
; i
< nsyms
; i
++)
185 vrow
= FIRSTS (i
- ntokens
);
188 for (j
= ntokens
; j
< nsyms
; j
++)
190 if (cword
& (1 << b
))
193 while ((ruleno
= *rp
++) > 0)
194 SETBIT (rrow
, ruleno
);
198 if (b
>= BITS_PER_WORD
&& j
+ 1 < nsyms
)
218 itemset
= XCALLOC (short, n
);
220 rulesetsize
= WORDSIZE (nrules
+ 1);
221 ruleset
= XCALLOC (unsigned, rulesetsize
);
229 closure (short *core
, int n
)
239 fprintf (stderr
, "Entering closure (items = {");
240 for (i
= 0; i
< n
; ++i
)
241 fprintf (stderr
, " %d ", core
[i
]);
242 fprintf (stderr
, "}, nitems = %d)\n", n
);
247 for (i
= 0; i
< rulesetsize
; ++i
)
248 ruleset
[i
] = FDERIVES (start_symbol
)[i
];
252 for (i
= 0; i
< rulesetsize
; ++i
)
255 for (i
= 0; i
< n
; ++i
)
257 int symbol
= ritem
[core
[i
]];
261 for (j
= 0; j
< rulesetsize
; ++j
)
262 ruleset
[j
] |= FDERIVES (symbol
)[j
];
268 itemsetend
= itemset
;
270 for (i
= 0; i
< rulesetsize
; ++i
)
272 int word
= ruleset
[i
];
275 ruleno
+= BITS_PER_WORD
;
281 for (b
= 0; b
< BITS_PER_WORD
; b
++)
285 itemno
= rule_table
[ruleno
].rhs
;
286 while (csp
< (core
+ n
) && *csp
< itemno
)
287 *itemsetend
++ = *csp
++;
288 *itemsetend
++ = itemno
;
296 while (csp
< (core
+ n
))
297 *itemsetend
++ = *csp
++;
309 XFREE (fderives
+ ntokens
* rulesetsize
);