X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FParser.java;h=0d04f3f1b7f5f14e06a9d6dba485419ad684695d;hp=24f8410d0d5f521a19cc47b2091dc65b69d447b1;hb=aeae43c3255b14e4ba65e0171b87c48bbf953c31;hpb=37d28a2fa4ecc9534ca03827c23fdb493af79645 diff --git a/src/edu/berkeley/sbp/Parser.java b/src/edu/berkeley/sbp/Parser.java index 24f8410..0d04f3f 100644 --- a/src/edu/berkeley/sbp/Parser.java +++ b/src/edu/berkeley/sbp/Parser.java @@ -32,8 +32,8 @@ public abstract class Parser { current.reduce(); Forest forest = current.token==null ? null : shiftedToken((T)current.token, loc); GSS.Phase next = gss.new Phase(current, this, current, input.next(count, gss.resets, gss.waits), loc, forest); - count = next.hash.size(); - if (current.isDone()) return (Forest)current.finalResult; + count = next.size(); + if (current.isDone()) return (Forest)gss.finalResult; current = next; } } @@ -93,13 +93,11 @@ public abstract class Parser { if (isRightNullable(p)) { Walk.Follow wf = new Walk.Follow(top.empty(), p.owner(), all_elements, cache); - Reduction red = new Reduction(p); - Topology follow = wf.walk(p.owner()); for(Position p2 = p; p2 != null && p2.element() != null; p2 = p2.next()) follow = follow.intersect(new Walk.Follow(top.empty(), p2.element(), all_elements, cache).walk(p2.element())); - state.reductions.put(follow, red); - if (wf.includesEof()) state.eofReductions.add(red); + state.reductions.put(follow, p); + if (wf.includesEof()) state.eofReductions.add(p); } // if the element following this position is an atom, copy the corresponding @@ -126,31 +124,30 @@ public abstract class Parser { public final int idx = master_state_idx++; private final HashSet hs; - private transient HashMap gotoSetNonTerminals = new HashMap(); + public transient HashMap gotoSetNonTerminals = new HashMap(); private transient TopologicalBag gotoSetTerminals = new TopologicalBag(); - private TopologicalBag reductions = new TopologicalBag(); - private HashSet eofReductions = new HashSet(); + private TopologicalBag reductions = new TopologicalBag(); + private HashSet eofReductions = new HashSet(); private TopologicalBag shifts = new TopologicalBag(); private boolean accept = false; private VisitableMap oshifts = null; - private VisitableMap oreductions = null; + private VisitableMap oreductions = null; // Interface Methods ////////////////////////////////////////////////////////////////////////////// - public boolean isAccepting() { return accept; } - - public boolean canShift(Token t) { return oshifts.contains(t); } - public boolean canReduce(Token t) { return t==null ? eofReductions.size()>0 : oreductions.contains(t); } - + boolean isAccepting() { return accept; } public Iterator iterator() { return hs.iterator(); } - public void invokeShifts(Token t, Invokable irbc, B b, C c) { + boolean canShift(Token t) { return oshifts.contains(t); } + void invokeShifts(Token t, Invokable irbc, B b, C c) { oshifts.invoke(t, irbc, b, c); } - public void invokeReductions(Token t, Invokable irbc, B b, C c) { - if (t==null) for(Reduction r : eofReductions) irbc.invoke(r, b, c); + + boolean canReduce(Token t) { return t==null ? eofReductions.size()>0 : oreductions.contains(t); } + void invokeReductions(Token t, Invokable irbc, B b, C c) { + if (t==null) for(Position r : eofReductions) irbc.invoke(r, b, c); else oreductions.invoke(t, irbc, b, c); } @@ -236,7 +233,6 @@ public abstract class Parser { } public String toString() { - //return "state["+idx+"]"; StringBuffer ret = new StringBuffer(); ret.append("state["+idx+"]: "); for(Position p : this) ret.append("{"+p+"} "); @@ -246,78 +242,6 @@ public abstract class Parser { public int compareTo(Table.State s) { return idx==s.idx ? 0 : idx < s.idx ? -1 : 1; } public int toInt() { return idx; } } - - /** - * the information needed to perform a reduction; copied here to - * avoid keeping references to Element objects in a Table - */ - public class Reduction { - // FIXME: cleanup; almost everything in here could go in either Sequence.Position.getRewrite() or else in GSS.Reduct - public final int numPop; - /*private*/ final Position position; - private final Forest[] holder; // to avoid constant reallocation - public int hashCode() { return position.hashCode(); } - public boolean equals(Object o) { - if (o==null) return false; - if (o==this) return true; - if (!(o instanceof Reduction)) return false; - Reduction r = (Reduction)o; - return r.position == position; - } - public Reduction(Position p) { - this.position = p; - this.numPop = p.pos; - this.holder = new Forest[numPop]; - } - public String toString() { return "[reduce " + position + "]"; } - - private Forest zero = null; - public Forest zero() { - if (zero != null) return zero; - if (numPop > 0) throw new Error(); - return zero = position.rewrite(null); - } - - public void reduce(GSS.Phase.Node parent) { - if (numPop==0) finish(parent, zero(), parent.phase()); - else reduce(parent, numPop-1, parent.phase()); - } - - public void reduce(GSS.Phase.Node parent, GSS.Phase.Node onlychild) { - if (numPop<=0) throw new Error("called wrong form of reduce()"); - int pos = numPop-1; - Forest old = holder[pos]; - holder[pos] = parent.pending(); - if (pos==0) { - System.arraycopy(holder, 0, position.holder, 0, holder.length); - finish(onlychild, position.rewrite(parent.phase().getLocation()), parent.phase()); - } else { - reduce(onlychild, pos-1, parent.phase()); - } - holder[pos] = old; - } - - // FIXME: this could be more elegant and/or cleaner and/or somewhere else - private void reduce(GSS.Phase.Node parent, int pos, GSS.Phase target) { - Forest old = holder[pos]; - holder[pos] = parent.pending(); - if (pos==0) { - System.arraycopy(holder, 0, position.holder, 0, holder.length); - for(int i=0; i