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

Cameron Esfahani dirty at apple.com
Thu Jul 12 19:16:27 PDT 2007


On Jul 12, 2007, at 5:52 PM, Terence Parr wrote:

> On Jul 12, 2007, at 5:38 PM, Cameron Esfahani wrote:
>>> elements returns[ pANTLR3_VECTOR List ]
>>> 	:	v += value+
>>> 		{
>>> 			$List = $v;
>>> 		}
>>> 	;
>>>
>>> But I get the following error from ANTLR:
>>>
>>> error(134): TWalker.g:58:4: rule '+=' list labels are not allowed  
>>> w/o output option: v
>
> Yeah, rules can have multiple return values and so I just decided  
> to only keep track of result trees, which are currently not  
> available in tree grammars.  I will fix that soon.
>

That sounds great.  It would be nice to use the similar constructs in  
tree grammars as well.

>>> I've read the documentation about "+=" only being applicable in  
>>> AST or template outputs.  And so I'm stuck.
>>>
>>> Does anyone have any suggestions on how to handle this?
>
> I'm in a rush but you can try a single v=value {list.add 
> ($v.start);} which will make a list of all the root/start nodes for  
> stuff going into value rule.
>
> Ter

I'm not quite sure how this is supposed to work.  Since there can be  
multiple "value", wouldn't v only be the first value?

Richard Clark, in his response, suggested something that seems to  
work.  It's not as clean as I'd like, but it does look like it works:

elements returns [ pANTLR3_VECTOR List ]
@init
{
	$List = antlr3VectorNew( 0 );
}
	:	( value
		{
			$List->add( $List, $value.Id, NULL );
		}
		)+
	;


Cameron Esfahani
dirty at apple.com

"Americans are very skilled at creating a custom meaning from  
something that's mass-produced."

Ann Powers


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070712/e18f2877/attachment-0001.html 


More information about the antlr-interest mailing list