[antlr-interest] Parser inheriting from DebugParser instead of Parser

Johannes Luber jaluber at gmx.de
Thu Jan 24 09:24:37 PST 2008


Dejas Ninethousand schrieb:
> It appears now that it was not a compiler bug (aside from reporting the 
> error twice) but what appears to be an error in the construction of the 
> *Parser.cs file which uses a technique that not allowable in C# (but 
> allowable in Java, hence my confusion).  I am unable to put ANTLERWorks 
> back in the state where the generated parser inherits from DebugParser 
> instead of Parser, (and accidentally blew away that version of the file) 
> so I will have to relate from memory what the issue was with the 
> generated code.
> 
> DebugParser has a field in it named dbg which is protected internal.  
> Somewhere in the generated *Parser.cs, when inheriting from DebugParser, 
> a field is defined that passes that inherited dbg to a constructor 
> during field initialization.  It looked something like this:
> 
> // inside generated parser class which inherits from DebugParser:
> 
> SomeType someField = new SomethingFromLib(dbg, someotherParam);
> 
> As I have just discovered, this type of field initialization that 
> references another field in the class ( i.e. dbg) is not allowed in C# 
> (though allowable in Java, which I bet is the source of the confusion).  
> This initialization must happen in the constructor (and when I moved it 
> to the constructor the file then compiled fine).  So the important thing 
> to note is that, for example:
> 
> class Foo
> {
>       Object dog = new Object();
>       Object cat = dog; // this line will not compile with cryptic error 
> message, nor will any line that references dog e.g. "new X(dog);"
> 
> }
> 
> Is not an allowable C# program while:
> 
> class Foo
> {
>       Object dog = new Object();
>       Object cat;
> 
>       public Foo()
>       {
>           cat = dog;
>       }
> 
> }
> 
> is.
> 
> I think a good way to think about this is "In C#, implicit (or explicit) 
> references to this or base (i.e. "super") are not allowable in field 
> initializers."  Not sure why C# does this, but evidently it is the case.
> 
> Hope this helps.

Ok, that is one the bugs. The reason for the behaviour change is the 
way, objects are initialized. Fields with initializers come first, 
beginning with the most derived class. Then constructors are next, 
beginning with the base class.

Johannes


More information about the antlr-interest mailing list