From: adam Date: Mon, 2 Jan 2006 06:52:26 +0000 (-0500) Subject: refactoring to eliminate Token.result() X-Git-Tag: tag_for_25-Mar~470 X-Git-Url: http://git.megacz.com/?p=sbp.git;a=commitdiff_plain;h=96a2822a729e563a64173f22dc184bc972a200ef;ds=sidebyside refactoring to eliminate Token.result() darcs-hash:20060102065226-5007d-b8a300317328f76042b43c170df7e8569328f5bb.gz --- diff --git a/src/edu/berkeley/sbp/Atom.java b/src/edu/berkeley/sbp/Atom.java index 3b7ce1f..aac834b 100644 --- a/src/edu/berkeley/sbp/Atom.java +++ b/src/edu/berkeley/sbp/Atom.java @@ -11,21 +11,13 @@ import edu.berkeley.sbp.*; public abstract class Atom extends Element implements Topology { protected abstract Topology top(); + public abstract String toString(); public Topology toAtom() { return this; } - /** equality is based on the underlying Topology */ - public int hashCode() { return top().hashCode(); } - - /** equality is based on the underlying Topology */ - public boolean equals(Object o) { return o != null && o instanceof Atom && ((Atom)o).top().equals(top()); } - - /** declared abstract to force subclasses to override it in a useful manner */ - public abstract String toString(); - // Topology Thunks ////////////////////////////////////////////////////////////////////////////// - public Topology unwrap() { return top().unwrap(); } + public Topology unwrap() { return top().unwrap(); } public Topology empty() { return top().empty(); } public boolean contains(T v) { return top().contains(v); } public Topology intersect(Topology t) { return top().intersect(t); } @@ -34,6 +26,8 @@ public abstract class Atom extends Element implements Topology< public Topology complement() { return top().complement(); } public boolean disjoint(Topology t) { return top().disjoint(t); } public boolean containsAll(Topology t) { return top().containsAll(t); } + public int hashCode() { return top().hashCode(); } + public boolean equals(Object o) { return o != null && o instanceof Atom && ((Atom)o).top().equals(top()); } // Subclasses ////////////////////////////////////////////////////////////////////////////// diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index 13ac199..8587dcb 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -24,7 +24,7 @@ public abstract class Forest { static Forest singleton(Token.Location loc, Sequence creator) { return create(loc, null, new Forest[] { }, creator, false, true); } static Forest singleton(Token.Location loc, Forest body, Sequence creator) { return create(loc, null, new Forest[] { body }, creator, false, true); } static Forest leaf(Token.Location loc, T tag, Sequence creator) { return create(loc, tag, null, creator, false, false); } - static Forest create(Token.Location loc, T tag, Forest[] tokens, Sequence creator, boolean unwrap, boolean singleton) { + public static Forest create(Token.Location loc, T tag, Forest[] tokens, Sequence creator, boolean unwrap, boolean singleton) { return new MultiForest(loc, tag, tokens, creator, unwrap, singleton); } diff --git a/src/edu/berkeley/sbp/GSS.java b/src/edu/berkeley/sbp/GSS.java index d8c0fd4..2d9f6ff 100644 --- a/src/edu/berkeley/sbp/GSS.java +++ b/src/edu/berkeley/sbp/GSS.java @@ -123,7 +123,7 @@ class GSS { } /** perform all shift operations, adding promoted nodes to next */ - public void shift(Phase next) { + public void shift(Phase next, Forest result) { closed = true; Forest res = null; boolean ok = false; @@ -137,7 +137,7 @@ class GSS { if (!n.holder.valid()) continue; if (token == null) continue; for(Parser.Table.State st : n.state.getShifts(token)) { - if (res == null) res = Forest.create(token.getLocation(), token.result(), null, null, false, false); + if (res == null) res = result; next.newNode(n, res, st, true, this); ok = true; } diff --git a/src/edu/berkeley/sbp/Parser.java b/src/edu/berkeley/sbp/Parser.java index ab06889..6bf274d 100644 --- a/src/edu/berkeley/sbp/Parser.java +++ b/src/edu/berkeley/sbp/Parser.java @@ -9,7 +9,7 @@ import java.util.*; import java.lang.reflect.*; /** a parser which translates streams of Tokens of type T into a Forest */ -public class Parser { +public abstract class Parser { private final Table pt; @@ -24,16 +24,15 @@ public class Parser { if (p.element() != null) reachable(p.element(), h); } - //public Parser( Topology top) { this(new Table( top)); } - //public Parser(String s, Topology top) { this(new Table(s, top)); } - /** * create a parser to parse the grammar with start symbol u - * @param top a "sample" Topology that can be cloned (FIXME, demanding this is lame) */ - public Parser(Union u, Topology top) { this(new Table(u, top)); } + protected Parser(Union u) { this.pt = new Table(u, top()); } + protected Parser(Table pt) { this.pt = pt; } + + public abstract Forest shiftedToken(T t); + public abstract Topology top(); - Parser(Table pt) { this.pt = pt; } /** parse input for a exactly one unique result, throwing Ambiguous if not unique or Failed if none */ public Tree parse1(Token.Stream input) throws IOException, Failed, Ambiguous { return parse(input).expand1(); } @@ -46,7 +45,8 @@ public class Parser { for(;;) { GSS.Phase next = gss.new Phase(current, input.next()); current.reduce(); - current.shift(next); + Forest forest = current.token==null ? null : shiftedToken((T)current.token); + current.shift(next, forest); if (current.isDone()) return (Forest)current.finalResult; current.checkFailure(); current = next; @@ -80,12 +80,13 @@ public class Parser { // Table ////////////////////////////////////////////////////////////////////////////// + static class Top extends Union { public Top() { super("0"); } } + /** an SLR(1) parse table which may contain conflicts */ static class Table { private final Union start0 = new Top(); private final Sequence start0seq; - static class Top extends Union { public Top() { super("0"); } } public final Walk.Cache cache = new Walk.Cache(); diff --git a/src/edu/berkeley/sbp/Token.java b/src/edu/berkeley/sbp/Token.java index 196f5ca..07edde3 100644 --- a/src/edu/berkeley/sbp/Token.java +++ b/src/edu/berkeley/sbp/Token.java @@ -10,10 +10,6 @@ import edu.berkeley.sbp.*; /** a token of input -- note that this represents an actual input token rather than an Element which matches a token */ public interface Token { - // FIXME!!! remove this - /** converts this Token into a standalone result (ie for a non-rewriting pattern) */ - public String result(); - /** this is declared abstract as a way of forcing subclasses to provide a thoughtful implementation */ public abstract String toString(); diff --git a/src/edu/berkeley/sbp/Walk.java b/src/edu/berkeley/sbp/Walk.java index 4f133a4..f8f8d4f 100644 --- a/src/edu/berkeley/sbp/Walk.java +++ b/src/edu/berkeley/sbp/Walk.java @@ -138,10 +138,10 @@ abstract class Walk { eof = false; cs = cso.empty(); - if (e instanceof Parser.Table.Top) eof = true; + if (e instanceof Parser.Top) eof = true; for(Element x : all) { boolean matched = false; - if (x instanceof Parser.Table.Top) walk(x); // because this symbol might not appear in any other Sequence + if (x instanceof Parser.Top) walk(x); // because this symbol might not appear in any other Sequence if (!(x instanceof Sequence)) continue; Sequence a = (Sequence)x; Position mp = null; diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java index 143c708..eba6cbe 100644 --- a/src/edu/berkeley/sbp/misc/CharToken.java +++ b/src/edu/berkeley/sbp/misc/CharToken.java @@ -13,6 +13,14 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable { // Public ////////////////////////////////////////////////////////////////////////////// + public static class CharToStringParser extends Parser { + public CharToStringParser(Union u) { super(u); } + public Topology top() { return new IntegerTopology(); } + public Forest shiftedToken(CharToken ct) { + return Forest.create(ct.getLocation(), ct.result(), null, null, false, false); + } + } + public static class CharRange extends Atom { private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); } private Topology t; diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index fee813d..f6994ce 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -274,7 +274,7 @@ public class MetaGrammar extends StringWalker { } out.append("\n // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n"); - new Parser(MetaGrammar.make(), CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(args[0])))).toJava(out); + new CharToken.CharToStringParser(MetaGrammar.make()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(args[0])))).toJava(out); out.append("\n // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n"); for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n"); diff --git a/src/edu/berkeley/sbp/misc/RegressionTests.java b/src/edu/berkeley/sbp/misc/RegressionTests.java index 1fd97f1..9429b69 100644 --- a/src/edu/berkeley/sbp/misc/RegressionTests.java +++ b/src/edu/berkeley/sbp/misc/RegressionTests.java @@ -22,16 +22,16 @@ public class RegressionTests { //MetaGrammar mg0 = new MetaGrammar(); //mg0.walk(MetaGrammar.meta); //System.out.println(mg0); - Tree res = new Parser(MetaGrammar.make(), CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(s[0])))); + Tree res = new CharToken.CharToStringParser(MetaGrammar.make()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(s[0])))); MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res); //System.out.println(mg); Union meta = mg.done(); SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1])); - res = new Parser(meta, CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(sis), "parsing " + s[1] + " using " + s[0])); + res = new CharToken.CharToStringParser(meta).parse1(new CharToken.Stream(new InputStreamReader(sis), "parsing " + s[1] + " using " + s[0])); Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts"); if (testcasegrammar==null) return; CharToken.Stream cs = new CharToken.Stream(new InputStreamReader(new FileInputStream(s[2])), "parsing " + s[2] + " using " + s[1]); - Parser parser = new Parser(testcasegrammar, CharToken.top()); + Parser parser = new CharToken.CharToStringParser(testcasegrammar); if (profile) { System.out.println("\nready..."); @@ -74,7 +74,7 @@ public class RegressionTests { return ret; } public boolean execute() throws Exception { - Forest res = new Parser(grammar, CharToken.top()).parse(inp); + Forest res = new CharToken.CharToStringParser(grammar).parse(inp); Collection> results = res==null ? new HashSet>() : res.expand(false); System.out.print("\r"); if (results.size() == 0 && output.length > 0) { diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index b44678f..f519729 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -14,13 +14,13 @@ public class TibDoc { public static void main(String[] s) throws Exception { System.out.println("parsing " + s[0]); - Tree res = new Parser(MetaGrammar.make(), CharToken.top()).parse1(new CharToken.Stream(new FileInputStream(s[0]))); + Tree res = new CharToken.CharToStringParser(MetaGrammar.make()).parse1(new CharToken.Stream(new FileInputStream(s[0]))); MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res); //System.out.println(gram); Union mg = gram.done(); System.out.println("\nparsing " + s[1]); - res = new Parser(mg, CharToken.top()).parse1(new Tib(new FileInputStream(s[1]))); + res = new CharToken.CharToStringParser(mg).parse1(new Tib(new FileInputStream(s[1]))); System.out.println(res); }