[antlr-interest] How to handle + when using arguments in Java code

Hendrik Maryns qwizv9b02 at sneakemail.com
Fri Nov 14 09:48:17 PST 2008


Jim Idle schreef:
> On Fri, 2008-11-14 at 13:05 +0100, Hendrik Maryns wrote:
>> Hendrik Maryns schreef:
>> > Hi all,
>> > 
> 
>> > The bare rule would be:
>> > /**
>> >  * Conjunction or disjunction.
>> >  */
>> > n_ary returns [Formula result]
>> > 	: CONJUNCTION ( WHITESPACE formula )+
>> > 	| DISJUNCTION ( WHITESPACE formula )+
>> > 	;
>> > 
>> > And for CONJUNCTION I would like new Conjunction(Formula... args) to be
>> > invoked with the formulas found (ignoring whitespace).  Similar for new
>> > Disjunction(Formula... args).  As you might guess
>> > 
> 
> I am not sure what your Formula class is, but the two patterns are:
> 
> n_ary returns [Formula result]
> 
> @init {
>    $result = new Formula();
> }
> : CONJUNCTION ( WHITESPACE f1=formula   { $result.add($f1); } ) +

I found this solution by myself, made a subrule as follows:

n_aryArguments returns [List<Formula> result]
@init{ $result = new ArrayList<Formula>(); }
	: ( WHITESPACE arg=formula { $result.add(arg) } )+
	;

> And
> 
> n_ary returns [Formula result]
> 
> : CONJUNCTION ( WHITESPACE f1+=formula    ) +  { $result = new
> Formula($f1); } // f1 is  a list

This looks better, but I get an error:

(134): rule '+=' list labels are not allowed w/o output option: f1

Cheers, H.
-- 
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 257 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20081114/b76511f8/attachment.bin 


More information about the antlr-interest mailing list