how to use antlr4 visitor

后端 未结 1 413

I am a beginner of antlr. I was trying to use visitor in my code and following the instruction on the net. However, I found out that the visitor was not entering the method

1条回答
  •  醉梦人生
    2020-12-30 06:20

    You have to call super if you want ANTLR4 to visit children. Like this:

    @Override 
    public Integer visitPgm_body(@NotNull MicroParser.Pgm_bodyContext ctx){
        super.visitPgm_body(ctx);
        System.out.println(ctx.getText());
        return 467;
    }
    
    @Override
    public Integer visitProgram(@NotNull MicroParser.ProgramContext ctx){
        super.visitProgram(ctx);
        System.out.println("11");
        return 456;
    }
    

    You have to think about where to put your logic: before super or after.

    0 讨论(0)
提交回复
热议问题