[antlr-interest] NoSuchElementException
Justin Murray
jmurray at aerotech.com
Thu Sep 6 09:02:05 PDT 2012
Mike,
Thanks again for your input. I tried both of these suggestions, but
still no luck. With the following grammar:
----
grammar AerobasicPreprocessor;
// Parser rules
preprocess
: line* LAST_SOURCE_LINE_? EOF
;
line
: PP_directive_
| SOURCE_LINE_
;
// Lexer rules
PP_directive_
: '#define'
;
fragment NEWLINE_
: '\u000D'? '\u000A'
| '\u0085'
| '\u2028'
| '\u2029'
;
SOURCE_LINE_
: .* NEWLINE_
;
LAST_SOURCE_LINE_
: .+
;
----
I get the error:
error(201): AerobasicPreprocessor.g:31:4: The following alternatives can
never be matched: 1
Line 31 here corresponds to LAST_SOURCE_LINE_. I tried both '.*' and
'.+' here. Neither worked, but I think '.+' makes more sense (a lexer
rule shouldn't match nothing). Any other ideas?
Thanks,
- Justin
-----Original Message-----
From: Mike Lischke [mailto:mike at lischke-online.de]
Sent: Thursday, September 06, 2012 11:18 AM
To: Justin Murray
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] NoSuchElementException
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