[antlr-interest] ANTLR 3: relax this error for the java taget: 'label conflicts with rule with same name'

Jim Idle jimi at temporal-wave.com
Thu Aug 14 12:20:42 PDT 2008


On Thu, 2008-08-14 at 18:19 +0200, Francis ANDRE wrote:

> Hi
> 
> One gets this error message Cobol.g:4:44 label subscript conflicts with 
> rule with same name
> on the following snippet
> 
> grammar Cobol;
> tokens { DOT='DOT';  SECTION='SECTION';  IDENT='I'; }
> debut    :    subscripts    ;
> subscripts returns[Object[\] indexes] : LP subscript = subscript (COMMA 
> subscript = subscript)* RP ;
> subscript returns [Object index]        :  IDENT | NUMBER ;
>    
> NUMBER      : '0'..'9'+;
> LP                  : '(';
> RP                  : ')';
> COMMA        : ',';
>    
> Since one can write this valid  Java code
> class Foo {
>     static Foo     foo = foo();
> 
>     static Foo foo() {
>         return new Foo();
>     }
> }
> I am proposing to relax this error message for Java and keep it for all 
> languages that do not allow this construct as C, C++...


I don't think that we want to have different rules for different
targets, for the very fact that the whole point is that it is target
independent. There are other considerations such as namespace pollution
and the fact that the generated code deliberately tries to be something
that you can debug and walk through and things like this would very much
hinder that. There is a gestalt to the whole thing.

Besides which, using the same name for rules and labels and $rule is
just asking for trouble - it makes your grammar completely ambiguous to
the human eye even if ANTLR can/could disambiguate the references.
Source code should be readable like text and you should not have to keep
considering the context of an identifier.

I can do this in many languages:

{
   int i = 66; ...
   { int i = 666; ... }
}

But no programmer worth their salt would do it deliberately, even though
it can be useful for code generators. Many compilers say things like
"[Warning] : Identifier 'i' at line xxx hides identifier of same name at
line yyy".

Just rename your labels (why not use hungarian?), make your grammar
obvious and put your energies in to the grammar itself. There are many
design considerations and constraints that you are not familiar with and
it is better propose changes once you understand what they are.

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080814/00d49a0c/attachment.html 


More information about the antlr-interest mailing list