]>
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
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");
69 fprintf (stderr
, "FIRSTS\n");
70 for (i
= ntokens
; i
< nsyms
; i
++)
72 fprintf (stderr
, "\t%s firsts\n", tags
[i
]);
73 for (j
= 0; j
< nvars
; j
++)
74 if (BITISSET (FIRSTS (i
- ntokens
), j
))
75 fprintf (stderr
, "\t\t%d (%s)\n", j
+ ntokens
, tags
[j
+ ntokens
]);
77 fprintf (stderr
, "\n\n");
87 fprintf (stderr
, "FDERIVES\n");
89 for (i
= ntokens
; i
< nsyms
; i
++)
91 fprintf (stderr
, "\t%s derives\n", tags
[i
]);
92 for (j
= 0; j
<= nrules
; j
++)
93 if (BITISSET (FDERIVES (i
), j
))
96 fprintf (stderr
, "\t\t%d:", j
);
97 for (rhsp
= ritem
+ rule_table
[j
].rhs
; *rhsp
> 0; ++rhsp
)
98 fprintf (stderr
, " %s", tags
[*rhsp
]);
102 fprintf (stderr
, "\n\n");
105 /*-------------------------------------------------------------------.
106 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
107 | items can represent the beginning of the input corresponding to |
108 | which other items. |
110 | For example, if some rule expands symbol 5 into the sequence of |
111 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
112 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
113 `-------------------------------------------------------------------*/
122 varsetsize
= rowsize
= WORDSIZE (nvars
);
124 firsts
= XCALLOC (unsigned, nvars
* rowsize
);
126 for (i
= ntokens
; i
< nsyms
; i
++)
127 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
129 int symbol
= ritem
[rule_table
[derives
[i
][j
]].rhs
];
131 SETBIT (FIRSTS (i
- ntokens
), symbol
- ntokens
);
140 /*-------------------------------------------------------------------.
141 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
142 | rules can help derive the beginning of the data for each |
145 | For example, if symbol 5 can be derived as the sequence of symbols |
146 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
147 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
148 `-------------------------------------------------------------------*/
155 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
) - ntokens
* rulesetsize
;
159 for (i
= ntokens
; i
< nsyms
; ++i
)
160 for (j
= ntokens
; j
< nsyms
; ++j
)
161 if (BITISSET (FIRSTS (i
- ntokens
), j
- ntokens
))
162 for (k
= 0; derives
[j
][k
] > 0; ++k
)
163 SETBIT (FDERIVES (i
), derives
[j
][k
]);
175 itemset
= XCALLOC (short, n
);
177 rulesetsize
= WORDSIZE (nrules
+ 1);
178 ruleset
= XCALLOC (unsigned, rulesetsize
);
186 closure (short *core
, int n
)
188 /* Index over CORE. */
191 /* Index over RULESET. */
194 /* A bit index over RULESET. */
199 fprintf (stderr
, "Entering closure (items = {");
200 for (c
= 0; c
< n
; ++c
)
201 fprintf (stderr
, " %d ", core
[c
]);
202 fprintf (stderr
, "})\n");
207 for (r
= 0; r
< rulesetsize
; ++r
)
208 ruleset
[r
] = FDERIVES (start_symbol
)[r
];
212 for (r
= 0; r
< rulesetsize
; ++r
)
215 for (c
= 0; c
< n
; ++c
)
216 if (ISVAR (ritem
[core
[c
]]))
217 for (r
= 0; r
< rulesetsize
; ++r
)
218 ruleset
[r
] |= FDERIVES (ritem
[core
[c
]])[r
];
223 for (ruleno
= 0; ruleno
< rulesetsize
* BITS_PER_WORD
; ++ruleno
)
224 if (BITISSET (ruleset
, ruleno
))
226 int itemno
= rule_table
[ruleno
].rhs
;
227 while (c
< n
&& core
[c
] < itemno
)
229 itemset
[itemsetsize
] = core
[c
];
233 itemset
[itemsetsize
] = itemno
;
239 itemset
[itemsetsize
] = core
[c
];
254 XFREE (fderives
+ ntokens
* rulesetsize
);