[antlr-interest] how do I accumulate multiple returns for parser matches in one variable?

Oliver Zeigermann oliver.zeigermann at gmail.com
Mon Jan 26 00:38:07 PST 2009


Have you tried this(not tested)?

ingrediententry

                @init {

                                List<int> li = new List<int>();

                }

                // ingredient returns an id

                :               q=number uid=unit iid=ingredient
(qid=ingredient_qualifier {$li.Add($qid);} )*
 {…

What you might try as an alternative:
AFAIK if you write this

qid+=ingredient_qualifier*

qid will be of type List and contrain the results of the rull calls as elements.


- Oliver

2009/1/26 Sven Prevrhal <sven.prevrhal at ucsf.edu>:
> I want to parse recipes and I have something like
>
>
>
> ingrediententry
>
>                 :               q=number uid=unit iid=ingredient
> qid=ingredient_qualifier*
>
>                 { …
>
>
>
> Where ingredient_qualifier can appear more than once. How do I accumulate a
> list of qualifiers that I can then process in the action to
>  ingrediententry?
>
> I tried
>
>
>
> ingrediententry
>
>                 @init {
>
>                                 List<int> li = new List<int>();
>
>                 }
>
>                 // ingredient returns an id
>
>                 :               q=number uid=unit iid=ingredient
> qid=ingredient_qualifier* {$li.Add($qid);}
>
>                 {…
>
>
>
> But that gives an missing attribute access error on rule scope for $qid…
>
>
>
> Cheers,
>
> Sven
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>


More information about the antlr-interest mailing list