请求获取编程语言D的BNF形式语法
I totally get the frustration of trying to track down a pure BNF grammar for D—they’re surprisingly hard to find in that exact format, even with the resources you’ve already located.
The grammar repository you referenced holds the official, authoritative syntax definitions for D, but they’re written in a custom EBNF-style format rather than strict BNF. Here’s how you can get a BNF version from it:
- Manual conversion: The existing rules use common EBNF shorthands (like
?for optional elements,*for zero-or-more repetitions). You can expand these into pure BNF by splitting rules. For example, a rule likeModule: ModuleDeclaration ImportDeclaration*would become:Module → ModuleDeclaration ImportDeclarationSequence Module → ModuleDeclaration ImportDeclarationSequence → ImportDeclaration ImportDeclarationSequence ImportDeclarationSequence → ImportDeclaration - Tool-assisted conversion: Some parser generators or grammar conversion scripts can take the repository’s grammar files and output BNF. If you’re comfortable with scripting, writing a small tool to process the files and expand the EBNF notations into BNF rules would save time.
You can also turn to the official D Language Specification, which presents the language’s syntax in an EBNF-like structure that’s straightforward to convert to BNF. Every syntax rule is laid out clearly there, so you can map each one directly by replacing shorthand notations with explicit BNF alternatives.
Right now, there’s no widely distributed pure BNF grammar for D, so converting the existing EBNF-style rules (either from the repo or the spec) is the most practical way to get the exact format you need.
内容的提问来源于stack exchange,提问作者Divyat

