Siva B wrote:
> Hi all,
>
> I have a problem when I am doing a function call with unknown number of
> parameters.
>
> suppose when I am writing grammar for sum of listed elements(no of elements
> not fixed i mean an array of elements)
> my code
>
> functionCall : 'sum' '(' arg=expr {arguments=list();
> arguments.append($arg.value); } (',' e=expr {arguments.append($e.value);})*
> ')' { print sum(arguments);};
>
> this works fine with sum(1,2,3)
> and also works fine with sum(1,2,3,)
Something like this should work (untested):
functionCall
@init { arguments = list(); }
: 'sum' '(' ( restOfArguments[arguments] | ')' )
{ print sum(arguments); }
;
restOfArguments[arguments]
: e=expr {arguments.append($e.value);} ( ',' restOfArguments[arguments]
| ','? ')' )
;
(I'm assuming you want to allow zero arguments. If not, delete the "| ')'"
alternative from functionCall.)
--
David-Sarah Hopwood ⚥ http://davidsarah.livejournal.com
signature.asc
Description: OpenPGP digital signature
List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
