antlr/tool/playground/TestT.java

32 lines
1018 B
Java
Raw Normal View History

import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
public class TestT {
public static void main(String[] args) throws Exception {
TLexer t = new TLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(t);
// tokens.fill();
// for (Object tok : tokens.getTokens()) {
// System.out.println(tok);
// }
TParser p = new TParser(tokens);
p.setBuildParseTree(true);
TParser.sContext tree = p.s();
System.out.println(tree.toStringTree(p));
ParseTreeWalker walker = new ParseTreeWalker();
TListener listener = new BlankTListener() {
public void enterEveryRule(ParserRuleContext ctx) {
System.out.println("enter rule "+TParser.ruleNames[ctx.ruleIndex]);
}
public void exitRule(TParser.DoIfContext ctx) { // specific to rule ifstat
System.out.println("exit rule ifstat");
}
};
walker.walk(listener, tree);
}
}