4 import java.util.Hashtable;
5 import java.util.Enumeration;
7 /** This class represents a set of LALR items. For purposes of building
8 * these sets, items are considered unique only if they have unique cores
9 * (i.e., ignoring differences in their lookahead sets).<p>
11 * This class provides fairly conventional set oriented operations (union,
12 * sub/super-set tests, etc.), as well as an LALR "closure" operation (see
15 * @see java_cup.lalr_item
16 * @see java_cup.lalr_state
17 * @version last updated: 3/6/96
18 * @author Scott Hudson
21 public class lalr_item_set {
23 /*-----------------------------------------------------------*/
24 /*--- Constructor(s) ----------------------------------------*/
25 /*-----------------------------------------------------------*/
27 /** Constructor for an empty set. */
28 public lalr_item_set() { }
30 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
32 /** Constructor for cloning from another set.
33 * @param other indicates set we should copy from.
35 public lalr_item_set(lalr_item_set other)
39 _all = (Hashtable)other._all.clone();
42 /*-----------------------------------------------------------*/
43 /*--- (Access to) Instance Variables ------------------------*/
44 /*-----------------------------------------------------------*/
46 /** A hash table to implement the set. We store the items using themselves
49 protected Hashtable _all = new Hashtable(11);
51 /** Access to all elements of the set. */
52 public Enumeration all() {return _all.elements();}
54 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
56 /** Cached hashcode for this set. */
57 protected Integer hashcode_cache = null;
59 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
61 /** Size of the set */
62 public int size() {return _all.size();}
64 /*-----------------------------------------------------------*/
65 /*--- Set Operation Methods ---------------------------------*/
66 /*-----------------------------------------------------------*/
68 /** Does the set contain a particular item?
69 * @param itm the item in question.
71 public boolean contains(lalr_item itm) {return _all.containsKey(itm);}
73 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
75 /** Return the item in the set matching a particular item (or null if not
77 * @param itm the item we are looking for.
79 public lalr_item find(lalr_item itm) {return (lalr_item)_all.get(itm);}
81 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
83 /** Is this set an (improper) subset of another?
84 * @param other the other set in question.
86 public boolean is_subset_of(lalr_item_set other) throws internal_error
90 /* walk down our set and make sure every element is in the other */
91 for (Enumeration e = all(); e.hasMoreElements(); )
92 if (!other.contains((lalr_item)e.nextElement()))
95 /* they were all there */
99 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
101 /** Is this set an (improper) superset of another?
102 * @param other the other set in question.
104 public boolean is_superset_of(lalr_item_set other) throws internal_error
107 return other.is_subset_of(this);
110 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
112 /** Add a singleton item, merging lookahead sets if the item is already
113 * part of the set. returns the element of the set that was added or
115 * @param itm the item being added.
117 public lalr_item add(lalr_item itm) throws internal_error
123 /* see if an item with a matching core is already there */
124 other = (lalr_item)_all.get(itm);
126 /* if so, merge this lookahead into the original and leave it */
129 other.lookahead().add(itm.lookahead());
132 /* otherwise we just go in the set */
135 /* invalidate cached hashcode */
136 hashcode_cache = null;
143 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
145 /** Remove a single item if it is in the set.
146 * @param itm the item to remove.
148 public void remove(lalr_item itm) throws internal_error
152 /* invalidate cached hashcode */
153 hashcode_cache = null;
155 /* remove it from hash table implementing set */
159 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
161 /** Add a complete set, merging lookaheads where items are already in
163 * @param other the set to be added.
165 public void add(lalr_item_set other) throws internal_error
169 /* walk down the other set and do the adds individually */
170 for (Enumeration e = other.all(); e.hasMoreElements(); )
171 add((lalr_item)e.nextElement());
174 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
176 /** Remove (set subtract) a complete set.
177 * @param other the set to remove.
179 public void remove(lalr_item_set other) throws internal_error
183 /* walk down the other set and do the removes individually */
184 for (Enumeration e = other.all(); e.hasMoreElements(); )
185 remove((lalr_item)e.nextElement());
188 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
190 /** Remove and return one item from the set (done in hash order). */
191 public lalr_item get_one() throws internal_error
197 if (the_set.hasMoreElements())
199 result = (lalr_item)the_set.nextElement();
207 /*-----------------------------------------------------------*/
208 /*--- General Methods ---------------------------------------*/
209 /*-----------------------------------------------------------*/
211 /** Helper function for null test. Throws an interal_error exception if its
213 * @param obj the object we are testing.
215 protected void not_null(Object obj) throws internal_error
218 throw new internal_error("Null object used in set operation");
221 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
223 /** Compute the closure of the set using the LALR closure rules. Basically
224 * for every item of the form: <pre>
225 * [L ::= a *N alpha, l]
227 * (where N is a a non terminal and alpha is a string of symbols) make
228 * sure there are also items of the form: <pre>
229 * [N ::= *beta, first(alpha l)]
231 * corresponding to each production of N. Items with identical cores but
232 * differing lookahead sets are merged by creating a new item with the same
233 * core and the union of the lookahead sets (the LA in LALR stands for
234 * "lookahead merged" and this is where the merger is). This routine
235 * assumes that nullability and first sets have been computed for all
236 * productions before it is called.
238 public void compute_closure()
239 throws internal_error
241 lalr_item_set consider;
242 lalr_item itm, new_itm, add_itm;
244 terminal_set new_lookaheads;
251 /* invalidate cached hashcode */
252 hashcode_cache = null;
254 /* each current element needs to be considered */
255 consider = new lalr_item_set(this);
257 /* repeat this until there is nothing else to consider */
258 while (consider.size() > 0)
260 /* get one item to consider */
261 itm = consider.get_one();
263 /* do we have a dot before a non terminal */
264 nt = itm.dot_before_nt();
267 /* create the lookahead set based on first after dot */
268 new_lookaheads = itm.calc_lookahead(itm.lookahead());
270 /* are we going to need to propagate our lookahead to new item */
271 need_prop = itm.lookahead_visible();
273 /* create items for each production of that non term */
274 for (p = nt.productions(); p.hasMoreElements(); )
276 prod = (production)p.nextElement();
278 /* create new item with dot at start and that lookahead */
279 new_itm = new lalr_item(prod,
280 new terminal_set(new_lookaheads));
282 /* add/merge item into the set */
283 add_itm = add(new_itm);
284 /* if propagation is needed link to that item */
286 itm.add_propagate(add_itm);
288 /* was this was a new item*/
289 if (add_itm == new_itm)
291 /* that may need further closure, consider it also */
292 consider.add(new_itm);
299 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
301 /** Equality comparison. */
302 public boolean equals(lalr_item_set other)
304 if (other == null || other.size() != size()) return false;
306 /* once we know they are the same size, then improper subset does test */
308 return is_subset_of(other);
309 } catch (internal_error e) {
310 /* can't throw error from here (because superclass doesn't) so crash */
317 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
319 /** Generic equality comparison. */
320 public boolean equals(Object other)
322 if (!(other instanceof lalr_item_set))
325 return equals((lalr_item_set)other);
328 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
330 /** Return hash code. */
331 public int hashCode()
337 /* only compute a new one if we don't have it cached */
338 if (hashcode_cache == null)
340 /* hash together codes from at most first 5 elements */
341 // CSA fix! we'd *like* to hash just a few elements, but
342 // that means equal sets will have inequal hashcodes, which
343 // we're not allowed (by contract) to do. So hash them all.
344 for (e = all(), cnt=0 ; e.hasMoreElements() /*&& cnt<5*/; cnt++)
345 result ^= ((lalr_item)e.nextElement()).hashCode();
347 hashcode_cache = new Integer(result);
350 return hashcode_cache.intValue();
353 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
355 /** Convert to string. */
356 public String toString()
358 StringBuffer result = new StringBuffer();
360 result.append("{\n");
361 for (Enumeration e=all(); e.hasMoreElements(); )
363 result.append(" " + (lalr_item)e.nextElement() + "\n");
367 return result.toString();
369 /*-----------------------------------------------------------*/