bnfc

How to write a grammar for hex ints in BNFC?


Here's a tiny grammar for hexadecimal integers.

Numbers . Numbers ::= [HexInt];
separator HexInt " " ;

token HexDigit ["0123456789abcdefABCDEF"] ;
rules HexInt ::= "0x" [HexDigit] ;
separator HexDigit "" ;

It fails to parse "0xff", however, because the lexer treats "ff" as a single token. How do I fix that?


Solution

  • There's no easy way to fix it. There seems to be a bug in BNFC that is including the built-in rule for Ident even though your grammar doesn't make use of it, and it takes precedence over HexDigit in your example (longest match wins).

    However you can write a token rule for hexadecimals:

    token HexInt ({"0x"} ["0123456789abcdefABCDEF"] +) ;