[antlr-interest] Can't get += working

Tom Smith yotommy at gmail.com
Mon Nov 19 05:43:00 PST 2007


Since you seem to be using output=template, you may want to try an
approach which has recently worked for me.  Revise your strategy so
that you are writing templates rather than passing return values.
Something like:

buildList : 'PROPERTY' 'LIST' 'IS' prop+=listObject (',' prop+=listObject )*
    -> propertyListTemplate(props={$prop})
    ;

listObject : n=ObjectName '=' v=StringLiteral
    -> nameValueTemplate(name={$n}, value={$v}) << ... >>
    ;

The propertyListTemplate can then use template application on the List of props:

propertyListTemplate(props) ::= <<
   my props are <props;separator=",">
>>

There may be better ways, which I would be happy to hear, as I am an utter noob.

Tom.

On 11/19/07, Mike Arace <mikearace at hotmail.com> wrote:
>
>  Hi guys,
>
> I'm running into a problem with Antlr involving the List += syntax.
>
> Say I have the following example rules:
>
> //------------------------
>
> buildList : 'PROPERTY' 'LIST' 'IS' prop+=listObject (',' prop+=listObject
> )*;
>
> listObject returns [String name, String value] : n=ObjectName '='
> v=StringLiteral { $name = $n.text; $value = $v.text; }
>
> StringLiteral :  '\'' (~'\'')* '\'' ( '\'' (~'\'')* '\'' )*;
>
> ObjectName : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' )*;
>
>
> //-------------------------
>
> Using the Java target, Antlr creates a static class called listObject_return
> that wraps the two return values of the listObjectRule, as well as a
> StringTemplate.  For the buildList rule, it creates a List called list_prop
> to hold the listObject values assigned using the += syntax, and a temporary
> variable called prop that holds the current value as it parses each of the
> elements.
>
> My problem is this:
>
> I assume that each of the elements of list_prop would be of type
> listObject_return, but that is not the case.  Instead, it creates a new
> listObject_return and correctly assigns the name and value properties within
> the listObject rule, and then runs prop.getTemplate() within buildList and
> adds that value to list_prop.  In my rule, this value is always null.
>
> Given the rules above, how could I access name and value in
> listObject_return  object from within buildList?
>
> Best Regards,
> Mike
>
> ________________________________
> Share life as it happens with the new Windows Live. Share now!


More information about the antlr-interest mailing list