Exemplo listener

import static java.lang.system.*;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;

public class ShapesMyListener extends ShapesBaseListener {
    @Override
    public void enterPoint(ShapesParser.PointContext ctx){
        int x = Integer.parseInt(ctx.x.getText());
        int y = Integer.parseInt(ctx.y.getText());
        out.println("enterPoint x=" + x + ",y=" + y);
    }
    
    @Override
    public void exitPoint(ShapesParser.PointContext ctx){
        int x = Integer.parseInt(ctx.x.getText());
        int y = Integer.parseInt(ctx.y.getText());
        out.println("enterPoint x=" + x + ",y=" + y);
    }
}

Para utilizar esta classe:

O comando antlr4-main permite a geração automática deste código no método main.antlr4-main <Grammar> <start-rule> -l <nome-da-classe-ou-fich-listener> ...

Last updated