Lets suppose the following scenarios with 2 ANTLR grammars:
1)
expr : antExp+;
antExpr : '{' T '}' ;
T : 'foo';
2)
expr : antExpr;
antExpr : '{' T* '}' ;
T : 'bar';
In both cases I need to know how to iterate over antExp+ and T*, because I need to generate an ArrayList of each element of them. Of course my grammar is more complex, but I think that this example should explain what I’m needing. Thank you!
Production rules in ANTLR can have one or more return types which you can reference inside a loop (a
(...)*or(...)+). So, let’s say you want to print each of theT‘s text theantExprule matches. This could be done like this:The same principle holds for example grammar #2:
EDIT
Note that you’re not restricted to returning a single reference. Running the parser generated from:
on the input
"aaa bbb ccc"would print the following: