What is the correct way to solve this problem in ANTLR:
I have a simple grammar rule, say for a list with an arbitrary number of elements.
list
: '[]'
| '[' value (COMMA value)* ']'
If I wanted to assign a return value for list, and have that value be the actual list of returned values from the production, what is the proper way to do it? The alternatives I'm entertaining are:
I guess the question is: How do the cool kids do it?
(FYI I'm using the python API for ANTLR, but if you hit me with another language, I can handle that)
In C# it might look like this:
list returns [ List<string> ValueList ]
@init
{
$ValueList = new List<string>();
}
: '[]'
| '[' value {$ValueList.Add(value);} (COMMA value {$ValueList.Add(value);})* ']'
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With