[antlr-interest] help on rewritting the tree

antlr_bill antlr_bill at yeah.net
Sun Oct 4 20:32:10 PDT 2009


thanks jim again,
i am rereading the 'the definitve antlr reference'
seems i misunderstanded at this .

aexpr
: mexpr ('+' ^ mexpr)*
;
the explanation on the book is here
All nonsuffixed tokens are subtree leaf nodes (operands), and the elements
suffixed with ^ are subtree root nodes (operators). The only rule
that uses a rewrite is atom. The parenthesized expression alternative is
much clearer when you say explicitly -> expr rather than putting ! on
the left parenthesis and right parenthesis tokens.

but i still not quite  understand the meaning of the node suffixed with ^ 


thanks,
BILL



在2009-10-06,"Jim Idle" <jimi at temporal-wave.com> 写道: -----原始邮件-----
发件人:"Jim Idle" <jimi at temporal-wave.com>
发送时间:2009年10月6日 星期二
收件人:
抄送:"antlr-interest at antlr.org" <antlr-interest at antlr.org>
主题:Re: [antlr-interest] [SPAM] Re:Re: help on rewritting the tree



ANTLR trees can have many children. Your javaSource rule will be fine, but because all elements are optional you need:

 

J: ^(V etc)

  | V // Empty translation unit

;

 

It is easier to see how the tree is going if you break it down and do not try to create sub trees in a rewrite rule. When you create subtrees, the created node does not properly have the start and end tokens for the sub tree and so on because these happen at rulePostProcessing, not within a rewrite.

 

Don’t use imaginary nodes unless they are necessary to create an unambiguous tree, so the DOT token is just fine as a root node for each element of a compound and you probably only need the imaginary as a root for the whole reference.

 

Jim

 

From:antlr_bill [mailto:antlr_bill at yeah.net]
Sent:Sunday, October 04, 2009 10:07 AM
To:Jim Idle
Cc:antlr-interest at antlr.org
Subject:[SPAM] Re:Re: [antlr-interest] help on rewritting the tree

 

Hi Jim, thanks for you quick reply !

do you mean i should break them into pieces ?
cause the tree structure only can have two children in maximum

if so ,so how about this one? break them too ?

javaSource
    :   (packageDeclaration)? (importDeclaration)* (typeDeclaration)*
    ->^(V packageDeclaration? importDeclaration* typeDeclaration*)
    ;

here means the java source file can contain  package and import and type ,three kinds of element
but i wrote that tree like that , is it right ?

any article about this ? i read the book of antlr many times on the tree grammer ,but still didnt get all of it

thanks again!


BILL


在2009-10-05,"Jim Idle" <jimi at temporal-wave.com>写道:

-----原始邮件-----
发件人:"Jim Idle" <jimi at temporal-wave.com>
发送时间:2009年10月5日 星期一
收件人:"antlr-interest at antlr.org" <antlr-interest at antlr.org>
主题:Re: [antlr-interest] help on rewritting the tree

a: b -> ^(V b) ;

 

b : part (DOT^ part )* ;

 

part:  ID( ABC )? ;

 

Jim

 

From:antlr-interest-bounces at antlr.org[mailto:antlr-interest-bounces at antlr.org]On Behalf Ofantlr_bill
Sent:Sunday, October 04, 2009 8:56 AM
To:antlr-interest at antlr.org
Subject:[antlr-interest] help on rewritting the tree

 

can someone please help on this ?
i have an rule like this :

a:
 :   ID( ABC )? (DOT ID (ABC)? )*
;
how to rewrite it into a tree grammer ?

 

 

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091005/71b50bf6/attachment.html 


More information about the antlr-interest mailing list