ANTLR4 parse tree simplification

前端 未结 2 428
盖世英雄少女心
盖世英雄少女心 2021-01-07 01:11

Is there any means to get ANTLR4 to automatically remove redundant nodes in generated parse trees?

More specifically, I\'ve been experimenting with a grammar for GLS

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 01:35

    Use the Visitor implementation to access each node in sequence. Build your own tree by adding nodes to parents as they are visited. Decide at the time the node is visited whether to add it to your new tree or not. For example:

    public T visitExpression(@NotNull AcParser.ExpressionContext ctx) {
            // Expressionable parent = getParent(Expressionable.class, ctx);
            // Class expClass = AcExpression.class;
            AcExpression obj = null;
            String text = ctx.getText();
    
            //do something with text or children
            for (int i=0; i

提交回复
热议问题