[antlr-interest] How do I accept input ending with a newline *or* EOF?

Jim Idle jimi at temporal-wave.com
Tue Feb 1 08:51:31 PST 2011


Just append a newline to the input string whether it has one or not -
there is no need to play with the input stream logic. A realloc of the
input string for one extra character will usually be accommodated by the
existing string allocation (which is almost always aligned/padded).

Jim

> -----Original Message-----
> From: Kirby Bohling [mailto:kirby.bohling at gmail.com]
> Sent: Monday, January 31, 2011 4:54 PM
> To: Jim Idle
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] How do I accept input ending with a
> newline *or* EOF?
>
> On Mon, Jan 31, 2011 at 6:34 PM, Jim Idle <jimi at temporal-wave.com>
> wrote:
> > It is much better to add a NL to the end of the input if it is not
> > present as the ambiguities are complicated and pointless without
> that,
> > but with this simple example you can do:
> <snip..>
>
> Jim,
>
>    I've seen you make this suggestion several times.  Is there a
> canonical or simple way to do that inside the Lexer?  Or is the easiest
> way to do that to monkey with the stream to ensure it kicks out a
> potentially missing character if it isn't the last character before the
> EOF?
>
> I've had this problem, and I just had my NL be ('\n'|EOF) and moved
> along, for the grammars I had it was 'good enough'.  It would seem much
> easier, and cleaner to fix up it up as you suggest, just not sure the
> cleanest way to go about it.
>
> Kirby


More information about the antlr-interest mailing list