Official D Grammar

Artur Skawina art.08.09 at gmail.com
Tue Apr 9 03:59:05 PDT 2013


On 04/09/13 12:24, Bruno Medeiros wrote:
> On 07/04/2013 16:14, Artur Skawina wrote:
>>> The "1..2" is actually mentioned in the spec:
>>> >"An exception to this rule is that a .. embedded inside what looks like two floating point literals, as in 1..2, is interpreted as if the .. was separated by a space from the first integer."
>>> >so it's there, even if it can be missed.
>> I know, but documenting a (grammar) bug does not make it go away.
>>
> 
> Who says its a bug? From my understanding, this exception is there on purpose, to make it easier to use the DOT_DOT operator in the slice expresions:
> 
>   foo[1..2] // It would be silly to have to put a space after the 1
> 
> At most you could make a case that "1." shouldn't ever parse as float, that the decimal part should be required if the dot is present.

It's a bug, because the grammar does not correctly describe the rules. A
parser/lexer based on the grammar alone will not work. Documenting the
exception(s) helps the human, but doesn't make the grammar correct.

I've started the PEG conversion of my lexer rules and the relevant one
looks like this:

   DecimalFloat:
             (LeadingDecimal "." !"." !IdentifierStart DecimalDigitsNoSingleUS DecimalExponent)
           / (LeadingDecimal "." !"." !IdentifierStart DigitUS*)
           / ("." LeadingDecimal DecimalExponent?)

This works as-is, w/o any extra info - the working lexer is mechanically
generated from aot this rule.
(It differs from the dlang definition in at least four ways - "1..2",
"1.ident", "1.000_1" and ".001")

I'll try to finish the conversion and post the whole lexical grammar in
a couple days (have almost no D-time right now). 

artur


More information about the Digitalmars-d mailing list