I'm a beginner using ANTLR. I'm just doing the following for test purposes and try to understand the way it works and produce errors:
INTEGER_NUMBER : ('0'..'9')+;
integer_number: INTEGER_NUMBER;
decimal_number: (integer_number '.' integer_number) | ('.' integer_number);
regular_number: decimal_number|integer_number;
I have the following warning on the "regular_number" rule : Decision can match input such as "INTEGER_NUMBER '.' INTEGER_NUMBER" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input.
What exactly does it means ?
Ok I have edited this post to include the code that reproduce this error
program :(price)*;
INTEGER_NUMBER : ('0'..'9')+;
INFO_PRICE : 'OLD PRICE';
FRACTION_NUMBER : '1/16'|'1/8'|'3/16'|'1/4'|'5/16'|'3/8'|'7/16'|'1/2'|'9/16'|'5/8'|'11/16'|'3/4'|'13/16'|'7/8'|'15/16';
CURRENCY : 'EUR'|'USD'|'GBP';
integer_number : INTEGER_NUMBER ;
decimal_number : (integer开发者_如何学JAVA_number? '.') integer_number ;
fraction_number : (integer_number ((' ')+))? FRACTION_NUMBER;
regular_number : decimal_number|integer_number ;
numerical_number :regular_number|fraction_number;
non_numerical_number : INFO_PRICE ((' ')*) ('+'|'-') ((' ')*)(numerical_number)((' ')*) CURRENCY;
price : numerical_number |non_numerical_number;
WS : (' ' | '\t' | '\f')+ {$channel=HIDDEN;};
NL :('\r' '\n' | '\r' | '\n') {$channel=HIDDEN;};
and the error know is this one
error(201): /ANTLR - Tuto 1/src/Test.g:37:2: The following alternatives can never be matched: 2 |---> : decimal_number|integer_number ;
As already mentioned in the comments, there's nothing wrong with the grammar you posted.
The error you mention occurs with the following grammar:
grammar Test;
regular_number
: decimal_number
| integer_number
;
integer_number
: INTEGER_NUMBER
;
decimal_number
: integer_number '.' integer_number
| '.' integer_number
| integer_number // <- I added this one
;
INTEGER_NUMBER
: '0'..'9'+
;
When the parser tries to match a regular_number
for the input string "123"
, it does not know which alternative to choose. Both decimal_number
and integer_number
match "123"
. Hence the error message:
... using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
EDIT
From within the regular_number
rule, the string "123.456"
could be parsed as:
integer_number decimal_number
123 .456
or as:
decimal_number
123.456
Why not define a DECIMAL_NUMBER in the lexer:
DECIMAL_NUMBER
: INTEGER_NUMBER '.' INTEGER_NUMBER
| '.' INTEGER_NUMBER
;
?
That should fix that problem.
There are more issues though:
numerical_number
: fraction_number
| regular_number
;
when the rule above tries to handle input like "2 1/8"
, the 2
from it could be a part of your fraction_number
including the 1/8
, but it could also be handled as a regular_number
followed by a fraction_number
.
Also, you shouldn't define spaces inside your parser rules, you've put them on the HIDDEN channel in the lexer, so they won't be "seen" by the parser.
Having said all that, this would be a possible alternative grammar that probably does what you want:
program
: price* EOF
;
price
: numerical_number
| non_numerical_number
;
numerical_number
: FRACTION_NUMBER
| DECIMAL_NUMBER
| INTEGER_NUMBER
;
non_numerical_number
: INFO_PRICE ('+' | '-') (INTEGER_NUMBER FRACTION_NUMBER | numerical_number) CURRENCY
;
DECIMAL_NUMBER
: INTEGER_NUMBER '.' INTEGER_NUMBER
| '.' INTEGER_NUMBER
;
FRACTION_NUMBER
: '1/16' | '1/8' | '3/16' | '1/4' | '5/16' | '3/8' | '7/16' | '1/2' |
'9/16' | '5/8' | '11/16' | '3/4' | '13/16' | '7/8' | '15/16'
;
INTEGER_NUMBER
: '0'..'9'+
;
INFO_PRICE
: 'OLD PRICE'
;
CURRENCY
: 'EUR' | 'USD' | 'GBP'
;
WS
: (' ' | '\t' | '\f' | '\r' | '\n')+ {$channel=HIDDEN;}
;
EDIT II
Also, you shouldn't define spaces inside your parser rules, you've put them on the HIDDEN channel in the lexer, so they won't be "seen" by the parser.
the thing being, how will the lexer make the difference beetween 1011 1/16 and 101 11/16 for example ?
When encountering the (sub) string 1011 1/16
, 1011
is tokenized as an INTEGER_NUMBER
, the space is put on the HIDDEN channel (and is therefor not present in the token-stream that is being passed to the parser), and 1/16
is tokenized as a FRACTION_NUMBER
.
For 101 11/16
the same applies: 101
is a INTEGER_NUMBER
and 11/16
a FRACTION_NUMBER
.
To emphasize: when you put spaces and line breaks on a different channel (like the HIDDEN channel), they are not present in the token stream that the parser operates on. So it makes no sense to use these tokens in parser rules: they're never going to be matched.
HTH
The error you posted means there are two rules that match certain input, in your case the input INTEGER_NUMBER '.' INTEGER_NUMBER
. However, as Bart commented, the grammar you posted does not have that problem. Are you sure the error comes from the exact thing you posted?
精彩评论