]>
git.saurik.com Git - bison.git/blob - src/closure.c
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2007 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to the Free
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <bitsetv-print.h>
38 /* NITEMSET is the size of the array ITEMSET. */
42 static bitset ruleset
;
44 /* internal data. See comments before set_fderives and set_firsts. */
45 static bitsetv fderives
= NULL
;
46 static bitsetv firsts
= NULL
;
48 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
49 #define FDERIVES(Var) fderives[(Var) - ntokens]
50 #define FIRSTS(Var) firsts[(Var) - ntokens]
58 print_closure (char const *title
, item_number
*array
, size_t size
)
61 fprintf (stderr
, "Closure: %s\n", title
);
62 for (i
= 0; i
< size
; ++i
)
65 fprintf (stderr
, " %2d: .", array
[i
]);
66 for (rp
= &ritem
[array
[i
]]; *rp
>= 0; ++rp
)
67 fprintf (stderr
, " %s", symbols
[*rp
]->tag
);
68 fprintf (stderr
, " (rule %d)\n", -*rp
- 1);
70 fputs ("\n\n", stderr
);
79 fprintf (stderr
, "FIRSTS\n");
80 for (i
= ntokens
; i
< nsyms
; i
++)
83 fprintf (stderr
, "\t%s firsts\n", symbols
[i
]->tag
);
84 BITSET_FOR_EACH (iter
, FIRSTS (i
), j
, 0)
86 fprintf (stderr
, "\t\t%s\n",
87 symbols
[j
+ ntokens
]->tag
);
90 fprintf (stderr
, "\n\n");
100 fprintf (stderr
, "FDERIVES\n");
101 for (i
= ntokens
; i
< nsyms
; i
++)
103 bitset_iterator iter
;
104 fprintf (stderr
, "\t%s derives\n", symbols
[i
]->tag
);
105 BITSET_FOR_EACH (iter
, FDERIVES (i
), r
, 0)
107 fprintf (stderr
, "\t\t%3d ", r
);
108 rule_rhs_print (&rules
[r
], stderr
);
111 fprintf (stderr
, "\n\n");
114 /*------------------------------------------------------------------.
115 | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
116 | items can represent the beginning of the input corresponding to |
117 | which other items. |
119 | For example, if some rule expands symbol 5 into the sequence of |
120 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
121 | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
123 `------------------------------------------------------------------*/
130 firsts
= bitsetv_create (nvars
, nvars
, BITSET_FIXED
);
132 for (i
= ntokens
; i
< nsyms
; i
++)
133 for (j
= 0; derives
[i
- ntokens
][j
]; ++j
)
135 item_number sym
= derives
[i
- ntokens
][j
]->rhs
[0];
137 bitset_set (FIRSTS (i
), sym
- ntokens
);
140 if (trace_flag
& trace_sets
)
141 bitsetv_matrix_dump (stderr
, "RTC: Firsts Input", firsts
);
142 bitsetv_reflexive_transitive_closure (firsts
);
143 if (trace_flag
& trace_sets
)
144 bitsetv_matrix_dump (stderr
, "RTC: Firsts Output", firsts
);
146 if (trace_flag
& trace_sets
)
150 /*-------------------------------------------------------------------.
151 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
152 | rules can help derive the beginning of the data for each |
155 | For example, if symbol 5 can be derived as the sequence of symbols |
156 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
157 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
158 `-------------------------------------------------------------------*/
166 fderives
= bitsetv_create (nvars
, nrules
, BITSET_FIXED
);
170 for (i
= ntokens
; i
< nsyms
; ++i
)
171 for (j
= ntokens
; j
< nsyms
; ++j
)
172 if (bitset_test (FIRSTS (i
), j
- ntokens
))
173 for (k
= 0; derives
[j
- ntokens
][k
]; ++k
)
174 bitset_set (FDERIVES (i
), derives
[j
- ntokens
][k
]->number
);
176 if (trace_flag
& trace_sets
)
179 bitsetv_free (firsts
);
185 new_closure (unsigned int n
)
187 itemset
= xnmalloc (n
, sizeof *itemset
);
189 ruleset
= bitset_create (nrules
, BITSET_FIXED
);
197 closure (item_number
*core
, size_t n
)
199 /* Index over CORE. */
202 /* A bit index over RULESET. */
205 bitset_iterator iter
;
207 if (trace_flag
& trace_sets
)
208 print_closure ("input", core
, n
);
210 bitset_zero (ruleset
);
212 for (c
= 0; c
< n
; ++c
)
213 if (ISVAR (ritem
[core
[c
]]))
214 bitset_or (ruleset
, ruleset
, FDERIVES (ritem
[core
[c
]]));
216 /* core is sorted on item number from ritem, which is sorted on rule number.
217 Compute itemset with the same sort. */
220 BITSET_FOR_EACH (iter
, ruleset
, ruleno
, 0)
222 item_number itemno
= rules
[ruleno
].rhs
- ritem
;
223 while (c
< n
&& core
[c
] < itemno
)
225 itemset
[nitemset
] = core
[c
];
229 itemset
[nitemset
] = itemno
;
235 itemset
[nitemset
] = core
[c
];
240 if (trace_flag
& trace_sets
)
241 print_closure ("output", itemset
, nitemset
);
249 bitset_free (ruleset
);
250 bitsetv_free (fderives
);