www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is the grammar spec usable for generating parsers?

reply Peter Alexander <peter.alexander.au gmail.com> writes:
I'm writing D support for tree-sitter 
(http://tree-sitter.github.io/tree-sitter/) using the grammar 
from the spec (https://dlang.org/spec/grammar.html) as a guide. 
When manually converting the spec grammar to tree-sitter's 
grammar rules, I've encountered a conflict early on and I'm 
wondering if the spec can be trusted or if there is a better spec.

The conflict I encountered is with static imports, e.g.

```
static import foo;
```

This should be an ImportDeclaration, but the grammar from the 
spec would parse it as an AttributeSpecifier (static) followed by 
an ImportDeclaration, i.e. as if it were

```
static { import foo; }
```

(but without the { })

Of course, I can fix this easily enough, but I'm wondering how 
much the grammar can be trusted. Has anyone  else specified a 
more correct grammar for D?
Nov 03 2018
parent reply Iain Buclaw <ibuclaw gdcproject.org> writes:
On Sat, 3 Nov 2018 at 23:20, Peter Alexander via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 I'm writing D support for tree-sitter
 (http://tree-sitter.github.io/tree-sitter/) using the grammar
 from the spec (https://dlang.org/spec/grammar.html) as a guide.
 When manually converting the spec grammar to tree-sitter's
 grammar rules, I've encountered a conflict early on and I'm
 wondering if the spec can be trusted or if there is a better spec.

 The conflict I encountered is with static imports, e.g.

 ```
 static import foo;
 ```

 This should be an ImportDeclaration, but the grammar from the
 spec would parse it as an AttributeSpecifier (static) followed by
 an ImportDeclaration, i.e. as if it were

 ```
 static { import foo; }
 ```

 (but without the { })

 Of course, I can fix this easily enough, but I'm wondering how
 much the grammar can be trusted. Has anyone  else specified a
 more correct grammar for D?
First have a look here, from memory I ended up basing the D grammar in gdb on this version instead. https://libdparse.dlang.io/grammar.html -- Iain
Nov 03 2018
parent reply Peter Alexander <peter.alexander.au gmail.com> writes:
On Saturday, 3 November 2018 at 22:29:59 UTC, Iain Buclaw wrote:
 First have a look here, from memory I ended up basing the D 
 grammar in gdb on this version instead.

 https://libdparse.dlang.io/grammar.html
Thanks. Unfortunately, it seems that libdparse doesn't know about 'static import' and instead parses it as an importDeclaration within a 'static' attribute block. I suppose this could be re-interpreted after the parsing stage.
Nov 03 2018
parent Iain Buclaw <ibuclaw gdcproject.org> writes:
On Sun, 4 Nov 2018 at 00:05, Peter Alexander via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 On Saturday, 3 November 2018 at 22:29:59 UTC, Iain Buclaw wrote:
 First have a look here, from memory I ended up basing the D
 grammar in gdb on this version instead.

 https://libdparse.dlang.io/grammar.html
Thanks. Unfortunately, it seems that libdparse doesn't know about 'static import' and instead parses it as an importDeclaration within a 'static' attribute block. I suppose this could be re-interpreted after the parsing stage.
Ah, yes. Indeed that looks correct as per grammar. In the built up parse tree, static funcDecl and static importDecl would be storing the same information, but interpreted differently in semantic. -- Iain
Nov 03 2018