[antlr-interest] NoSuchElementException

Mike Lischke mike at lischke-online.de
Thu Sep 6 08:18:08 PDT 2012


Justin,

> grammar Test;
> 
> // Parser rules
> preprocess
> 	:	line* EOF
> 	;
> 
> line
> 	:	PP_directive_
> 	|	SOURCE_LINE_
> 	;
> 
> // Lexer rules
> 
> PP_directive_
> 	:	'#define'
> 	;
> 
> NEWLINE_
> 	:	'\u000D'? '\u000A'
> 	|	'\u0085'
> 	|	'\u2028'
> 	|	'\u2029'
> 	;
> 	
> SOURCE_LINE_
> 	:	.* (EOF | NEWLINE_)
> 	;
> 
> ----
> 
> This one does not crash, but does give me the following error:
> 
> error(201): AerobasicPreprocessor.g:27:4: The following alternatives can
> never be matched: 1
> 
> Line 27 corresponds to the SOURCE_LINE_ rule. This error doesn't really
> make any sense to me.

Yes, it's sometimes difficult to actually find the source of the error/warning, because a rule can make that trouble only in a higher level rule, leading to ambiguities. In your case I think the EOF from the preprocess rule is redundant and can be removed (leaving EOF in SOURCE_LINE_).

> If I remove the EOF from the SOURCE_LINE_ rule,
> the grammar builds successfully. However, this doesn't give me what I
> need, which is the possibility of a line at the end of a file, without a
> newline. Any other ideas?


If all fails define a lexer rule like:

REST: .*;

and use this so:

preprocess
	:	line* REST
	;

Mike
-- 
www.soft-gems.net




More information about the antlr-interest mailing list