Underscores in floating literals

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Feb 10 16:51:31 PST 2012


As an exercise in D programming, I'm writing a D lexer from scratch,
based on the online specs posted on DPLO. I'm running into what looks
like a discrepancy between the specs and compiler behaviour (I'm using
gdc-4.6.2):

Spec says:
	DecimalFloat:
	    LeadingDecimal .
	    LeadingDecimal . DecimalDigits
	    DecimalDigits . DecimalDigitsNoSingleUS DecimalExponent
	    . DecimalInteger
	    . DecimalInteger DecimalExponent
	    LeadingDecimal DecimalExponent

Based on the 3rd condition above, things like "123._e12" should be
illegal (you can't have only a '_' between '.' and 'e').  However, the
compiler accepts "123._e12".

Furthermore, the spec says:

	DecimalExponent
	    DecimalExponentStart DecimalDigitsNoSingleUS

This means "123.e_2" should be accepted, but the compiler says:

	test.d:6: exponent expected

So the question is: am I reading the specs wrong, or is this a compiler
bug, or a spec bug?  What *should* the floating literal syntax be?

Does it make sense to treat '_' essentially as a null string when seen
inside a floating literal? If so, "123._e12" should be accepted, and so
should "123.e_2", in which case *both* the spec and the compiler are
wrong.

Also, are multiple consecutive underscores permitted? I.e., is
"1__000__000.0___0" legal?


T

-- 
First Rule of History: History doesn't repeat itself -- historians merely repeat each other.


More information about the Digitalmars-d mailing list