1
votes

Question: is there a (more straightforward) way of building custom parse trees at parse time in ANTLR v4?

I guess that one could traverse and rewrite the automatically built tree but I was wondering if we can still do manual tree building (or tweaking, for that matter) at parse time (similar to ANTLR v3 and ealier). The idea is that, depending on how one writes his/her grammar, we get a lot of useless nodes in the ANTLR-built tree and while I get it that you can override only the listener methods that interest you, one still has to check and skip useless token types, etc.

2

2 Answers

3
votes

No, our experience with ANTLR 3 is the manual AST feature inevitably resulted in code which was more difficult to maintain and understand, leading to a high rate of regression bugs for developers making any change to a grammar. Tokens are no longer omitted from the tree since it's difficult to tell which terminals will be needed by future releases of an application, and you don't want to have to change/verify all of your code operating on the parse tree if a terminal which was previously unused is now required by a new component or feature.

2
votes

You can override org.antlr.v4.runtime.Parser.addContextToParseTree() to get some control over what nodes get created. Not sure this is quite what you mean by custom.

@parser::members {

@Override
protected void addContextToParseTree() {
    // code is a rule enabled by semantic predicate 'full' 
    // that matches generic lines of code.
    if(!full && _ctx instanceof CodeContext){
        return;
    }

    // otherwise add the node to the tree..
    super.addContextToParseTree();
}

}