[antlr-interest] How to handle rewrite of multiple nodes

Simon simonzmail at gmail.com
Tue Mar 29 22:34:12 PDT 2011


Thank you!

This one worked fine:

> bracketedBlock : '{' statement* '}' -> ^(Block statement*) ;

Strangely it works for two statements but not for 3 - my input like this

  { echo 'foo';  echo 'bar';  echo 'tree'; }

Now produces this AST

64: Block
  34: echo
    92: 'foo'
  34: echo
    92: 'bar'

(No 'echo tree' in the AST).

This might be a higher level problem in my grammar though ... but if
something comes to mind I'd love to know any ideas why that might be.

Thanks again!

Cheers,

Simon

On Wed, Mar 30, 2011 at 1:27 PM, John B. Brodie <jbb at acm.org> wrote:
> Greetings!
>
> On Wed, 2011-03-30 at 12:43 +1100, Simon wrote:
>> Hello all,
>>
>> I have a grammar that does a rewrite like so:
>>
>>   bracketedBlock
>>       : '{' stmts=statement* '}' -> ^(Block $stmts)
>>       ;
>>
> you are not properly handling the cardinality of the * meta-operator.
>
> try:
>
> bracketedBlock : '{' statement* '}' -> ^(Block statement*) ;
>
> or:
>
> bracketedBlock : '{' stmts+=statement* '}' -> ^(Block $stmts} :
>
>
>
> Hope this helps...
>   -jbb
>
>
>


More information about the antlr-interest mailing list