X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=adf93fa64d35b8c645d7e3c72928f8585fdf2e03;hp=fcff530b8db6eaff5009d3943797c80d29d031d2;hb=c366dacc334fe2e35835164f5a37d3eebb2ca6d5;hpb=0a0227b9180534d2a431f3d6e08a398bde2244c4 diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index fcff530..adf93fa 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -35,11 +35,23 @@ public abstract class Sequence extends Element implements Iterable { //////////////////////////////////////////////////////////////////////////////// + public Element noFollow = null; + public String name = null; + public void setName(String name) { this.name = name; } + public final Topology noFollow() { return noFollow==null ? null : noFollow.toAtom(); } + + Topology toAtom() { + if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this); + return elements[0].toAtom(); + } + protected final Element[] elements; + HashSet needed; + HashSet hated; final HashSet needs; final HashSet hates; - boolean lame = false; + public boolean lame = false; final Position firstp; Position firstp() { return firstp; } @@ -48,15 +60,26 @@ public abstract class Sequence extends Element implements Iterable { protected Sequence(Element[] elements, HashSet and, HashSet not) { this.needs = and==null ? new HashSet() : and; this.hates = not==null ? new HashSet() : not; - //for(Sequence s : needs) s.lame = true; - //for(Sequence s : hates) s.lame = true; + if (this.needs != null) + for(Sequence s : this.needs) + (s.needed==null?(s.needed=new HashSet()):s.needed).add(this); + if (this.hates != null) + for(Sequence s : this.hates) + (s.hated==null?(s.hated=new HashSet()):s.hated).add(this); this.elements = elements; this.firstp = new Position(0); } - void reachable(HashSet h) { firstp().reachable(h); } - - Forest epsilonForm() { return firstp().rewrite(null); } + // DO NOT MESS WITH THE FOLLOWING LINE!!! + private Forest.Ref epsilonForm = null; + private boolean eps = false; + Forest epsilonForm() { + if (epsilonForm==null) { + epsilonForm = new Forest.Ref(); + epsilonForm.merge(firstp().rewrite2(null)); + } + return epsilonForm; + } protected abstract Forest postReduce(Token.Location loc, Forest[] args); @@ -66,12 +89,6 @@ public abstract class Sequence extends Element implements Iterable { /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */ public class Position { - void reachable(HashSet h) { - if (h.contains(this)) return; - h.add(this); - if (element() != null) element().reachable(h); - } - final int pos; private final Position next; final Forest[] holder; @@ -103,8 +120,17 @@ public abstract class Sequence extends Element implements Iterable { // Reduction ///////////////////////////////////////////////////////////////////////////////// - Forest rewrite(Token.Location loc) { - for(int i=pos; i Forest rewrite(Token.Location loc) { + if (this==firstp()) return epsilonForm(); + return rewrite2(loc); + } + + final Forest rewrite2(Token.Location loc) { + for(int i=0; i ret = Sequence.this.postReduce(loc, holder); for(int k=0; k { 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) { @@ -159,26 +185,27 @@ 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]); } } - static class Unwrap extends Sequence { + 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) { - if (drops==null) return Forest.create(loc, null, args, this, true, false); + for(int i=0; i[] args2 = new Forest[count]; int j = 0; for(int i=0; i and, HashSet not) { this(tag, e, null, and, not); } @@ -192,7 +219,8 @@ public abstract class Sequence extends Element implements Iterable { Forest[] args2 = new Forest[count]; int j = 0; for(int i=0; i