[antlr-interest] Re: Local lookahead depth
Oliver Zeigermann
oliver at zeigermann.de
Sun Nov 9 23:41:03 PST 2003
--- In antlr-interest at yahoogroups.com, "lgcraymer" <lgc at m...> wrote:
> --- In antlr-interest at yahoogroups.com, Oliver Zeigermann
> <oliver at z...> wrote:
> > Admitted, this is not a really practical example, but consider
the
> > following grammar:
> >
> > {
> > int cnt = 0;
> > }
> >
> > LANGUAGE
> > : ( SHORTWORD ) => SHORTWORD { System.out.println("SHORT"); }
> > | LONGWORD { System.out.println("LONG"); }
> > ;
> >
> > protected SHORTWORD : { cnt = 0; } ( {cnt < 1000}? '*' { cnt++; }
)
> + '#' ;
> > protected LONGWORD : { cnt = 0; } ( {cnt < 10000}? '*' { cnt++; }
)
> + '#' ;
> >
> > It describes a language with two words:
> > 1.) SHORTWORD: exactly 1000 '*' followed by a single '#'
> > 2.) LONGWORD: exactly 10000 '*' followed by a single '#'
>
> Personally, I prefer:
>
> STAR_WORD
> { int count = 0; }
> :
> ( '*' { count++; } )+ '#'
> { if (count == 1000)
> $setType(SHORTWORD);
> else if (count == 10000)
> $setType(LONGWORD);
> }
> }
> ;
It does not work. This grammar would accept 1001 (etc.) stars followed
by a single '#' as well.
Oliver
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list