[antlr-interest] rewriting tree such that children are at same level

John B. Brodie jbb at acm.org
Sun Jul 19 11:22:01 PDT 2009


Greetings!

On Sun, 2009-07-19 at 21:02 +0300, hakan eryargi wrote:
> well, i know both trees evaluate to same result, but i want it this
> way for human readability.  this will be used for defining requirments
> and later will be presented in a gui. and i guess it's easier to debug
> this way..
> 
> so you mean writing experssion as: (just postponing OR for now)
> 
> expression
>        p+=primary (AND p+=primary)* -> ^(AND $p+)
>        ;
> 

Jim forgot to take into account that the AND phrases may be missing,
which is why you get the empty exception when no AND is in the input
stream.

So I think your original rule of:

expression : primary ( (AND^ primary)+ | (OR^ primary)+ )? ;

should be something like this (untested):

expression : 
   p+=primary ( ( /*empty*/ -> primary /* or maybe $p or $p+ */ )
              | ( (AND p+=primary)+ -> ^(AND $p+) )
              | ( (OR p+=primary)+ -> ^(OR $p+) )
              ) ;

> i get an RewriteEmptyStreamException
> Exception in thread "main"
> org.antlr.runtime.tree.RewriteEmptyStreamException: token AND
>        at org.antlr.runtime.tree.RewriteRuleElementStream._next(RewriteRuleElementStream.java:158)
>        at org.antlr.runtime.tree.RewriteRuleTokenStream.nextNode(RewriteRuleTokenStream.java:58)
>        at tmp.parser.testParser.expression(testParser.java:232)
>        at tmp.parser.testParser.program(testParser.java:104)
>        at tmp.parser.TestParser.main(TestParser.java:24)
> 
> On Sun, Jul 19, 2009 at 8:47 PM, Jim Idle<jimi at temporal-wave.com> wrote:
> > Not sure WHY you would want that buy it is just:
> >
> > p+=primary (AND p+=primary)* -> ^(AND $p+)
> >
> > Jim
> >
> > On Jul 19, 2009, at 7:04 AM, hakan eryargi <hakan.eryargi at gmail.com> wrote:
> >
> >> hello,
> >>
> >> below is the main part of my grammar. it's ok but it creates an AST
> >> from expression
> >>
> >> a & b & c  -> (& (& a b) c) : an AND node with children c and another
> >> AND node with children a and b
> >>
> >> but i want: (& a b c) : an AND node with tree children
> >>
> >> how can i make that with a rewrite rule ?
> >>
> >> i've found following post in mail list but didnt helped me much :/
> >> http://markmail.org/message/ed3ncmfimu3xnczt
> >>
> >> program :
> >>   expression EOF!
> >>   ;
> >>
> >> expression
> >>   : primary ((AND^ primary)+ | (OR^ primary)+)?
> >>   ;
> >>
> >> primary    :
> >>   VARIABLE
> >>   | LPAREN! expression RPAREN!
> >>   ;
> >>
> >> thanks,
> >> hakan
> >>
> >> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >> Unsubscribe:
> >> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> >
> 
> 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