[antlr-interest] Having trouble with creating a parser for my desired grammar. Running afoul of multiple alternatives warnings

Bart Kiers bkiers at gmail.com
Wed Nov 16 00:24:20 PST 2011


Hi John,


On Tue, Nov 15, 2011 at 11:46 PM, John B. Brodie <jbb at acm.org> wrote:

> Greetings!
> ...
> I do not think you want to recognize floating point values in the
> parser. any tokens you send to the HIDDEN $channel (or skip();) will be
> silently accepted before and after the '.' of the float. change your
> INTEGER rule to this:
>

I fully agree...



> fragment FLOAT: ;
> INTEGER : DIGIT+ ('.' DIGIT+ {$type=FLOAT;} )? ;
>
> and use FLOAT in the number rule.
>

.. however, Jarrod's grammar allows for input to end with `expression '.'`,
which could be "123." (an INTEGER followed by a DOT). This would be input
the lexer would trip over.

A possible fix could look like:

INTEGER
  :  DIGIT+ ({input.LA(1)=='.' && input.LA(2)>='0' && input.LA(2)>='9'}?=>
'.' DIGIT+ {$type=FLOAT;})?
  ;

I.e., only match a '.' if the character after the '.' is a digit.

Regards,

Bart.


More information about the antlr-interest mailing list