1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
package java_cup;
import java.util.Hashtable;
import java.util.Enumeration;
/** This class represents a set of LALR items. For purposes of building
* these sets, items are considered unique only if they have unique cores
* (i.e., ignoring differences in their lookahead sets).<p>
*
* This class provides fairly conventional set oriented operations (union,
* sub/super-set tests, etc.), as well as an LALR "closure" operation (see
* compute_closure()).
*
* @see java_cup.lalr_item
* @see java_cup.lalr_state
* @version last updated: 3/6/96
* @author Scott Hudson
*/
public class lalr_item_set {
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/** Constructor for an empty set. */
public lalr_item_set() { }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Constructor for cloning from another set.
* @param other indicates set we should copy from.
*/
public lalr_item_set(lalr_item_set other)
throws internal_error
{
not_null(other);
_all = (Hashtable)other._all.clone();
}
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/** A hash table to implement the set. We store the items using themselves
* as keys.
*/
protected Hashtable _all = new Hashtable(11);
/** Access to all elements of the set. */
public Enumeration all() {return _all.elements();}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Cached hashcode for this set. */
protected Integer hashcode_cache = null;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Size of the set */
public int size() {return _all.size();}
/*-----------------------------------------------------------*/
/*--- Set Operation Methods ---------------------------------*/
/*-----------------------------------------------------------*/
/** Does the set contain a particular item?
* @param itm the item in question.
*/
public boolean contains(lalr_item itm) {return _all.containsKey(itm);}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Return the item in the set matching a particular item (or null if not
* found)
* @param itm the item we are looking for.
*/
public lalr_item find(lalr_item itm) {return (lalr_item)_all.get(itm);}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Is this set an (improper) subset of another?
* @param other the other set in question.
*/
public boolean is_subset_of(lalr_item_set other) throws internal_error
{
not_null(other);
/* walk down our set and make sure every element is in the other */
for (Enumeration e = all(); e.hasMoreElements(); )
if (!other.contains((lalr_item)e.nextElement()))
return false;
/* they were all there */
return true;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Is this set an (improper) superset of another?
* @param other the other set in question.
*/
public boolean is_superset_of(lalr_item_set other) throws internal_error
{
not_null(other);
return other.is_subset_of(this);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Add a singleton item, merging lookahead sets if the item is already
* part of the set. returns the element of the set that was added or
* merged into.
* @param itm the item being added.
*/
public lalr_item add(lalr_item itm) throws internal_error
{
lalr_item other;
not_null(itm);
/* see if an item with a matching core is already there */
other = (lalr_item)_all.get(itm);
/* if so, merge this lookahead into the original and leave it */
if (other != null)
{
other.lookahead().add(itm.lookahead());
return other;
}
/* otherwise we just go in the set */
else
{
/* invalidate cached hashcode */
hashcode_cache = null;
_all.put(itm,itm);
return itm;
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Remove a single item if it is in the set.
* @param itm the item to remove.
*/
public void remove(lalr_item itm) throws internal_error
{
not_null(itm);
/* invalidate cached hashcode */
hashcode_cache = null;
/* remove it from hash table implementing set */
_all.remove(itm);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Add a complete set, merging lookaheads where items are already in
* the set
* @param other the set to be added.
*/
public void add(lalr_item_set other) throws internal_error
{
not_null(other);
/* walk down the other set and do the adds individually */
for (Enumeration e = other.all(); e.hasMoreElements(); )
add((lalr_item)e.nextElement());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Remove (set subtract) a complete set.
* @param other the set to remove.
*/
public void remove(lalr_item_set other) throws internal_error
{
not_null(other);
/* walk down the other set and do the removes individually */
for (Enumeration e = other.all(); e.hasMoreElements(); )
remove((lalr_item)e.nextElement());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Remove and return one item from the set (done in hash order). */
public lalr_item get_one() throws internal_error
{
Enumeration the_set;
lalr_item result;
the_set = all();
if (the_set.hasMoreElements())
{
result = (lalr_item)the_set.nextElement();
remove(result);
return result;
}
else
return null;
}
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/** Helper function for null test. Throws an interal_error exception if its
* parameter is null.
* @param obj the object we are testing.
*/
protected void not_null(Object obj) throws internal_error
{
if (obj == null)
throw new internal_error("Null object used in set operation");
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Compute the closure of the set using the LALR closure rules. Basically
* for every item of the form: <pre>
* [L ::= a *N alpha, l]
* </pre>
* (where N is a a non terminal and alpha is a string of symbols) make
* sure there are also items of the form: <pre>
* [N ::= *beta, first(alpha l)]
* </pre>
* corresponding to each production of N. Items with identical cores but
* differing lookahead sets are merged by creating a new item with the same
* core and the union of the lookahead sets (the LA in LALR stands for
* "lookahead merged" and this is where the merger is). This routine
* assumes that nullability and first sets have been computed for all
* productions before it is called.
*/
public void compute_closure()
throws internal_error
{
lalr_item_set consider;
lalr_item itm, new_itm, add_itm;
non_terminal nt;
terminal_set new_lookaheads;
Enumeration p;
production prod;
boolean need_prop;
/* invalidate cached hashcode */
hashcode_cache = null;
/* each current element needs to be considered */
consider = new lalr_item_set(this);
/* repeat this until there is nothing else to consider */
while (consider.size() > 0)
{
/* get one item to consider */
itm = consider.get_one();
/* do we have a dot before a non terminal */
nt = itm.dot_before_nt();
if (nt != null)
{
/* create the lookahead set based on first after dot */
new_lookaheads = itm.calc_lookahead(itm.lookahead());
/* are we going to need to propagate our lookahead to new item */
need_prop = itm.lookahead_visible();
/* create items for each production of that non term */
for (p = nt.productions(); p.hasMoreElements(); )
{
prod = (production)p.nextElement();
/* create new item with dot at start and that lookahead */
new_itm = new lalr_item(prod,
new terminal_set(new_lookaheads));
/* add/merge item into the set */
add_itm = add(new_itm);
/* if propagation is needed link to that item */
if (need_prop)
itm.add_propagate(add_itm);
/* was this was a new item*/
if (add_itm == new_itm)
{
/* that may need further closure, consider it also */
consider.add(new_itm);
}
}
}
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Equality comparison. */
public boolean equals(lalr_item_set other)
{
if (other == null || other.size() != size()) return false;
/* once we know they are the same size, then improper subset does test */
try {
return is_subset_of(other);
} catch (internal_error e) {
/* can't throw error from here (because superclass doesn't) so crash */
e.crash();
return false;
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Generic equality comparison. */
public boolean equals(Object other)
{
if (!(other instanceof lalr_item_set))
return false;
else
return equals((lalr_item_set)other);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Return hash code. */
public int hashCode()
{
int result = 0;
Enumeration e;
int cnt;
/* only compute a new one if we don't have it cached */
if (hashcode_cache == null)
{
/* hash together codes from at most first 5 elements */
// CSA fix! we'd *like* to hash just a few elements, but
// that means equal sets will have inequal hashcodes, which
// we're not allowed (by contract) to do. So hash them all.
for (e = all(), cnt=0 ; e.hasMoreElements() /*&& cnt<5*/; cnt++)
result ^= ((lalr_item)e.nextElement()).hashCode();
hashcode_cache = new Integer(result);
}
return hashcode_cache.intValue();
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Convert to string. */
public String toString()
{
StringBuffer result = new StringBuffer();
result.append("{\n");
for (Enumeration e=all(); e.hasMoreElements(); )
{
result.append(" " + (lalr_item)e.nextElement() + "\n");
}
result.append("}");
return result.toString();
}
/*-----------------------------------------------------------*/
}
|