X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=68e311e1f0e1dc4b12c8a9b90e5cb0a3dc3c85f8;hp=610af5879d4daa47b54bbf360def27de2d7ee97d;hb=fc1e5069ec5401c425dd29b77b04285916b62d10;hpb=526da96dd06e152d194ec92c9ef9df6085a1251b diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 610af58..68e311e 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -12,6 +12,12 @@ public abstract class Sequence extends Element implements Iterable { // Static Constructors ////////////////////////////////////////////////////////////////////////////// + public abstract Sequence and(Sequence s); + public abstract Sequence not(Sequence s); + + private void needs(Sequence s) { s.needed.add(this); needs.add(s); } + private void hates(Sequence s) { s.hated.add(this); hates.add(s); } + /** the empty sequence (matches the empty string) */ public static final Sequence empty = new Sequence.Constant.Empty(); @@ -81,13 +87,21 @@ public abstract class Sequence extends Element implements Iterable { return epsilonForm; } - protected abstract Forest postReduce(Token.Location loc, Forest[] args); + protected abstract Forest postReduce(Input.Location loc, Forest[] args); // 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 +114,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,14 +127,14 @@ 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) { + final Forest rewrite(Input.Location loc) { if (this==firstp()) return epsilonForm(); return rewrite2(loc); } - final Forest rewrite2(Token.Location loc) { + final Forest rewrite2(Input.Location loc) { for(int i=0; i { 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 ////////////////////////////////////////////////////////////////////////////// @@ -169,8 +180,10 @@ public abstract class Sequence extends Element implements Iterable { static class Constant extends Sequence { 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); + public Sequence and(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.hates(s); return ret; } + public Forest postReduce(Input.Location loc, Forest[] args) { + return (Forest)Forest.leaf(loc, result); } static class Drop extends Constant { public Drop(Element[] e, HashSet and, HashSet not, boolean lame) { @@ -185,22 +198,26 @@ 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(Input.Location loc, Forest[] args) { return (Forest)Forest.singleton(loc, args[idx]); } + public Sequence and(Sequence s) { Sequence ret = new Singleton(elements, idx, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new Singleton(elements, idx, needs, hates); ret.hates(s); return ret; } } public static class Unwrap extends Sequence { private boolean[] drops; public Unwrap(Element[] e, HashSet and, HashSet not) { super(e, and, not); this.drops = null; } 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) { + public Sequence and(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.hates(s); return ret; } + public Forest postReduce(Input.Location loc, Forest[] args) { for(int i=0; i[] args2 = new Forest[count]; int j = 0; for(int i=0; i { /*private*/public final Object tag; private final boolean[] drops; private int count = 0; + public Sequence and(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops, needs, hates); ret.hates(s); return ret; } public RewritingSequence(Object tag, Element[] e, HashSet and, HashSet not) { this(tag, e, null, and, not); } public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet and, HashSet not) { super(e, and, not); @@ -215,12 +234,12 @@ public abstract class Sequence extends Element implements Iterable { this.drops = drops == null ? new boolean[e.length] : drops; for(int i=0; i Forest postReduce(Token.Location loc, Forest[] args) { + public Forest postReduce(Input.Location loc, Forest[] args) { Forest[] args2 = new Forest[count]; int j = 0; for(int i=0; i