1 2 package com.sun.java_cup.internal; 3 4 import java.util.Hashtable ; 5 import java.util.Enumeration ; 6 7 20 21 public class lalr_item_set { 22 23 24 25 26 27 28 public lalr_item_set() { } 29 30 31 32 35 public lalr_item_set(lalr_item_set other) 36 throws internal_error 37 { 38 not_null(other); 39 _all = (Hashtable )other._all.clone(); 40 } 41 42 43 44 45 46 49 protected Hashtable _all = new Hashtable (11); 50 51 52 public Enumeration all() {return _all.elements();} 53 54 55 56 57 protected Integer hashcode_cache = null; 58 59 60 61 62 public int size() {return _all.size();} 63 64 65 66 67 68 71 public boolean contains(lalr_item itm) {return _all.containsKey(itm);} 72 73 74 75 79 public lalr_item find(lalr_item itm) {return (lalr_item)_all.get(itm);} 80 81 82 83 86 public boolean is_subset_of(lalr_item_set other) throws internal_error 87 { 88 not_null(other); 89 90 91 for (Enumeration e = all(); e.hasMoreElements(); ) 92 if (!other.contains((lalr_item)e.nextElement())) 93 return false; 94 95 96 return true; 97 } 98 99 100 101 104 public boolean is_superset_of(lalr_item_set other) throws internal_error 105 { 106 not_null(other); 107 return other.is_subset_of(this); 108 } 109 110 111 112 117 public lalr_item add(lalr_item itm) throws internal_error 118 { 119 lalr_item other; 120 121 not_null(itm); 122 123 124 other = (lalr_item)_all.get(itm); 125 126 127 if (other != null) 128 { 129 other.lookahead().add(itm.lookahead()); 130 return other; 131 } 132 133 else 134 { 135 136 hashcode_cache = null; 137 138 _all.put(itm,itm); 139 return itm; 140 } 141 } 142 143 144 145 148 public void remove(lalr_item itm) throws internal_error 149 { 150 not_null(itm); 151 152 153 hashcode_cache = null; 154 155 156 _all.remove(itm); 157 } 158 159 160 161 165 public void add(lalr_item_set other) throws internal_error 166 { 167 not_null(other); 168 169 170 for (Enumeration e = other.all(); e.hasMoreElements(); ) 171 add((lalr_item)e.nextElement()); 172 } 173 174 175 176 179 public void remove(lalr_item_set other) throws internal_error 180 { 181 not_null(other); 182 183 184 for (Enumeration e = other.all(); e.hasMoreElements(); ) 185 remove((lalr_item)e.nextElement()); 186 } 187 188 189 190 191 public lalr_item get_one() throws internal_error 192 { 193 Enumeration the_set; 194 lalr_item result; 195 196 the_set = all(); 197 if (the_set.hasMoreElements()) 198 { 199 result = (lalr_item)the_set.nextElement(); 200 remove(result); 201 return result; 202 } 203 else 204 return null; 205 } 206 207 208 209 210 211 215 protected void not_null(Object obj) throws internal_error 216 { 217 if (obj == null) 218 throw new internal_error("Null object used in set operation"); 219 } 220 221 222 223 238 public void compute_closure() 239 throws internal_error 240 { 241 lalr_item_set consider; 242 lalr_item itm, new_itm, add_itm; 243 non_terminal nt; 244 terminal_set new_lookaheads; 245 Enumeration p; 246 production prod; 247 boolean need_prop; 248 249 250 251 252 hashcode_cache = null; 253 254 255 consider = new lalr_item_set(this); 256 257 258 while (consider.size() > 0) 259 { 260 261 itm = consider.get_one(); 262 263 264 nt = itm.dot_before_nt(); 265 if (nt != null) 266 { 267 268 new_lookaheads = itm.calc_lookahead(itm.lookahead()); 269 270 271 need_prop = itm.lookahead_visible(); 272 273 274 for (p = nt.productions(); p.hasMoreElements(); ) 275 { 276 prod = (production)p.nextElement(); 277 278 279 new_itm = new lalr_item(prod, 280 new terminal_set(new_lookaheads)); 281 282 283 add_itm = add(new_itm); 284 285 if (need_prop) 286 itm.add_propagate(add_itm); 287 288 289 if (add_itm == new_itm) 290 { 291 292 consider.add(new_itm); 293 } 294 } 295 } 296 } 297 } 298 299 300 301 302 public boolean equals(lalr_item_set other) 303 { 304 if (other == null || other.size() != size()) return false; 305 306 307 try { 308 return is_subset_of(other); 309 } catch (internal_error e) { 310 311 e.crash(); 312 return false; 313 } 314 315 } 316 317 318 319 320 public boolean equals(Object other) 321 { 322 if (!(other instanceof lalr_item_set)) 323 return false; 324 else 325 return equals((lalr_item_set)other); 326 } 327 328 329 330 331 public int hashCode() 332 { 333 int result = 0; 334 Enumeration e; 335 int cnt; 336 337 338 if (hashcode_cache == null) 339 { 340 341 for (e = all(), cnt=0 ; e.hasMoreElements() ; cnt++) 345 result ^= ((lalr_item)e.nextElement()).hashCode(); 346 347 hashcode_cache = new Integer (result); 348 } 349 350 return hashcode_cache.intValue(); 351 } 352 353 354 355 356 public String toString() 357 { 358 StringBuffer result = new StringBuffer (); 359 360 result.append("{\n"); 361 for (Enumeration e=all(); e.hasMoreElements(); ) 362 { 363 result.append(" " + (lalr_item)e.nextElement() + "\n"); 364 } 365 result.append("}"); 366 367 return result.toString(); 368 } 369 370 } 371 372 | Popular Tags |