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

Dejas Ninethousand dejas9000 at gmail.com
Thu Jan 24 08:34:35 PST 2008


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.




On Jan 23, 2008 3:54 PM, Johannes Luber <jaluber at gmx.de> wrote:

> Dejas Ninethousand schrieb:
> > I was somehow able to place ANTLRWorks into a mode where its C#
> > generated parser class inherited from a class called DebugParser instead
> > of Parser.  However, now the program is back in a state generating code
> > that descends from Parser.  It appears descending from DebugParser may
> > have uncovered a bug in the MS C# complier, so I am interested in
> > generating the old version of the class again (descending from
> > DebugParser) but can't figure out how to do it.  Does anyone know how
> > this is done?
> >
>
> Can you describe the bug? There are few bugs related to debugging which
> haven't been fixed yet. Before you suspect the compiler - suspect your
> code first, then the libraries. ;)
>
> Johannes
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080124/c70d9235/attachment.html 


More information about the antlr-interest mailing list