[antlr-interest] parsing empty function parameters

Austin Hastings Austin_Hastings at Yahoo.com
Fri Oct 5 17:33:31 PDT 2007


Andy,

What's the parse tree for

f()

going to look like?


Is that a call to a function with no args, or with one unspecified arg?

=Austin

Andy Tripp wrote:
> Using ANTLR v3.0.1,
> I'm trying to parse function parameters which can be empty, like this:
> f(a, , c, d);
> g(,,,,)
>
> I have:
> argList
>   : argSpec (COMMA argSpec)*     ;
>
> But then whenever I try to allow "argSpec" to have an empty 
> alternative, ANTLR itself
> produces an OutOfMemoryError.
>
> Here's the original "argSpec" rule, which doesn't allow empty:
>
> argSpec
>   : argPassingType? argSpec2
>   ;
>
> I tried making the whole thing optional:
> argSpec
>   : (argPassingType? argSpec2)?
>   ;
>
> I tried a syntactic predicate:
> argSpec
>   : (','|')') =>                // if next token is comma or rparen, 
> nothing to consume.
>   | argPassingType? argSpec2
>   ;
>
> I tried a semantic predicate:
> argSpec
>   : {input.LT(1).getText().equals(",") || 
> input.LT(1).getText().equals(")")}?
>   | argPassingType? argSpec2
>   ;
>
> As I said, all these cause ANTLR to produce OutOfMemoryError, presumably
> in an infinite loop matching this rule, but consuming no token.
> Any suggestions?
>
>



More information about the antlr-interest mailing list