views:

469

answers:

2

Hello,

I have a simple rule in ANTLR:

title returns [ElementVector<Element> v]
@init{

    $v = new ElementVector<Element>() ;

}
    :  '[]' 
    | '[' title_args {$v.add($title_args.ele);} (',' title_args {$v = $title_args.ele ;})* ']' 
    ;

with title_args being:

title_args returns [Element ele]
    : author {$ele = new Element("author", $author.text); }
    | location {$ele = new Element("location", $location.text); }
    ;

Trying to compile that I get confronted with a 127 error in the title rule: title_args is a non-unique reference.

I've followed the solution given to another similar question in this website (http://stackoverflow.com/questions/759028/how-to-deal-with-list-return-values-in-antlr) however it only seems to work with lexical rules.

Is there a specific way to go around it ?

Thank you, Christos

A: 

I think the problem is your reusing the title_args var. Try changing one of those variable names.

Jorn
+1  A: 

You have 2 title_args in your expression, you need to alias them. Try this:

|   '[' t1=title_args {$v.add($t1.ele);} (',' t2=title_args {$v = $t2.ele ;})* ']'

t1 and t2 are arbitrary aliases you can choose anything you want as long as they match up.

Ted Elliott