[antlr-interest] Problem using += operator in actions

Martin Fowler fowler at acm.org
Tue Apr 17 06:00:58 PDT 2007


This is another one of those problems I ended up solving myself but I 
post here to help others.

I wanted to collect together some words using the += operator. I used a 
grammar line like this:

item	: 'item'  w += WORD+ ';'
	{System.out.println("name is: " + $w);}
	 ;

The problem was that $w ended up being null.

My mistake was that the phrase w+=WORD needed to be contained in 
parentheses before applying the +, like so:

item	: 'item'  (w += WORD)+ ';'
	{System.out.println("name is: " + $w);}
	 ;


-- 
Martin Fowler
http://martinfowler.com


More information about the antlr-interest mailing list