[antlr-interest] Java arrays in "returns" ?

Bart Kiers bkiers at gmail.com
Sat Mar 13 01:28:40 PST 2010


2010/3/12 Marcin Rzeźnicki <marcin.rzeznicki at gmail.com>

> Hi
> I've got an awkward question. How can I return an array from rule?
> "rule_name returns [Object[] sth] " obviously does not work because syntax
> checker thinks that closing bracket of Object[] closes returns. I could not
> find anywhere how to escape it. Thx for any help.<http://www.antlr.org/mailman/options/antlr-interest/your-email-address>
>

This message from 2007 indicates it is not possible to have nested brackets
inside "rule return statements":
http://www.antlr.org/pipermail/antlr-interest/2007-March/019712.html
and AFAIK, this still is not possible.

You will have to do something like:

@parser::members {

  class Wrapper {

    String[] array;

    Wrapper(String[] a) {
      array = a;
    }
  }

}

parse
  :  rule {String s = $rule.wrapper.array[2];}
  ;

rule returns [Wrapper wrapper]
  :  ... {$wrapper = new Wrapper(new String[]{"a", "b", "c"});}
  ;


Regards,

Bart.


More information about the antlr-interest mailing list