[antlr-interest] handle extra things

David-Sarah Hopwood david-sarah at jacaranda.org
Mon Nov 30 11:44:12 PST 2009


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 292 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20091130/37b8ca7e/attachment.bin 


More information about the antlr-interest mailing list