X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=3420ad5ca328f4c2b9692489313a1c39304a43e0;hp=f5fff5d32d5cb9fc955731b90f2ba73de0b5b074;hb=98ae7592861f7e7d3bf5744b5db84ffdefc6011d;hpb=35698bf08ba446ffe96917e8964b5bc43404eda3 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index f5fff5d..3420ad5 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -39,42 +39,54 @@ public class MetaGrammar extends StringWalker { public static class BuildContext extends HashMap { private final Meta.MetaGrammarFile mgf; + public Meta.NonTerminal currentNonTerminal; public BuildContext(Meta.MetaGrammarFile mgf) { this.mgf = mgf; } public Union build(String s) { Union ret = get(s); if (ret != null) return ret; - Meta.MetaNonterminal mnt = mgf.get(s); + Meta.NonTerminal mnt = mgf.get(s); if (mnt==null) throw new Error("undeclared nonterminal \""+s+"\""); return mnt.build(this); } } public static class Meta { - public class MetaGrammarFile extends HashMap { + public Sequence resolveTag(String s, String nonTerminalName, Element[] els, Object[] labels, boolean [] drops) { + return Sequence.rewritingSequence(s, els, labels, drops); + } + public class MetaGrammarFile extends HashMap { public MetaGrammarFile(Tree tree) { if (!tree.head().equals("grammar")) throw new Error(); for(Tree nt : tree.child(0)) - add(new MetaNonterminal(nt)); + add(new NonTerminal(nt)); } - private void add(MetaNonterminal mnt) { + private void add(NonTerminal mnt) { if (this.get(mnt.name)!=null) throw new Error("duplicate definition of nonterminal \""+mnt.name+"\""); this.put(mnt.name, mnt); } public String toString() { String ret = ""; - for(MetaNonterminal mnt : this.values()) ret += mnt + "\n"; + for(NonTerminal mnt : this.values()) ret += mnt + "\n"; return ret; } } - public class MetaNonterminal { + public class NonTerminal { public String name; public MetaUnion rhs; - public MetaNonterminal(Tree tree) { + public NonTerminal(Tree tree) { name = string(tree.child(0)); rhs = rhs(tree.child(1)); } public String toString() { return name + " = " + rhs; } - public Union build(BuildContext bc) { return rhs.build(bc, name); } + public Union build(BuildContext bc) { + NonTerminal ont = bc.currentNonTerminal; + bc.currentNonTerminal = this; + try { + return rhs.build(bc, name); + } finally { + bc.currentNonTerminal = ont; + } + } } public MetaUnion rhs(Tree t) { return t.numChildren()==1 @@ -137,9 +149,9 @@ public class MetaGrammar extends StringWalker { } public MetaSequence makeMetaSequence(Tree t) { - if ("psx".equals(t.head())) return makeMetaConjunct(t.child(0)); - if (t.head().equals("&")) return new MetaAnd(makeMetaSequence(t.child(0)), makeMetaConjunct(t.child(1)), false); - if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), makeMetaConjunct(t.child(1)), true); + if ("psx".equals(t.head())) return makeConjunct(t.child(0)); + if (t.head().equals("&")) return new MetaAnd(makeMetaSequence(t.child(0)), makeConjunct(t.child(1)), false); + if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), makeConjunct(t.child(1)), true); return null; } @@ -166,7 +178,7 @@ public class MetaGrammar extends StringWalker { public String toString() { return left + " &"+(not?"~":"")+" "+right; } } - public class MetaConjunct implements MetaSequence { + public class Conjunct implements MetaSequence { public boolean negated = false; public MetaClause[] elements; public HashMap labelMap = new HashMap(); @@ -198,7 +210,7 @@ public class MetaGrammar extends StringWalker { if (dropAll) ret = Sequence.drop(els, false); else if (unwrap) ret = Sequence.unwrap(els, /*repeatTag FIXME*/null, drops); else if (tag!=null) { - ret = Sequence.rewritingSequence(tag, els, labels, drops); + ret = resolveTag(tag, bc.currentNonTerminal==null ? null : bc.currentNonTerminal.name, els, labels, drops); } else { int idx = -1; for(int i=0; i t) { + private Conjunct(Tree t) { elements = new MetaClause[t.numChildren()]; int i = 0; for(Tree tt : t) elements[i++] = makeMetaClause(tt, this); @@ -227,33 +239,33 @@ public class MetaGrammar extends StringWalker { return ret; } } - public MetaConjunct makeMetaConjunct(Tree t) { - //System.err.println("makeMetaConjunct("+t+")"); + public Conjunct makeConjunct(Tree t) { + //System.err.println("makeConjunct("+t+")"); if ("/".equals(t.head())) { - MetaConjunct ret = makeMetaConjunct(t.child(0)); + Conjunct ret = makeConjunct(t.child(0)); ret.separator = makeMetaClause(t.child(1), ret); return ret; } if ("->".equals(t.head())) { - MetaConjunct ret = makeMetaConjunct(t.child(0)); + Conjunct ret = makeConjunct(t.child(0)); ret.followedBy = makeMetaClause(t.child(1), ret); return ret; } if ("::".equals(t.head())) { - MetaConjunct ret = makeMetaConjunct(t.child(1)); + Conjunct ret = makeConjunct(t.child(1)); ret.tag = string(t.child(0)); return ret; } if ("ps".equals(t.head())) { - return new MetaConjunct(t.child(0)); + return new Conjunct(t.child(0)); } - return new MetaConjunct(t); + return new Conjunct(t); } - public MetaClause makeMetaClause(Tree t, MetaConjunct c) { + public MetaClause makeMetaClause(Tree t, Conjunct c) { //System.err.println("MetaClause.makeMetaClause("+t+")"); - if (t==null) return new MetaEpsilon(); - if (t.head()==null) return new MetaEpsilon(); + if (t==null) return new Epsilon(); + if (t.head()==null) return new Epsilon(); if (t.head().equals("{")) throw new Error("metatree: " + t); if (t.head().equals("*")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, null, true, true); if (t.head().equals("+")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, null, false, true); @@ -264,15 +276,15 @@ public class MetaGrammar extends StringWalker { if (t.head().equals("+/")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, makeMetaClause(t.child(1), c), false, true); if (t.head().equals("**/")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, makeMetaClause(t.child(1), c), true, true); if (t.head().equals("++/")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, makeMetaClause(t.child(1), c), false, true); - if (t.head().equals("()")) return new MetaEpsilon(); + if (t.head().equals("()")) return new Epsilon(); if (t.head().equals("[")) return new MetaRange(t.child(0)); - if (t.head().equals("literal")) return new MetaStringLiteral(t.child(0)); - if (t.head().equals("nonTerminal")) return new MetaNonterminalReference(t.child(0)); - if (t.head().equals(")")) return new MetaSelfReference(); - if (t.head().equals("(")) return new MetaParens(t.child(0)); + if (t.head().equals("literal")) return new StringLiteral(t.child(0)); + if (t.head().equals("nonTerminal")) return new NonTerminalReference(t.child(0)); + if (t.head().equals(")")) return new SelfReference(); + if (t.head().equals("(")) return new Parens(t.child(0)); if (t.head().equals("~")) return new MetaInvert(t.child(0), c); if (t.head().equals("!")) { MetaClause mc = makeMetaClause(t.child(0), c); mc.drop = true; return mc; } - if (t.head().equals("^")) { c.tag = string(t.child(0)); return new MetaStringLiteral(t.child(0)); } + if (t.head().equals("^")) { c.tag = string(t.child(0)); return new StringLiteral(t.child(0)); } if (t.head().equals("^^")) throw new Error("carets: " + t); if (t.head().equals(":")) { String name = string(t.child(0)); @@ -315,20 +327,20 @@ public class MetaGrammar extends StringWalker { (separator==null?"":(" /"+separator)); } } - public class MetaEpsilon extends MetaClause { + public class Epsilon extends MetaClause { public String toString() { return "()"; } public Element build(BuildContext bc) { return Union.epsilon; } } - public class MetaParens extends MetaClause { + public class Parens extends MetaClause { public MetaUnion body; - public MetaParens(Tree t) { this.body = rhs(t); } + public Parens(Tree t) { this.body = rhs(t); } public String toString() { return "( " + body + " )"; } public Element build(BuildContext bc) { return body.buildAnon(bc); } } /* public static class MetaTree extends MetaClause { - public MetaConjunct body; - public MetaTree(Tree t) { this.body = makeMetaConjunct(t); } + public Conjunct body; + public MetaTree(Tree t) { this.body = makeConjunct(t); } public String toString() { return "{ " + body + " }"; } public Element build(BuildContext bc) { return new Union("{}");// body.buildSequence(); @@ -350,25 +362,25 @@ public class MetaGrammar extends StringWalker { } } } - public class MetaStringLiteral extends MetaClause { + public class StringLiteral extends MetaClause { public String literal; public Element build(BuildContext bc) { return string(literal); } - public MetaStringLiteral(Tree literal) { this.literal = string(literal); this.drop = true; } + public StringLiteral(Tree literal) { this.literal = string(literal); this.drop = true; } public String toString() { return "\""+StringUtil.escapify(literal, "\"\r\n\\")+"\""; } } - public class MetaNonterminalReference extends MetaClause { + public class NonTerminalReference extends MetaClause { public String name; - public MetaNonterminalReference(Tree name) { this.name = string(name); } + public NonTerminalReference(Tree name) { this.name = string(name); } public Element build(BuildContext bc) { return bc.build(name); } public String toString() { return name; } } - public class MetaSelfReference extends MetaClause { + public class SelfReference extends MetaClause { public String toString() { return "(*)"; } public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ } } public class MetaInvert extends MetaClause { public MetaClause element; - public MetaInvert(Tree t, MetaConjunct c) { this.element = makeMetaClause(t, c); } + public MetaInvert(Tree t, Conjunct c) { this.element = makeMetaClause(t, c); } public String toString() { return "~"+element; } public Element build(BuildContext bc) { return infer((Topology)Atom.toAtom(element.build(bc)).complement()); } }