[antlr-interest] Another simple case (of confusion)

John B. Brodie jbb at acm.org
Fri Dec 16 07:11:45 PST 2011


Greetings!

On 12/16/2011 09:48 AM, Norman Dunbar wrote:
> Sorry, me again.
>
> I have this in my grammar:
>
> BIN_VALUE : SIGN? '%' BIN_DIGIT+ ;
>
> HEX_VALUE : SIGN? '%' HEX_DIGIT+ ;
>
> INT_VALUE : SIGN? DIGIT+
>
>
> fragment
> BIN_DIGIT : '0'..'1';
>
> fragment
> HEX_DIGIT : (DIGIT | 'a'..'f' | 'A'..'F') ;
>
> fragment
> DIGIT : '0'..'9' ;
>
> fragment
> SIGN : '+'|'-' ;
>
>
> The interpreter allows "+%1.9" or other "bad" values for a BIN_VALUE. 
> The diagram for the above is "grammar T -> BIN_VALUE -> <epsilon>". 
> There's nothing in the console output.
>
> If I do a similar invalid HEX_VALUE such as "+$5.f", I get a similar 
> result, the value is accepted as a valid HEX_VALUE.
>
> Similarly, a decimal point in my INT_VALUE is accepted as well, however, 
> if the invalid character at at the start of the input, it does get 
> rejected - 'a5000' for an INT_VALUE for example - giving "no viable alt 
> exception".
>
>


You did not show your parser rules so I can only guess, but does your
top-most parser rule consume all of the input by requiring an EOF as its
terminator?

ANTLR will happily just consume only a valid prefix of your input and
stop at the first invalid character, unless you tell it to consume
everything.

So I speculate that it really did not accept the decimal point but
rather just stopped.

I do not use ANTLRWorks, but I wonder if perhaps you can verify the
premature exit by inspecting what characters are contained within the
BIN_VALUE token mentioned in your above diagram?

Hope this helps...
   -jbb




More information about the antlr-interest mailing list