]>
git.saurik.com Git - bison.git/blob - src/closure.c
bb45ba026cae20fe99ed42852bc383c00f28555b
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 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
40 #define FDERIVES(Var) (fderives + ((Var) - ntokens) * rulesetsize)
41 #define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
43 /* number of words required to hold a bit for each rule */
44 static int rulesetsize
;
46 /* number of words required to hold a bit for each variable */
47 static int varsetsize
;
58 fprintf (stderr
, "n = %d\n", n
);
59 for (i
= 0; i
< itemsetsize
; ++i
)
60 fprintf (stderr
, " %d\n", itemset
[i
]);
61 fprintf (stderr
, "\n\n");
70 fprintf (stderr
, "FIRSTS\n");
71 for (i
= ntokens
; i
< nsyms
; i
++)
73 fprintf (stderr
, "\t%s firsts\n", tags
[i
]);
74 for (j
= 0; j
< nvars
; j
++)
75 if (BITISSET (FIRSTS (i
), j
))
76 fprintf (stderr
, "\t\t%d (%s)\n", j
+ ntokens
, tags
[j
+ ntokens
]);
78 fprintf (stderr
, "\n\n");
88 fprintf (stderr
, "FDERIVES\n");
90 for (i
= ntokens
; i
< nsyms
; i
++)
92 fprintf (stderr
, "\t%s derives\n", tags
[i
]);
93 for (j
= 0; j
<= nrules
; j
++)
94 if (BITISSET (FDERIVES (i
), j
))
97 fprintf (stderr
, "\t\t%d:", j
);
98 for (rhsp
= ritem
+ rule_table
[j
].rhs
; *rhsp
> 0; ++rhsp
)
99 fprintf (stderr
, " %s", tags
[*rhsp
]);
100 fputc ('\n', stderr
);
103 fprintf (stderr
, "\n\n");
106 /*-------------------------------------------------------------------.
107 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
108 | items can represent the beginning of the input corresponding to |
109 | which other items. |
111 | For example, if some rule expands symbol 5 into the sequence of |
112 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
113 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
114 `-------------------------------------------------------------------*/
121 varsetsize
= WORDSIZE (nvars
);
123 firsts
= XCALLOC (unsigned, nvars
* varsetsize
);
125 for (i
= ntokens
; i
< nsyms
; i
++)
126 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
128 int symbol
= ritem
[rule_table
[derives
[i
][j
]].rhs
];
130 SETBIT (FIRSTS (i
), symbol
- ntokens
);
139 /*-------------------------------------------------------------------.
140 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
141 | rules can help derive the beginning of the data for each |
144 | For example, if symbol 5 can be derived as the sequence of symbols |
145 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
146 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
147 `-------------------------------------------------------------------*/
154 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
);
158 for (i
= ntokens
; i
< nsyms
; ++i
)
159 for (j
= ntokens
; j
< nsyms
; ++j
)
160 if (BITISSET (FIRSTS (i
), j
- ntokens
))
161 for (k
= 0; derives
[j
][k
] > 0; ++k
)
162 SETBIT (FDERIVES (i
), derives
[j
][k
]);
174 itemset
= XCALLOC (short, n
);
176 rulesetsize
= WORDSIZE (nrules
+ 1);
177 ruleset
= XCALLOC (unsigned, rulesetsize
);
185 closure (short *core
, int n
)
187 /* Index over CORE. */
190 /* Index over RULESET. */
193 /* A bit index over RULESET. */
198 fprintf (stderr
, "Entering closure (items = {");
199 for (c
= 0; c
< n
; ++c
)
200 fprintf (stderr
, " %d ", core
[c
]);
201 fprintf (stderr
, "})\n");
206 for (r
= 0; r
< rulesetsize
; ++r
)
207 ruleset
[r
] = FDERIVES (start_symbol
)[r
];
211 for (r
= 0; r
< rulesetsize
; ++r
)
214 for (c
= 0; c
< n
; ++c
)
215 if (ISVAR (ritem
[core
[c
]]))
216 for (r
= 0; r
< rulesetsize
; ++r
)
217 ruleset
[r
] |= FDERIVES (ritem
[core
[c
]])[r
];
222 for (ruleno
= 0; ruleno
< rulesetsize
* BITS_PER_WORD
; ++ruleno
)
223 if (BITISSET (ruleset
, ruleno
))
225 int itemno
= rule_table
[ruleno
].rhs
;
226 while (c
< n
&& core
[c
] < itemno
)
228 itemset
[itemsetsize
] = core
[c
];
232 itemset
[itemsetsize
] = itemno
;
238 itemset
[itemsetsize
] = core
[c
];