X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FParser.java;h=d04c49f21ca13468d8cf185c0e85b0b7c42c929b;hp=b4ac23a0a3285023df34427c262e921a9411e241;hb=2fa0e673bf1813cce48ae7004e533a317d5f58e0;hpb=cd979dfc5968a185c12d6eedc2aa688ef4b0f8b8 diff --git a/src/edu/berkeley/sbp/Parser.java b/src/edu/berkeley/sbp/Parser.java index b4ac23a..d04c49f 100644 --- a/src/edu/berkeley/sbp/Parser.java +++ b/src/edu/berkeley/sbp/Parser.java @@ -11,15 +11,12 @@ public abstract class Parser { private final Table pt; /** create a parser to parse the grammar with start symbol u */ - protected Parser(Union u) { this.pt = new Table(u, top()); } - protected Parser(Table pt) { this.pt = pt; } + protected Parser(Union u, Topology top) { this.pt = new Table(u, top); } + protected Parser(Table pt) { this.pt = pt; } /** implement this method to create the output forest corresponding to a lone shifted input token */ public abstract Forest shiftedToken(T t, Token.Location loc); - /** this method must return an empty topology of the input token type */ - public abstract Topology top(); - /** parse input, using the table pt to drive the parser */ public Forest parse(Token.Stream input) throws IOException, ParseFailed { GSS gss = new GSS(); @@ -93,13 +90,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 @@ -129,13 +124,13 @@ public abstract class Parser { 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 ////////////////////////////////////////////////////////////////////////////// @@ -148,8 +143,8 @@ public abstract class Parser { } 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(Reduction r : eofReductions) irbc.invoke(r, b, c); + 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); } @@ -244,42 +239,10 @@ 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 - /*private*/ final Position position; - 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; - } - public String toString() { return "[reduce " + position + "]"; } - - private Forest zero = null; - public Forest zero() { - if (zero != null) return zero; - if (position.pos > 0) throw new Error(); - return zero = position.rewrite(null); - } - - } } - private static final Forest[] emptyForestArray = new Forest[0]; - - // Helpers ////////////////////////////////////////////////////////////////////////////// - + private static void reachable(Element e, HashSet h) { if (e instanceof Atom) return; for(Sequence s : ((Union)e))