ANTLR4: Obtaining list of tokens for a specific rule in Listener
Asked Answered
D

1

6

I am extending a Listener in ANTLR4 and I want to get all of the tokens which are associated with a particular rule in the parser, is there a method in doing that?

i.e.

myConfiguration: CONFIG EQUALS parameters ;
parameters: ALPHANUMERIC+

CONFIG: 'config' ;
ALPHANUMERIC: [a-zA-Z0-9] ;

How can I tell my listener to lookup the value of CONFIG and EQUALS when entering the myConfiguration parsing rule?

Is there a for loop of some sort I could use?

for( all tokens in this rule) {
    System.out.println(token.getText());
}

I can see that there is a list of tokens through the parser class but I cant find a list of tokens which are associated with the current rule.

The reason why I am asking this is so that I can avoid re-typing the token names which I require in the Listener AND in the grammar. By doing that I can check whether or not each token type in that particular rule has been found without having to type in the name manually.

Deming answered 21/2, 2013 at 16:0 Comment(0)
B
8

This might be what you're looking for.

List<TerminalNode> terminalNodes = new ArrayList<TerminalNode>();
for (int i = 0; i < context.getChildCount(); i++) {
    if (context.getChild(i) instanceof TerminalNode) {
        terminalNodes.add((TerminalNode)context.getChild(i));
    }
}
Ballista answered 21/2, 2013 at 18:33 Comment(2)
I tried out the code and it gave me the values of the tokens found in the parse tree. I was looking for a way to get the constant token names available in a parse rule, which if possible should be obtainable before walking the parse tree. I.e. the myConfiguration rule should give me the tokens of: CONFIG EQUALS parameters -- in the form of tokens instead of values which I can extract their names from. The example given above depends on the input instead of the lexer. I am looking for the actual tokens that can be matched prior to matching anything for a specific rule.Deming
This seems to be the only way to make the listener as independent as possible from the grammar.Deming

© 2022 - 2024 — McMap. All rights reserved.