Hello!

I created three simple grammar files:

G1_Lexer.g:

lexer grammar G1_Lexer;
options {
    language=CSharp3;
}
INT :   '0'..'9'+;

G1_Parser.g:

parser grammar G1_Parser;
options {
    language=CSharp3;
    tokenVocab = G1_Lexer;
}
rule1: INT;

G2.g

grammar G2;
options {
    language=CSharp3;
    output=AST;
}
import G1_Parser;
prog    :rule1*;

Then generate the C# code. The constructor of G2Parser is:

        public G2Parser(ITokenStream input, RecognizerSharedState state)
                : base(input, state)
        {
                ITreeAdaptor treeAdaptor = null;
                CreateTreeAdaptor(ref treeAdaptor);
                TreeAdaptor = treeAdaptor ?? new CommonTreeAdaptor(); //throws 
NullReference exception
                gG1_Parser = new G2_G1_Parser(input, state, this);

                OnCreated();
        }

As you can see the gG1_Parser is initialized after setting of TreeAdaptor; but 
TreeAdaptor setter uses this field:

        public ITreeAdaptor TreeAdaptor
        {
..........
                set
                {
                        this.adaptor = value;
                        gG1_Parser.TreeAdaptor = this.adaptor;
                }
        }


I changed the '..\org\antlr\codegen\templates\CSharp3\CSharp3.stg' file like 
this:

public <grammar.recognizerName>(<inputStreamType> input, RecognizerSharedState 
state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
        : base(input, state)
{
        <grammar.directDelegates:
         {g|<g:delegateName()> = new <g.recognizerName>(input, 
state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
        <parserCtorBody()>
        <grammar.indirectDelegates:{g | <g:delegateName()> = 
<g.delegator:delegateName()>.<g:delegateName()>;}; separator="\n">
        <last(grammar.delegators):{g|gParent = <g:delegateName()>;}>

        OnCreated();
}

Move the <parserCtorBody()> after <grammar.directDelegates and now it works 
correctly.

Kind regards,
Andrey Sirotkin


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to