/** Produce the new lalr_item that results from shifting the dot one position * to the right. */ public lalr_item shift() throws internal_error { lalr_item result; /* can't shift if we have dot already at the end */ if (dot_at_end()) throw new internal_error("Attempt to shift past end of an lalr_item"); /* create the new item w/ the dot shifted by one */ result = new lalr_item(the_production(), dot_pos()+1, new terminal_set(lookahead())); /* change in our lookahead needs to be propagated to this item */ add_propagate(result); return result; }
/** Generic equality comparison. */ public boolean equals(Object other) { if (!(other instanceof lalr_item)) return false; else return equals((lalr_item)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 */ 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(); }
/** Propagate incoming lookaheads through this item to others need to * be changed. * @params incoming symbols to potentially be added to lookahead of this item. */ public void propagate_lookaheads(terminal_set incoming) throws internal_error { boolean change = false; /* if we don't need to propagate, then bail out now */ if (!needs_propagation && (incoming == null || incoming.empty())) return; /* if we have null incoming, treat as an empty set */ if (incoming != null) { /* add the incoming to the lookahead of this item */ change = lookahead().add(incoming); } /* if we changed or need it anyway, propagate across our links */ if (change || needs_propagation) { /* don't need to propagate again */ needs_propagation = false; /* propagate our lookahead into each item we are linked to */ for (int i = 0; i < propagate_items().size(); i++) ((lalr_item)propagate_items().elementAt(i)) .propagate_lookaheads(lookahead()); } }
nt = itm.dot_before_nt(); if (nt != null) new_lookaheads = itm.calc_lookahead(itm.lookahead()); need_prop = itm.lookahead_visible(); new_itm = new lalr_item(prod, new terminal_set(new_lookaheads)); itm.add_propagate(add_itm);
itm = new lalr_item(start_prod); itm.lookahead().add(terminal.EOF); sym = itm.symbol_after_dot(); if (sym != null) outgoing.add(sym); sym2 = itm.symbol_after_dot(); if (sym.equals(sym2)) new_items.add(itm.shift()); for (int l =0; l < fix_itm.propagate_items().size(); l++) (lalr_item)fix_itm.propagate_items().elementAt(l); fix_itm.propagate_items().setElementAt(existing ,l);
System.err.println(itm1.to_simple_string()); System.err.print (" and "); System.err.println(itm2.to_simple_string()); System.err.print(" under symbols: {" ); for (int t = 0; t < terminal.number(); t++) if (itm1.lookahead().contains(t) && itm2.lookahead().contains(t)) if (itm1.the_production().index() < itm2.the_production().index()) System.err.println("the first production.\n"); else
if (itm.dot_at_end()) act = new reduce_action(itm.the_production()); if (!itm.lookahead().contains(t)) continue; if (itm.the_production().index() < ((reduce_action)other_act).reduce_with().index())
/** Produce the new lalr_item that results from shifting the dot one position * to the right. */ public lalr_item shift() throws internal_error { lalr_item result; /* can't shift if we have dot already at the end */ if (dot_at_end()) throw new internal_error("Attempt to shift past end of an lalr_item"); /* create the new item w/ the dot shifted by one */ result = new lalr_item(the_production(), dot_pos()+1, new terminal_set(lookahead())); /* change in our lookahead needs to be propagated to this item */ add_propagate(result); return result; }
/** 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; } }
if (itm.dot_at_end()) if (compare.dot_at_end()) if (compare.lookahead().intersects(itm.lookahead())) terminal_set lookahead = itm.lookahead(); for (int t = 0; t < terminal.number(); t++) if (conflict_set.contains(t) && lookahead.contains(t))
if (itm.dot_at_end()) if (compare.dot_at_end()) if (compare.lookahead().intersects(itm.lookahead())) shift_sym = compare.symbol_after_dot(); if (!shift_sym.is_non_term()) if (itm.lookahead().contains((terminal)shift_sym))
/** Propagate lookahead sets out of this state. This recursively * propagates to all items that have propagation links from some item * in this state. */ protected void propagate_lookaheads() throws internal_error { /* recursively propagate out from each item in the state */ for (Enumeration itm = items().all(); itm.hasMoreElements(); ) ((lalr_item)itm.nextElement()).propagate_lookaheads(null); }
nt = itm.dot_before_nt(); if (nt != null) new_lookaheads = itm.calc_lookahead(itm.lookahead()); need_prop = itm.lookahead_visible(); new_itm = new lalr_item(prod, new terminal_set(new_lookaheads)); itm.add_propagate(add_itm);
itm = new lalr_item(start_prod); itm.lookahead().add(terminal.EOF); sym = itm.symbol_after_dot(); if (sym != null) outgoing.add(sym); sym2 = itm.symbol_after_dot(); if (sym.equals(sym2)) new_items.add(itm.shift()); for (int l =0; l < fix_itm.propagate_items().size(); l++) (lalr_item)fix_itm.propagate_items().elementAt(l); fix_itm.propagate_items().setElementAt(existing ,l);
/** Propagate incoming lookaheads through this item to others need to * be changed. * @params incoming symbols to potentially be added to lookahead of this item. */ public void propagate_lookaheads(terminal_set incoming) throws internal_error { boolean change = false; /* if we don't need to propagate, then bail out now */ if (!needs_propagation && (incoming == null || incoming.empty())) return; /* if we have null incoming, treat as an empty set */ if (incoming != null) { /* add the incoming to the lookahead of this item */ change = lookahead().add(incoming); } /* if we changed or need it anyway, propagate across our links */ if (change || needs_propagation) { /* don't need to propagate again */ needs_propagation = false; /* propagate our lookahead into each item we are linked to */ for (int i = 0; i < propagate_items().size(); i++) ((lalr_item)propagate_items().elementAt(i)) .propagate_lookaheads(lookahead()); } }
System.err.println(itm1.to_simple_string()); System.err.print (" and "); System.err.println(itm2.to_simple_string()); System.err.print(" under symbols: {" ); for (int t = 0; t < terminal.number(); t++) if (itm1.lookahead().contains(t) && itm2.lookahead().contains(t)) if (itm1.the_production().index() < itm2.the_production().index()) System.err.println("the first production.\n"); else
if (itm.dot_at_end()) act = new reduce_action(itm.the_production()); if (!itm.lookahead().contains(t)) continue; if (itm.the_production().index() < ((reduce_action)other_act).reduce_with().index()) if(fix_with_precedence(itm.the_production(), t, our_act_row, act)) { term = null;
/** 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; } };
if (itm.dot_at_end()) if (compare.dot_at_end()) if (compare.lookahead().intersects(itm.lookahead()))