[antlr-interest] Tree rewriting: java.lang.RuntimeException more than one node as root

Gavin Lambert antlr at mirality.co.nz
Thu Aug 13 06:44:19 PDT 2009


At 00:59 14/08/2009, Jeroen van Schagen wrote:
>I recently started writing an interpreter for the WAEBRIC 
>language, which is a language for generating XHTML code based on 
>self defined function blocks. However, I keep getting runtime 
>exceptions when running my parser. The following program 
>interprets fine:
[...]
>statement: markup+ statement -> ^( markup markup* ',' statement ) 
>
>             | ... ;
>markup:            designator arguments? ;

The root of a tree must be a single token/node.  Since the markup 
rule can match more than one token, it doesn't qualify.  When you 
have situations like this, it's often best to use an imaginary 
token as the root (and it's frequently useful even in other cases, 
to make the tree easier to read or parse in a tree parser.)

statement
   : markup+ statement
     -> ^(STATEMENT markup+ ',' statement)
   ;

Although bear in mind that as written, this rule is ambiguous -- 
it's nonsensical to follow a list of markup with a statement 
because a statement is just more markup.  It's also a bit strange 
to put a generated comma into the rewrite; the tree nesting should 
be sufficient indication of structure.



More information about the antlr-interest mailing list