I\'m trying to use ASTs with ANTLR4, with this files:
Builder.java
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
First I'll explain what you have observed above:
First and foremost, please read the documentation for the methods you call. The Parser.addParseListener documentation includes the following note:
THIS IS ONLY FOR ADVANCED USERS. Please give your ParseTreeListener to a ParseTreeWalker instead of giving it to the parser!!!!
The implementation of toString()
for the ParserRuleContext
class simply prints the rule invocation stack at the time the context was created. You are printing this once when the listener enters a rule, and once when it exits. For actionexpr
, cond
, and condexpr
you print it again, resulting in a total of 4 identical output lines for each of those contexts.
Now some notes on your goals:
enterCond
and exitCond
, the MEASURE
text is available by calling ctx.MEASURE().getText()
.enterActionexpr
and exitActionexpr
, the ACTION
text is available by calling ctx.ACTION().getText()
.COND
token by creating a new TerminalNodeImpl
and CommonToken
for the updated token, and assigning it to the correct index in the field CondContext.children
using either a visitor or a listener.A tree label you could use, to set the context of the parse and then walks the leaves of the observed graph with a visitor class and trigger methods in order to create actions from the expressions in the language source code. Thus, at the initial visitation, the listener does not process the actual visitor pattern. The actual visitor pattern and the processing through visitation is done by the methods of the expressionbase listener class extension.
The listener identifies the expression:
@Override public void enterListener(ExprParser.EXPR_CONTEXTContext ctx) {
//some code to view the compilation process
}
The expression rule gets a name label:
'EXPR_CONTEXT' expression # EXPR_CONTEXT //the tree label
The expression walker is implemented:
public class ExprWalker extends ExprBaseListener {
@Override
public void enterListener(ExprParser.EXPR_CONTEXTContext ctx) {
java.util.List<ExprParser.ExpressionContext> e = ctx.expression();
System.out.println("EXPRESSION: " //print action
+ e.get(0).getText() + ", " //first element
+ e.get(1).getText() //second element
+ ", " + ... + ", " //number of elements
+ e.get(N).getText()); //last element
}
The main file then walks with the walker:
ParseTree tree = parser.parse(); //parse the tree
intermezzo: before applying the walker visitation pattern one may imagine tree segment optimisation- or processing patterns. The parsed tree could be handled here as a seperate induction of the source code tree. This approach enables more complicated code- and tree processing patterns.
ParseTreeWalker walker = new ParseTreeWalker(); //get the walker
walker.walk(new ExprWalker(), tree); //start visiting