X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=ac34264b3a527fe16c9fb6790ab802e4aab2840d;hb=3473141880127c6aa52d094d51739e5eb67f1703;hp=610af5879d4daa47b54bbf360def27de2d7ee97d;hpb=526da96dd06e152d194ec92c9ef9df6085a1251b;p=sbp.git diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 610af58..ac34264 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -87,7 +87,15 @@ public abstract class Sequence extends Element implements Iterable { // Position ////////////////////////////////////////////////////////////////////////////// /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */ - public class Position { + public class Position implements IntegerMappable { + + private Forest zero = null; + public Forest zero() { + if (zero != null) return zero; + if (pos > 0) throw new Error(); + return zero = rewrite(null); + } + final int pos; private final Position next; @@ -100,11 +108,6 @@ public abstract class Sequence extends Element implements Iterable { } boolean isFirst() { return pos==0; } - boolean isRightNullable(Walk.Cache cache) { - if (isLast()) return true; - if (!element().possiblyEpsilon(cache)) return false; - return next().isRightNullable(cache); - } /** the element immediately after this Position, or null if this is the last Position */ public Element element() { return pos>=elements.length ? null : elements[pos]; } @@ -118,7 +121,7 @@ public abstract class Sequence extends Element implements Iterable { /** true iff this Position is the last one in the sequence */ public boolean isLast() { return next()==null; } - // Reduction ///////////////////////////////////////////////////////////////////////////////// + // Position ///////////////////////////////////////////////////////////////////////////////// final Forest rewrite(Token.Location loc) { if (this==firstp()) return epsilonForm(); @@ -142,14 +145,16 @@ public abstract class Sequence extends Element implements Iterable { for(Position p = Sequence.this.firstp(); p != null; p = p.next()) { ret.append(' '); if (p==this) ret.append(" | "); - if (p.element()!=null) ret.append(p.element().possiblyEpsilon(null) ? "["+p.element()+"]" : p.element()); + if (p.element()!=null) ret.append(p.element()); else ret.append(' '); } ret.append("}>"); return ret.toString(); } + private final int idx = master_position_idx++; + public int toInt() { return idx; } } - + private static int master_position_idx = 0; // toString ////////////////////////////////////////////////////////////////////////////// @@ -170,7 +175,7 @@ public abstract class Sequence extends Element implements Iterable { private final Object result; public Constant(Element[] e, Object result, HashSet and, HashSet not) { super(e, and, not); this.result = result; } public Forest postReduce(Token.Location loc, Forest[] args) { - return (Forest)Forest.leaf(loc, result, this); + return (Forest)Forest.leaf(loc, result); } static class Drop extends Constant { public Drop(Element[] e, HashSet and, HashSet not, boolean lame) { @@ -185,7 +190,7 @@ public abstract class Sequence extends Element implements Iterable { private final int idx; public Singleton(Element e, HashSet and, HashSet not) { this(new Element[] { e }, 0, and, not); } public Singleton(Element[] e, int idx, HashSet and, HashSet not) { super(e, and, not); this.idx = idx; } - public Forest postReduce(Token.Location loc, Forest[] args) { return (Forest)Forest.singleton(loc, args[idx], this); } + public Forest postReduce(Token.Location loc, Forest[] args) { return (Forest)Forest.singleton(loc, args[idx]); } } public static class Unwrap extends Sequence { @@ -194,13 +199,13 @@ public abstract class Sequence extends Element implements Iterable { public Unwrap(Element[] e, boolean[] drops, HashSet and, HashSet not) { super(e, and, not); this.drops = drops; } public Forest postReduce(Token.Location loc, Forest[] args) { for(int i=0; i[] args2 = new Forest[count]; int j = 0; for(int i=0; i { int j = 0; for(int i=0; i