www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Underscores in floating literals

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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.
Feb 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/11/2012 01:51 AM, H. S. Teoh wrote:
 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?

Spec bug. What you are observing is in accordance with TDPL. (However, I think it would be better to reject the first example too.)
 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?

Yes.
Feb 10 2012