[antlr-interest] How do I accept input ending with a newline *or* EOF?
Jim Idle
jimi at temporal-wave.com
Mon Jan 31 16:34:12 PST 2011
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:
grammar t;
start
: input* EOF
;
input
: ((ID)=>ID)+
| NL
;
ID
: ('a'..'z')+
;
WHITESPACE
: ' '+ {skip();}
;
NL : '\n' ;
Note that lexer rules should NEVER match an empty input, or they will
instantly match nothing an infinite number of times. The predicate is not
absolutely necessary but will suppress a warning.
You could also use the LL(*) mechanism, but we this will need to read all
the ID* available until NL or EOF before selecting the path:
grammar t;
start
: input* input2 EOF
;
input
: ID* NL
;
input2
: ID*
;
ID
: ('a'..'z')+
;
WHITESPACE
: ' '+ {skip();}
;
NL : '\n' ;
Or, but only if the elements you are looking for remain as simple as this,
then you can trivially do this:
grammar t;
start
: input EOF
;
input
: ID* (NL ID*)*
;
ID
: ('a'..'z')+
;
WHITESPACE
: ' '+ {skip();}
;
NL : '\n' ;
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of chris king
> Sent: Monday, January 31, 2011 3:50 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] How do I accept input ending with a newline
> *or* EOF?
>
> Hello! I'm trying to write a grammar that will accept lines of zero or
> more IDs and I'd like to allow the last line to end in a new line *or
> *EOF. I came up with this grammar:
>
> grammar test;
>
> start
> : input*
> ;
>
> input
> : ID* ('\n' | EOF)
> ;
>
> ID
> : ('a'..'z')*
> ;
>
> WHITESPACE
> : ' '+ {skip();}
> ;
>
> But got this error from ANTLRWorks saying start has un-reachable
> alternatives:
>
> [15:38:33] error(201): test2.g:9:5: The following alternatives can
> never be
> matched: 2
>
> If I remove the reference to EOF than everything works but I have to
> end the last line in a new line and I don't want to have to do that.
> Any suggestions?
>
> Thanks,
> Chris
>
> 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