[antlr-interest] Problem with AST type in tree grammar in C target

Jim Idle jimi at temporal-wave.com
Fri Feb 18 13:59:13 PST 2011


The type of a node is pANTLR3_BASE_TREE and that is the default, you do
not need to specify it as it is ALWAYS that unless you want to do lots and
lots of work, which you don't.

Now the default tree factory actually creates pANTLR3_COMMON_TREE but this
contains pANTLR3_BASE_TREE and it is the pointer to the base tree that is
used because EVERY tree node implementation must contain one of these.

So, when you use the pointer, if you want to access the COMMON_TREE, then
you cast the 'super' pointer in the base tree to pANTLR3_COMMON_TREE and
hey presto.

{
	pANTLR3_COMMON_TREE ct = (pANTLR3_COMMON_TREE)($assLhs->super);
}

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Kevin J. Cummings
> Sent: Friday, February 18, 2011 12:58 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Problem with AST type in tree grammar in C
> target
>
> I'm trying to implement a Tree Grammar for my Lexer and Parser.
>
> In my Tree Grammar, I specify:
>
> > tree grammar m2Emit;
> >
> > options { language=C;
> >           ASTLabelType=pANTLR3_COMMON_TREE;
> >         }
>
> Further down, I have a rule:
>
> > stmt
> >     : ^(ASSIGN assLhs=ref assExp=exp )
> >     ;
>
> When I compile, I see the following errors:
>
> > m2Emit.c: In function 'm2Emit_exp_return exp(m2Emit_Ctx_struct*)':
> > m2Emit.c:1113:20: error: cannot convert 'ANTLR3_BASE_TREE_struct*' to
> > 'ANTLR3_COMMON_TREE_struct*' in assignment
> > m2Emit.c: In function 'm2Emit_ref_return ref(m2Emit_Ctx_struct*)':
> > m2Emit.c:2193:20: error: cannot convert 'ANTLR3_BASE_TREE_struct*' to
> > 'ANTLR3_COMMON_TREE_struct*' in assignment
>
> In m2Emit.c, I can see the following code:
>
> >     retval.start = LT(1); retval.stop = retval.start;
>
> It looks like LT(1) is an ANTLR3_BASE_TREE_struct*, while retval.start
> is an ANTLR3_COMMON_TREE_struct*.
>
> Is this a bug, or did I miss some important documentation?
>
> In case it matters, I'm trying to compile as C++....
>
>
> --
> Kevin J. Cummings
> kjchome at verizon.net
> cummings at kjchome.homeip.net
> cummings at kjc386.framingham.ma.us
> Registered Linux User #1232 (http://counter.li.org)
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list