[antlr-interest] Fundamental tree parsing question

Benjamin Niemann pink at odahoda.de
Wed Jul 11 01:30:46 PDT 2007


Ted Villalba wrote:

> I thought I would reuse this thread since it seems a similar issue, or
> misunderstanding on my part...
>  When I add a rewrite specification to my grammar rule it suddenly fails
> with a .RewriteEmptyStreamException.
>  The full grammar is below, but here is what I think it boils down to -
>  I'm
> not sure how to represent zero or more instances in the rewrite of an
> operator when it is the root node .
> For example, the following grammar is parsed successfully and ANTLRWorks
> builds a flat tree.
> value   : terms ((WS)+ operator^ (WS)+ value)*
> 
> But when I try to add the following rewrite specification, I get the
> exception:
> value : terms ((WS)+ operator (WS)+ value)* -> ^(operator ^(TERMS terms)
> ^(VALUE value)*)

The correct way to translate these AST operators to rewrite rules should be:

value:
   (terms -> terms)
   (
     (WS)+ operator (WS)+ v=value
     -> ^(operator $value $v)
   )*
   ;

The first $value refers to the current tree as it has been built so far.
Upon each iteration of the loop a tree is built using the current tree as
the first child and $v as the second. The result becomes the first child in
the next iteration and so on...

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/



More information about the antlr-interest mailing list