X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=2ec3f45b1ef6cbe3bdb527d288f1ebdaa1f4ab41;hp=d5a70b36bb8942a931bf7a10f35a2a81b6f835f1;hb=773d7aff52fe6678ba4e564de0bea9c4b8435e11;hpb=6f995008673ec682df49866ac2e9623397c99679 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index d5a70b3..2ec3f45 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -22,9 +22,6 @@ public class MetaGrammar extends ReflectiveWalker { private static final Union SELF = new Union("()"); - public static Union epsilon = new Union("()"); - static { epsilon.add(Sequence.empty); } - //////////////////////////////////////////////////////////////////////////////// private Union g; @@ -57,12 +54,11 @@ public class MetaGrammar extends ReflectiveWalker { public PreSequence _amp_(PreSequence p, Object[] o) { return p.and(new PreSequence(o, null).buildSequence(null, true, false, true)); } public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null).buildSequence(null, true, false, true)); } - public Object _star_(Element r) { return Repeat.many0(r); } - public Element epsilon(Object o, Object b) { return epsilon; } + public Element epsilon(Object o, Object b) { return Union.epsilon; } + public Object _leftparen__rightparen_(Object o) { return Union.epsilon; } public Element _rightparen_(Object e) { return SELF; } public Element _bang_(Element r) { return r; } public Object _caret_(String s) { return new MyLift(s); } - public Object _leftparen__rightparen_(Object o) { return epsilon; } public Union nonTerminal(String s) { return nonTerminal(s, null, false, false); } public Union _colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, false); } public Union _bang__colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, true); } @@ -102,7 +98,6 @@ public class MetaGrammar extends ReflectiveWalker { false); } - public Object _plus_(final Element r) { return Repeat.many1(r); } public PreSequence rewrite(Object[] o) { return new PreSequence(o, null); } public PreSequence seqx(PreSequence p, String tag) { return _equals__greater_(p, tag); } @@ -114,17 +109,20 @@ public class MetaGrammar extends ReflectiveWalker { public PreSequence ps(Object[] o) { return new PreSequence(o); } public PreSequence _slash_(PreSequence p, Object sep) { return p.sparse(sep); } + public Object _star_(Element r) { return Repeat.many0(r); } + public Object _plus_(final Element r) { return Repeat.many1(r); } public Object _plus__slash_(final Element r, Element s) { return Repeat.many1(r, s); } public Object _star__slash_(final Element r, Element s) { return Repeat.many0(r, s); } - public Object _star__star_(final Element r) { return Repeat.maximal(Repeat.many0(r)); } - public Object _plus__plus_(final Element r) { return Repeat.maximal(Repeat.many1(r)); } - public Element _question_(final Element r) { return Repeat.maybe(r); } + public Object _star__star_(final Element r) { return Repeat.maximal(Repeat.many0(r)); } + public Object _plus__plus_(final Element r) { return Repeat.maximal(Repeat.many1(r)); } + public Element _question_(final Element r) { return Repeat.maybe(r); } + public MetaGrammar gram(Object o, MetaGrammar g, Object o2) { return g; } public MetaGrammar grammar(Object[] o) { return this; } public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; } public char _backslash_n() { return '\n'; } public char _backslash_r() { return '\r'; } - public Object literal(String s) { return new MyDrop(string(s)); } + public Element literal(String s) { Element ret = string(s); dropAll.add(ret); return ret; } public Range _minus_(char a, char b) { return new Range(a, b); } public Range range(char a) { return new Range(a, a); } public Element _leftbracket_ (Range[] rr) { return ranges(true, rr); } @@ -155,16 +153,25 @@ public class MetaGrammar extends ReflectiveWalker { public PreSequence sparse(Object e) { Object[] ret; - if (o.length <= 1) ret = o; - else { + boolean[] drops; + if (o.length <= 1) { + ret = o; + drops = new boolean[this.drops.length]; + System.arraycopy(this.drops, 0, drops, 0, this.drops.length); + } else { ret = new Object[o.length * 2 - 1]; + drops = new boolean[o.length * 2 - 1]; for(int i=0; i set = new HashSet(); Element[] o2 = o==null ? new Element[0] : new Element[o.length]; int nonDrop = 0; - boolean[] drops = null; if (o != null) { - drops = new boolean[o.length]; int j = 0; for(int i=0; i", new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "(", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "*/", new Tree[] { new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "e", new Tree[] { })})}), + new Tree(null, null, new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "=>", new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "*/", new Tree[] { new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "e", new Tree[] { })})}), new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }), - new Tree(null, "s", new Tree[] { })})})})})})})})})})}), + new Tree(null, "s", new Tree[] { })})})})})}), new Tree(null, null, new Tree[] { new Tree(null, "p", new Tree[] { }), new Tree(null, "s", new Tree[] { })})})})})}), new Tree(null, "::=", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "p", new Tree[] { }), @@ -440,12 +452,12 @@ new Tree(null, "gram", new Tree[] { new Tree(null, null, new Tree[] { }), new Tree(null, "x", new Tree[] { })}), new Tree(null, null, new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "p", new Tree[] { }), new Tree(null, "s", new Tree[] { })})})})})}), - new Tree(null, null, new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "p", new Tree[] { }), - new Tree(null, "s", new Tree[] { })})}), - new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }), + new Tree(null, null, new Tree[] { new Tree(null, "/", new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "p", new Tree[] { }), new Tree(null, "s", new Tree[] { })})}), new Tree(null, "^", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "/", new Tree[] { })})}), - new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "e", new Tree[] { })})})})})})})}), + new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "e", new Tree[] { })})})})}), + new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }), + new Tree(null, "s", new Tree[] { })})})})})})}), new Tree(null, "::=", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "s", new Tree[] { }), new Tree(null, "e", new Tree[] { }), new Tree(null, "q", new Tree[] { }), @@ -571,10 +583,6 @@ new Tree(null, "gram", new Tree[] { new Tree(null, null, new Tree[] { }), new Tree(null, "d", new Tree[] { })})})})}), new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }), new Tree(null, "s", new Tree[] { })})})}), - new Tree(null, "/", new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "^", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "!", new Tree[] { })})}), - new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "e", new Tree[] { })})})})}), - new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }), - new Tree(null, "s", new Tree[] { })})})}), new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "(", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "e", new Tree[] { })})}), new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }), new Tree(null, "s", new Tree[] { })})}), @@ -729,3 +737,4 @@ new Tree(null, "gram", new Tree[] { new Tree(null, null, new Tree[] { }), +