[antlr-interest] Re: Handling delimeter separated lists

Chris Daly cj_daly at yahoo.com
Fri Mar 12 15:14:47 PST 2004


Hi Jason,

I do this sort of thing a lot.  The pattern I've come up with looks
something like this (I haven't run it through Antlr so there could be
some typos, but it should give the general idea).  Note how you can
construct the ArrayList immediately inside the returns clause and how
you can do all of the ArrayList.add() calls in foo_list (no need to
pass the list into the foo rule.

foo_list returns [List l = new ArrayList()] :
{ String f = null; }
f=foo { l.add(f); } (COMMA f=foo { l.add(f); } )*
;

foo returns [String val = null] :
foo:FOO { val = foo.getText(); }
;


Hope this helps...

Chris



--- In antlr-interest at yahoogroups.com, Jason <jasonriz at y...> wrote:
> Hello,
> 
> I have a grammar which has multiple instances of
> comma-separated lists of different types.  The upshot
> of this is that I've got several sets of rules which
> look like this:
> 
> foo_list: foo (COMMA foo)*;
> 
> I find these lists much harder to process than their
> LR counterparts.  I'm generally doing something like
> this:
> 
> foo_list
>   returns [List fooInstList]
>   :
> { String fooInst = null; fooInstList = null; } 
> :
>   fooInst = foo[true, null] 
>   {
>     fooInstList = new ArrayList( );
>     fooInstList.add(fooInst);
>   }
>   (EAL_COMMA property[false, fooInstList])*
>   {
>     // Nothing left to do
>   }
> }
> ;
> 
> foo [boolean isFirst, List foobarList] returns [String
> fooInst]
>   {fooInst = null;} 
>   :
>   foo:FOOBAR
>   {
>     String fooInst = foo.getText( );
>     if (!isFirst)
>     {
>       foobarList.add(fooInst);
>     }
>   }
> ;
> 
> This seems to work but it seems less than elegant
> especially in those cases where foo is called from
> productions other than foo_list.  This issue must
> arise pretty frequently in LL grammars.  Does anyone
> have any idioms they'd like to share?  Is there
> something I'm missing?  Thanks in advance.
> 
> -jason  
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Search - Find what you're looking for faster
> http://search.yahoo.com



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list