Why can't Integer be negative?

21 views
Skip to first unread message

Ron Siemens

unread,
Oct 6, 2022, 8:34:11 PM10/6/22
to BNFC Developers and Users
I've been using BNFC for a while, got pretty far with my Grammar.  All of a sudden I had need for some negative numbers in my specifications.  To my surprise, Integer is only positive!  Why?!

Then I looked at the sample C language spec and even the unsigned types are positive only - weird!

If I make my own token to support negative values

token Integer ('-')? ["123456789"] digit*;

The generated code now has a "char *" for Integer instead of int.  What a mess!  Isn't there a simpler way to make the built-in "Integer" support negative numbers?  That should have been fixed ages ago, no?

Ron Siemens

unread,
Oct 6, 2022, 8:41:42 PM10/6/22
to BNFC Developers and Users
I mean the non-unsigned types:


token Unsigned ["123456789"] digit * ('u'|'U');


token Long ["123456789"] digit * ('l'|'L');
 

token UnsignedLong ["123456789"] digit * (('u''l')|('U''L'));

Ron Siemens

unread,
Oct 6, 2022, 8:56:51 PM10/6/22
to BNFC Developers and Users

Doc mentions this:

>These types are hard-coded and cannot be value types of rules:

  • Integer: sequence of digits, e.g. 123445425425436'

>Precise definitions are given in the LBNF report.

What is this LBNF report?  Maybe this other documentation which says:

>Each token type is implemented by a newtype in Haskell, as a String in Java, and as a typedef to char* in C etc.

I am using C, so that's why my new Integer token is now a "char *"!  There's no way I can make it an int to keep my code compatible with the built-in Integer token?

Very frustrating...

Ron Siemens

unread,
Oct 7, 2022, 12:08:49 AM10/7/22
to BNFC Developers and Users
After some snooping around, I realized this isn't as "hard-coded" as it seemed.  I changed the definition in the output lex file, before generating my code.
Reply all
Reply to author
Forward
0 new messages