[antlr-interest] Empty alternative causes error

Jim Idle jimi at temporal-wave.com
Mon Feb 18 14:35:44 PST 2008


My educated guess is that you are trying to write a C# parser and 
falling in to the trap of typing in the language spec too literally. The 
spec is not a spec for writing a grammar and so it creates ambiguities 
all over the place. Basically, you just cannot write it by following the 
language spec like that, you must left factor everything so it is a 
sensible grammar.

I suspect that what is happening is that when you remove the empty alt, 
a rule higher up creates the ambiguity. There is a difference between 
generic declarations and generic invocations and you also need to be 
able to distinguish between generics and the < operator in some 
circumstances. 

Finally, you don't need the CLOSE_GENERICS element of the tree you are 
building.

There are no bugs in this area that I am aware of, I am afraid that you 
probably need to rethink your grammar.

Jim




> -----Original Message-----
> From: Jamie Penney [mailto:jpen054 at ec.auckland.ac.nz]
> Sent: Monday, February 18, 2008 2:05 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Empty alternative causes error
> 
> Hi All,
> I have the following rules:
> type_argument_list_handler
>     :    (type_argument_list)=> type_argument_list -> ^(OPTIONAL
> type_argument_list)
>     |    /*empty*/ -> ^(OPTIONAL)
>     ;
> 
> type_argument_list
>     :    LT type_parameters GT -> ^(OPEN_GENERICS[$LT] type_parameters
> CLOSE_GENERICS[$GT])
>     ;
> 
> If I try to remove the empty alternative, I get warnings about
> type_argument_list_handler being able to handle LT using multiple
> alternatives, yet there are no alternatives. If I leave it there, I 
get
> a test for the UP token generated from that rule, yet UP does not
> exist.
> 
> I have two possible answers to this problem.
> 1) The grammar is wrong. Is there another way of specifying empty
> rules?
> 2) ANTLR is wrong. I know that Token.UP is really what it is searching
> for, yet it uses UP instead. Is this possibly a bug?
> 
> Thanks,
> Jamie




More information about the antlr-interest mailing list