[antlr-interest] c-target tree creation

Jim Idle jimi at temporal-wave.com
Thu Jan 7 14:49:59 PST 2010


You should always rewrite the top node so you have a single root node and that is your problem as the nil node is created to hold the children and then as you don't rewrite it, it stays there but it is not created when there is just a single node. So just do this:

Try:

translation_unit
         @init{ _pParser->m_bError = false; _pParser->m_ScopeDelimiter = "@"; }
         : telement EOF

			->^(TUNIT telement)
         ;

telement
	: ( pragma
           | expression_statement
        )*
	;

And you will be all set.

Jim

PS: Please use the list rather than emailing me directly :-)


> -----Original Message-----
> From: Christian Hoffmann [mailto:Sanus at gmx.de]
> Sent: Thursday, January 07, 2010 2:34 PM
> To: Jim Idle
> Subject: Re: [antlr-interest] c-target tree creation
> 
> Hi Jim,
> 
> this is my top-rule...
> 
> translation_unit
>         @init{ _pParser->m_bError = false; _pParser->m_ScopeDelimiter =
> "@"; }
>         : ( pragma
>           | expression_statement
>           )* EOF!
>         ;
> 
> Can you see a problem?
> 
> Thx
> Chris
> 
> 
> 
> JI> Actually the nil node should never be there so there must be
> JI> something awry with your grammar. Try making sure that your tope
> rule looks like:
> 
> JI> top : myrule EOF! ;
> 
> JI> Jim
> 
> >> -----Original Message-----
> >> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> >> bounces at antlr.org] On Behalf Of Christian Hoffmann
> >> Sent: Thursday, January 07, 2010 2:04 PM
> >> To: antlr-interest at antlr.org
> >> Subject: [antlr-interest] c-target tree creation
> >>
> >> Hi,
> >>
> >> I'm stumbling over a unhappy circumstance. Normaly a tree is built
> >> witch a
> >> nil node and the children. But if the parser regognises just one
> line,
> >> the nil node is not used (probably there are no children).
> >>
> >> Example 1 - generated tree has just one node
> >>   Source:
> >>     int a;
> >>   AST:
> >>     <TOK_VAR_DEF(99) at line 2,
> >>       <TOK_VAR_TYPE(100) at line 2, int(49) at line 2>,
> >>       <TOK_VAR_DECL(101) at line 2, a(122) at line 2>
> >>     >
> >>
> >>
> >> Example 2 - generated tree has two nodes
> >>   Source:
> >>     int a;
> >>     int b;
> >>   AST:
> >>     <nil(0) at line 2,
> >>       <TOK_VAR_DEF(99) at line 2,
> >>         <TOK_VAR_TYPE(100) at line 2, int(49) at line 2>,
> >>         <TOK_VAR_DECL(101) at line 2, a(122) at line 2>
> >>       >,
> >>       <TOK_VAR_DEF(99) at line 3,
> >>         <TOK_VAR_TYPE(100) at line 3, int(49) at line 3>,
> >>         <TOK_VAR_DECL(101) at line 3, b(122) at line 3>
> >>       >
> >>     >
> >>
> >>
> >> I think it would be easier for walking in a loop if the nil-node is
> >> always created. Is this possible in future versions?
> >>
> >> Regards,
> >> Christian
> >>
> >> --
> >> Christian Hoffmann
> >> Ützenkamp 4
> >> 38118 Braunschweig
> >> Tel: 0171/7300609
> >> Web: www.c-hoffmann.de
> >>      www.logical-arts.de
> >>
> >>
> >> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >> Unsubscribe: http://www.antlr.org/mailman/options/antlr-
> interest/your-
> >> email-address
> 
> 
> 
> 
> JI> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> JI> Unsubscribe:
> JI> http://www.antlr.org/mailman/options/antlr-interest/your-email-
> address
> 
> 
> 
> --
> Mit freundlichen Grüßen
> Christian Hoffmann
> 
> mailto:Sanus at gmx.de
> 






More information about the antlr-interest mailing list