[antlr-interest] An object reference is required for the nonstatic field, method, or property 'Antlr.Runtime.Debug.DebugParser.dbg'

Johannes Luber jaluber at gmx.de
Sat Sep 8 07:46:08 PDT 2007


Fatih Tolga wrote:
> Hi everybody,
> I've just started to learn ANTLR. I am going on the tutorials in the
> site. Firstly, I tried to debug an c# project. ANTLRWorks generated the
> code with "-debug" option. This is the grammer that is in the tutorial:
> 
> grammar Test;
> 
> options
> {language = 'CSharp';
> output=AST;
> }
> expr : mexpr (PLUS^ mexpr)* SEMI!
> ;
> mexpr
> : atom (STAR^ atom)*
> ;
> atom: INT
> ;
> //class csharpTestLexer extends Lexer;
> WS : (' '
> | '\t'
> | '\n'
> | '\r')
> { channel = HIDDEN; }
> ;
> LPAREN: '('
> ;
> RPAREN: ')'
> ;
> STAR: '*'
> ;
> PLUS: '+'
> ;
> SEMI: ';'
> ;
> protected
> DIGIT
> : '0'..'9'
> ;
> INT : (DIGIT)+
> ;
> 
> 
> When I tried to build the generated c# code, an error occured in the
> parser codes. This is the line where the error occured:
>    protected ITreeAdaptor adaptor = new DebugTreeAdaptor(dbg, new
> CommonTreeAdaptor());
> 
> This is the error message:
> An object reference is required for the nonstatic field, method, or
> property 'Antlr.Runtime.Debug.DebugParser.dbg'
> 
> I moved "new DebugTree..." part to the constructor of the Parser like that:
>    public TestParser(ITokenStream input, IDebugEventListener dbg)
>        : base(input, dbg)
>    {
>        this.adaptor = new DebugTreeAdaptor(dbg, new CommonTreeAdaptor());
>    }

That error is caused through an inappropriate code generation template,
which violates the order of the C# object initialization. Moving it into
the constructor is a good solution. As there is some debugging support
missing from the C# target, I suggest to debug via the IDE alone. It is
for now functionally equivalent and has less hassle.
> 
> This time it compiled but another error occured:
> 'Test.exe' does not contain a static 'Main' method suitable for an entry
> point
> 
> What should/would I do?
> 
Do you have a rig which calls into the parser code? ANTLR generates only
something like the code equivalent of a library. By itself a library
doesn't work. Look at the example code for a template you can your own
rig base upon.

Best regards,
Johannes Luber


More information about the antlr-interest mailing list