www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dlang grammar

reply Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
Hello,

Is there somewhere a proper grammar file for D language, that you 
can easily feed into a parser generator, and get out of it nicely 
baked parser (preferably parser in Java)?

Tried to search for such grammars, and no luck. Found of course:
1. https://dlang.org/spec/grammar.html
2. https://libdparse.dlang.io/grammar.html

I've tried both of them to adapt to antlr4 but eventually failed 
(requires a lot more effort than I have willpower for it). First 
for some reason got out of memory exception, while for the latter 
it is plainly incomplete, and also with couple of mistyped words, 
even if it is generated from libdparse.

Thanks for the tips,
Alexandru.
May 06 2022
next sibling parent reply Sergey <kornburn yandex.ru> writes:
On Friday, 6 May 2022 at 21:03:02 UTC, Alexandru Ermicioi wrote:
 Hello,

 Is there somewhere a proper grammar file for D language, that 
 you can easily feed into a parser generator, and get out of it 
 nicely baked parser (preferably parser in Java)?

 Tried to search for such grammars, and no luck. Found of course:
 1. https://dlang.org/spec/grammar.html
 2. https://libdparse.dlang.io/grammar.html

 I've tried both of them to adapt to antlr4 but eventually 
 failed (requires a lot more effort than I have willpower for 
 it). First for some reason got out of memory exception, while 
 for the latter it is plainly incomplete, and also with couple 
 of mistyped words, even if it is generated from libdparse.

 Thanks for the tips,
 Alexandru.
Maybe this example could be useful https://github.com/PhilippeSigaud/Pegged/blob/master/examples/dgrammar/src/pegged/examples/dgrammar.d
May 06 2022
parent reply Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
On Friday, 6 May 2022 at 21:16:30 UTC, Sergey wrote:
 Maybe this example could be useful

 https://github.com/PhilippeSigaud/Pegged/blob/master/examples/dgrammar/src/pegged/examples/dgrammar.d
Thx, I'll try adapt it to antlr format (EBNF).
May 06 2022
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Friday, 6 May 2022 at 22:46:28 UTC, Alexandru Ermicioi wrote:
 On Friday, 6 May 2022 at 21:16:30 UTC, Sergey wrote:
 Maybe this example could be useful

 https://github.com/PhilippeSigaud/Pegged/blob/master/examples/dgrammar/src/pegged/examples/dgrammar.d
Thx, I'll try adapt it to antlr format (EBNF).
That grammar is very old, and contains some experiments. It is probably not what you want. — Bastiaan.
May 08 2022
parent Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
On Sunday, 8 May 2022 at 14:40:36 UTC, Bastiaan Veelo wrote:
 On Friday, 6 May 2022 at 22:46:28 UTC, Alexandru Ermicioi wrote:
 On Friday, 6 May 2022 at 21:16:30 UTC, Sergey wrote:
 Maybe this example could be useful

 https://github.com/PhilippeSigaud/Pegged/blob/master/examples/dgrammar/src/pegged/examples/dgrammar.d
Thx, I'll try adapt it to antlr format (EBNF).
That grammar is very old, and contains some experiments. It is probably not what you want. — Bastiaan.
Thx for the tip. I gave one more try to the official grammar, and managed to generate a parser (ofc after some bugfix in antlr4 itself). Dunno if it is an working one yet, will have to test it. Best regards, Alexandru.
May 09 2022
prev sibling parent reply max haughton <maxhaton gmail.com> writes:
On Friday, 6 May 2022 at 21:03:02 UTC, Alexandru Ermicioi wrote:
 Hello,

 Is there somewhere a proper grammar file for D language, that 
 you can easily feed into a parser generator, and get out of it 
 nicely baked parser (preferably parser in Java)?

 Tried to search for such grammars, and no luck. Found of course:
 1. https://dlang.org/spec/grammar.html
 2. https://libdparse.dlang.io/grammar.html

 I've tried both of them to adapt to antlr4 but eventually 
 failed (requires a lot more effort than I have willpower for 
 it). First for some reason got out of memory exception, while 
 for the latter it is plainly incomplete, and also with couple 
 of mistyped words, even if it is generated from libdparse.

 Thanks for the tips,
 Alexandru.
Vladimir and a few others have an effort to do a tree-sitter grammar for D. That's the closest anyone has come in recent years at least. What do you want the grammar for?
May 06 2022
parent Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
On Friday, 6 May 2022 at 22:24:33 UTC, max haughton wrote:
 Vladimir and a few others have an effort to do a tree-sitter 
 grammar for D. That's the closest anyone has come in recent 
 years at least.

 What do you want the grammar for?
Well, I was trying to learn Truffle Language Implementation Framework (https://www.graalvm.org/22.0/graalvm-as-a-platform/language-imple entation-framework) for starters, and then play a bit with it, and find out the features it provides, using D grammar as basis. In long term (low probability) even to do some kind of D language interpreter for java. Just wondering how well is truffle api built for debugging, as well as for LSP integration.
May 06 2022