[antlr-interest] CSharp3 Composite error
Andrey Sirotkin
avs at lizatec.com
Fri Mar 11 06:13:24 PST 2011
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
More information about the antlr-interest
mailing list