[antlr-interest] Dealing with lists in a in tree grammar action

Richard Clark rdclark at gmail.com
Thu Jul 12 18:09:10 PDT 2007


On 7/11/07, Cameron Esfahani <dirty at apple.com> wrote:

> Here is a small portion from my tree grammar.  An array can have one or more
> elements in it.  What I'd like to do in my action for "elements" is gather
> each of the "value" and add them to a list.

> elements returns[ pANTLR3_VECTOR List ]
>  : v += value+
>  {
>  $List = $v;
>  }
>  ;

You can insert code at arbitrary points, so want something like this
(pseudo-code guts...)

elements returns[ pANTLR3_VECTOR List ]
@init { $List = new pANTLR3_VECTOR; }
  : ( value
      { /* Whatever you want in here, e.g.: */ append($value.value, $List); }
    )+
  ;

 The stuff in @init {...} will be called at the start of the elements
function and the stuff in the inner {...} will be called each time
through the loop.

 ...RIchard


More information about the antlr-interest mailing list