Underscores in floating literals
Timon Gehr
timon.gehr at gmx.ch
Fri Feb 10 17:06:00 PST 2012
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.
More information about the Digitalmars-d
mailing list