X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FParser.java;h=d4d5415972c984141db742da684e523a41e4736d;hp=357519a486dc682304390d6f0b8bfb806a5cedb2;hb=24112db237318c030b4d4f457d90c34fd69d652b;hpb=5ea3b9182192a0fbb7a0bd86b919384ddaeff29a diff --git a/src/edu/berkeley/sbp/Parser.java b/src/edu/berkeley/sbp/Parser.java index 357519a..d4d5415 100644 --- a/src/edu/berkeley/sbp/Parser.java +++ b/src/edu/berkeley/sbp/Parser.java @@ -15,9 +15,9 @@ public abstract class Parser { 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 shiftToken(Input.Location oldloc, Tok t, Input.Location newloc); + protected abstract Forest shiftToken(Tok t, Input.Location newloc); - public boolean helpgc = true; + boolean helpgc = true; public String toString() { return pt.toString(); } @@ -26,13 +26,13 @@ public abstract class Parser { GSS gss = new GSS(); Input.Location loc = input.getLocation(); GSS.Phase current = gss.new Phase(null, this, null, input.next(), loc, null); - current.newNode(null, Forest.leaf(null, null), pt.start, true); + current.newNode(null, Forest.create(null, null, null, false), pt.start, true); int count = 1; for(int idx=0;;idx++) { Input.Location oldloc = loc; loc = input.getLocation(); current.reduce(); - Forest forest = current.token==null ? null : shiftToken(oldloc, (Tok)current.token, loc); + Forest forest = current.token==null ? null : shiftToken((Tok)current.token, loc); GSS.Phase next = gss.new Phase(current, this, current, input.next(), loc, forest); if (!helpgc) { FileOutputStream fos = new FileOutputStream("out-"+idx+".dot"); @@ -82,11 +82,15 @@ public abstract class Parser { if (hs.contains(e)) return; hs.add(e); if (e instanceof Atom) return; - for(Sequence s : (Union)e) { - hs.add(s); - for(Position p = s.firstp(); p != null; p = p.next()) - walk(p.element(), hs); - } + for(Sequence s : (Union)e) + walk(s, hs); + } + private void walk(Sequence s, HashSet hs) { + hs.add(s); + for(Position p = s.firstp(); p != null; p = p.next()) + walk(p.element(), hs); + for(Sequence ss : s.needs()) walk(ss, hs); + for(Sequence ss : s.hates()) walk(ss, hs); } /** the start state */ @@ -287,10 +291,15 @@ public abstract class Parser { // Helpers ////////////////////////////////////////////////////////////////////////////// + private static void reachable(Sequence s, HashSet h) { + reachable(s.firstp(), h); + for(Sequence ss : s.needs()) reachable(ss, h); + for(Sequence ss : s.hates()) reachable(ss, h); + } private static void reachable(Element e, HashSet h) { if (e instanceof Atom) return; for(Sequence s : ((Union)e)) - reachable(s.firstp(), h); + reachable(s, h); } private static void reachable(Position p, HashSet h) { if (h.contains(p)) return;