[antlr-interest] NoSuchElementException
Juancarlo Añez
apalala at gmail.com
Thu Sep 6 11:58:01 PDT 2012
Mike,
You don't need ANTLR to look at source lines that start with '#define'.
There are easier and more efficient ways to do that.
The problem with your grammar is that the lexer eats too much input.
You can try something like this:
DEFINE
:
(
{getCharPositionInLine() == 0}?=>
'#define'
}
;
EOL: ...;
ANY: (~(EOL));
file : (DEFINE? line)+;
line : ANY* (EOL|EOF).
It's off the top of my head, so it may not work, but you get the idea.
-- Juanca
On Thu, Sep 6, 2012 at 11:32 AM, Justin Murray <jmurray at aerotech.com> wrote:
> 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
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
--
Juancarlo *Añez*
More information about the antlr-interest
mailing list