]>
git.saurik.com Git - bison.git/blob - src/closure.c
a90b867047119b96c0855f3b0ed9c6ef0ebe11d5
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 /* NITEMSET is the size of the array ITEMSET. */
35 static unsigned *ruleset
;
37 /* internal data. See comments before set_fderives and set_firsts. */
38 static bitset
*fderives
;
39 static unsigned int *firsts
;
41 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
42 #define FDERIVES(Var) fderives[(Var) - ntokens]
43 #define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
45 /* number of words required to hold a bit for each rule */
46 static int rulesetsize
;
48 /* number of words required to hold a bit for each variable */
49 static int varsetsize
;
57 print_closure (const char *title
, short *array
, size_t size
)
60 fprintf (stderr
, "Closure: %s\n", title
);
61 for (i
= 0; i
< size
; ++i
)
64 fprintf (stderr
, " %2d: .", array
[i
]);
65 for (rp
= &ritem
[array
[i
]]; *rp
>= 0; ++rp
)
66 fprintf (stderr
, " %s", symbols
[*rp
]->tag
);
67 fprintf (stderr
, " (rule %d)\n", -*rp
- 1);
69 fputs ("\n\n", stderr
);
78 fprintf (stderr
, "FIRSTS\n");
79 for (i
= ntokens
; i
< nsyms
; i
++)
81 fprintf (stderr
, "\t%s firsts\n", symbols
[i
]->tag
);
82 for (j
= 0; j
< nvars
; j
++)
83 if (BITISSET (FIRSTS (i
), j
))
84 fprintf (stderr
, "\t\t%d (%s)\n",
85 j
+ ntokens
, symbols
[j
+ ntokens
]->tag
);
87 fprintf (stderr
, "\n\n");
97 fprintf (stderr
, "FDERIVES\n");
99 for (i
= ntokens
; i
< nsyms
; i
++)
101 fprintf (stderr
, "\t%s derives\n", symbols
[i
]->tag
);
102 for (j
= 0; j
<= nrules
; j
++)
103 if (bitset_test (FDERIVES (i
), j
))
106 fprintf (stderr
, "\t\t%d:", j
- 1);
107 for (rhsp
= &ritem
[rules
[j
].rhs
]; *rhsp
>= 0; ++rhsp
)
108 fprintf (stderr
, " %s", symbols
[*rhsp
]->tag
);
109 fputc ('\n', stderr
);
112 fprintf (stderr
, "\n\n");
115 /*-------------------------------------------------------------------.
116 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
117 | items can represent the beginning of the input corresponding to |
118 | which other items. |
120 | For example, if some rule expands symbol 5 into the sequence of |
121 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
122 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
123 `-------------------------------------------------------------------*/
130 varsetsize
= WORDSIZE (nvars
);
132 firsts
= XCALLOC (unsigned, nvars
* varsetsize
);
134 for (i
= ntokens
; i
< nsyms
; i
++)
135 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
137 int symbol
= ritem
[rules
[derives
[i
][j
]].rhs
];
139 SETBIT (FIRSTS (i
), symbol
- ntokens
);
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 `-------------------------------------------------------------------*/
163 fderives
= XCALLOC (bitset
, nvars
);
164 bitset_stats_init ();
165 for (i
= 0 ; i
< nvars
; ++i
)
167 fderives
[i
] = bitset_create (nrules
+ 1, BITSET_FIXED
);
168 bitset_zero (fderives
[i
]);
173 for (i
= ntokens
; i
< nsyms
; ++i
)
174 for (j
= ntokens
; j
< nsyms
; ++j
)
175 if (BITISSET (FIRSTS (i
), j
- ntokens
))
176 for (k
= 0; derives
[j
][k
] > 0; ++k
)
177 bitset_set (FDERIVES (i
), derives
[j
][k
]);
189 itemset
= XCALLOC (short, n
);
191 rulesetsize
= WORDSIZE (nrules
+ 1);
192 ruleset
= XCALLOC (unsigned, rulesetsize
);
200 closure (short *core
, int n
)
202 /* Index over CORE. */
205 /* An index over RULESET. */
208 /* A bit index over RULESET. */
212 print_closure ("input", core
, n
);
216 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
217 if (bitset_test (FDERIVES (start_symbol
), ruleno
))
218 SETBIT (ruleset
, ruleno
);
220 RESETBIT (ruleset
, ruleno
);
224 for (r
= 0; r
< rulesetsize
; ++r
)
227 for (c
= 0; c
< n
; ++c
)
228 if (ISVAR (ritem
[core
[c
]]))
229 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
230 if (bitset_test (FDERIVES (ritem
[core
[c
]]), ruleno
))
231 SETBIT (ruleset
, ruleno
);
236 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
237 if (BITISSET (ruleset
, ruleno
))
239 int itemno
= rules
[ruleno
].rhs
;
240 while (c
< n
&& core
[c
] < itemno
)
242 itemset
[nitemset
] = core
[c
];
246 itemset
[nitemset
] = itemno
;
252 itemset
[nitemset
] = core
[c
];
258 print_closure ("output", itemset
, nitemset
);
269 for (i
= 0 ; i
< nvars
; ++i
)
270 bitset_free (fderives
[i
]);