In the process of using ANTLR4 to write multiple programming language code to generate an abstract syntax tree(AST) in JSON format, I overwrote the Listener class to access the ast one by one. Now when I extend other languages, I find that the MyListener class I overwrote inherits the listener base class of my own custom syntax MyCGrammer, which is MyCGrammerBaseListener.
public class MyListener extends MyCGrammerBaseListener{
public String oFile=new String();
public Stack<ASTNode> NodeStack=new Stack<>();
@Override public void enterCompilationUnit(MyCGrammerParser.CompilationUnitContext ctx) {
ASTCompilationUnit node = new ASTCompilationUnit();
NodeStack.push(node);
}
@Override public void exitCompilationUnit(MyCGrammerParser.CompilationUnitContext ctx) {
ASTNode node=NodeStack.pop();
node.children.addAll(((ASTCompilationUnit)node).items);
pilationUnitContext MyCGrammerBaseListener and MyCGrammerParser.Com in the code above are my own test with antlr grammar compiled libraries, unable to migrate to other languages.
I had tried to put the MyCGrammerParser to org. Antlr. V4. Runtime. The Parser, because the former is inherited from the latter, But after the change org. Antlr. V4. Runtime. The Parser is not CompilationUnitContext method.
Does anyone know whether ANTLR listener has a common base class, which can be easily extended for multiple languages.(It is possible to pass different language Parser,Lexer, and Listener objects in the actual calling function, defined with their common base class)
0 Answer
No answer yet
这家伙很懒,什么都没留下...