[antlr-interest] How does one handle variable number of function parameters?

Rob Greene robgreene at gmail.com
Tue Nov 29 05:43:07 PST 2005


> A function call sounds like it looks something like this:
>
>    function_call : ID LPAREN^ args RPAREN!;
>    args  :  expr ( COMMA! expr )* { ## = #( #[ARGLIST,"ARGLIST"], ## ); } ;
>
> Is this the place you need help, or is it something after this? Once
> you have it parsed, where do you need to go?

Yes, I think this is where I need help. I've currently got the
function defined as this (identifier piece, of course):

term
	: NUMBER
	| LPAREN! expression RPAREN!
	| IDENTIFIER^ ( LPAREN! expression (COMMA! expression)* RPAREN! )?
	;

I see:
1) You've made the LPAREN the root - why? I figured I'd have the
IDENTIFIER as the root with the expressions as the children. Probaby
doesn't matter if I get the tree parser setup right...?

2) What the heck does this mean? I've seen it mentioned in the ANTLR
documentation, but I didn't grok it.
    args  :  expr ( COMMA! expr )* { ## = #( #[ARGLIST,"ARGLIST"], ## ); } ;

3) I'd like to have the parenthesis optional. Do I want to have two
definitions to pick up the parenthesis in the parser grammar file?
	| (IDENTIFIER LPARN) => IDENTIFIER^ LPAREN! expression (COMMA!
expression)* RPAREN!
	| IDENTIFIER^

4) and, finally, I need to figure out how to "process" this. With my
expression parser, I'm basically doing an interpreter, so that seems
to make everything a lot simpler. All the other functions are working
(you'll see it at the bottom of the expression.g file.

I've re-attached my current grammar and hacking file just in case that
helps. Note that I think the Main class will hold the function
arguments as they get evaluated and then they will be available for
the function call itself.

Thanks again! This is really neat - but I keep on missing important tidbits. :-(
-Rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: expression.g
Type: application/octet-stream
Size: 2813 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20051129/41c32137/expression.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Main.java
Type: application/octet-stream
Size: 1172 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20051129/41c32137/Main.obj


More information about the antlr-interest mailing list