user840718
user840718

Reputation: 1611

ANTLR - Trigger after processing one rule

I have this very simple grammar in ANTLR4:

 rule : 'SubRelationOf' LPAREN r COMMA r RPAREN
 r:     RN | 'RelationUnionOf' LPAREN (r COMMA)+ r RPAREN

 //LEXER
 RN : [a-z]+ ;
 COMMA: ',';
 LPAREN: '(';
 RPAREN: ')';

I'm also using the Listener implementation with the stack to trace every single event, since the grammar is context-free and recursive. As you can see, the rule is made of two arguments: the first r and the second r. What I strongly need, is a way to distinguish when I finish to process the first one. Since the Listener implementation has the following:

public void exitRule(Parser.RuleContext ctx) {
// do something
}

there is no way to understand when I am in the first r or in the second r of the rule. Is there a trigger that says "ok you are just out of the first r, execute your arbitrary code"? Is there a way to implement this?

Upvotes: 1

Views: 75

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53407

In rules where a subrule or token appears more than once you will have a function that returns a list instead of a single entry. So, your RuleContext has a member r() which returns a list. The order of the entries in it is what you defined in your grammar.

Upvotes: 1

Related Questions