* analyses depend on which elements *reference* a given element,
* rather than which elements *are referenced by* a given element.
*
- * This class is package-private because it is likely to change often.
+ * All members of this class are package-private because they are
+ * likely to change often.
*/
-abstract class Grammar<Token> {
+public abstract class Grammar<Token> {
protected Union rootUnion;
- public HashMap<Sequence, Topology> follow = new HashMap<Sequence, Topology>();
- public HashSet<Sequence> followEof = new HashSet<Sequence>();
- public final HashMap<SequenceOrElement,Boolean> possiblyEpsilon = new HashMap<SequenceOrElement,Boolean>();
- public HashSet<SequenceOrElement> all = new HashSet<SequenceOrElement>();
+ HashMap<Sequence, Topology> follow = new HashMap<Sequence, Topology>();
+ HashSet<Sequence> followEof = new HashSet<Sequence>();
+ final HashMap<SequenceOrElement,Boolean> possiblyEpsilon = new HashMap<SequenceOrElement,Boolean>();
+ HashSet<SequenceOrElement> all = new HashSet<SequenceOrElement>();
abstract Topology<Token> emptyTopology();
- public Grammar(Union root) {
+ Grammar(Union root) {
this.rootUnion = root;
if (root != null)
for(Sequence s : root)
// Follow //////////////////////////////////////////////////////////////////////////////
- public Topology follow(Sequence s) { return follow.get(s); }
+ Topology follow(Sequence s) { return follow.get(s); }
- public void buildFollowSet(Sequence seq, Topology followTopology, boolean eof) {
+ void buildFollowSet(Sequence seq, Topology followTopology, boolean eof) {
all.add(seq);
Topology old = follow.get(seq);
if (old==null) old = followTopology;
}
}
- public Topology epsilonFollowSet(Union u) {
+ Topology epsilonFollowSet(Union u) {
Topology ret = emptyTopology();
for(Sequence s : u)
ret = ret.union(epsilonFollowSet(s, new HashSet<Sequence>()));
return ret;
}
- public Topology epsilonFollowSet(Sequence seq, HashSet<Sequence> seen) {
+ Topology epsilonFollowSet(Sequence seq, HashSet<Sequence> seen) {
Topology ret = seq.follow==null ? emptyTopology().complement() : seq.follow.getTokenTopology();
if (seen.contains(seq)) return ret;
seen.add(seq);
return ret;
}
- public Topology first(SequenceOrElement e, HashSet<Sequence> seen) {
+ Topology first(SequenceOrElement e, HashSet<Sequence> seen) {
if (e instanceof Atom) return ((Atom)e).getTokenTopology();
Topology ret = emptyTopology();
if (e instanceof Union) {
return ret;
}
- public boolean isRightNullable(Pos p) {
+ boolean isRightNullable(Pos p) {
if (p.isLast()) return true;
if (!possiblyEpsilon(p.element())) return false;
return isRightNullable(p.next());
}
- public boolean isNulledSubsequence(Sequence parent, Sequence potentialSubSequence) {
+ boolean isNulledSubsequence(Sequence parent, Sequence potentialSubSequence) {
HashSet<Sequence> eq = new HashSet<Sequence>();
gatherNulledSubsequences(parent, eq);
return eq.contains(potentialSubSequence);
// Pos Ordinality Comparisons //////////////////////////////////////////////////////////////////////////////
- public boolean canKill(Pos mep, Pos himp) {
+ boolean canKill(Pos mep, Pos himp) {
if (!isRightNullable(mep)) return false;
if (!isRightNullable(himp)) return false;
Sequence me = mep.owner();
return false;
}
- public boolean canNeed(Pos mep, Pos himp) {
+ boolean canNeed(Pos mep, Pos himp) {
if (!isRightNullable(mep)) return false;
if (!isRightNullable(himp)) return false;
Sequence me = mep.owner();
return false;
}
- public int comparePositions(Pos position, Pos rposition) {
+ int comparePositions(Pos position, Pos rposition) {
int ret = 0;
if (canKill(position, rposition) && canKill(rposition, position)) throw new Error();
if (canKill(position, rposition)) ret = -1;
import java.io.*;
public class CommandLine {
- /*
public static void main(String[] argv) throws Throwable {
String grammarFile = argv[0];
String targetFile = argv[1];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
System.setErr(new PrintStream(baos));
- Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(grammarFile)).expand1();
- Union meta = Grammar.create(res, "s",
- new Grammar.Bindings() {
- //public Sequence createSequence(Production p) {
- //Sequence ret = super.createSequence(p);
- //if (ret != null) return ret;
- //return Sequence.create(p.nonTerminal, p.elements, p.drops, false);
- //}
- });
+ Tree<String> res = new CharParser(MetaGrammar.getMetaGrammar()).parse(new FileInputStream(grammarFile)).expand1();
+ Union meta = GrammarAST.buildFromAST(res, "s", null);
CharInput input = new CharInput(new FileInputStream(targetFile), "", true);
Tree ret = new CharParser(meta).parse(input).expand1();
if (ret==null) throw new NullPointerException("CharParser returned null");
System.exit(-1);
}
}
- */
}