Convert a grammar between ABNF, EBNF, and GBNF
Write a grammar from one notation in another, and understand the refusals when a notation cannot say a rule.
You have a grammar in one notation and a tool that reads another, such as an ABNF grammar and a model runtime that takes GBNF. dmog reads each notation as its list of rules and writes them in the other.
Convert the grammar #
Here is a small ABNF grammar, greeting.abnf:
$ cat greeting.abnf
greeting = "hello" SP name
name = 1*ALPHA
Write it as GBNF:
$ dmog translate --from abnf --to gbnf greeting.abnf
greeting ::= ( [hH] [eE] [lL] [lL] [oO] ) SP name
name ::= ALPHA+
root ::= greeting
ALPHA ::= [A-Za-z]
SP ::= " "
Three things changed on the way, each because GBNF says them differently. An ABNF string matches either case, so "hello" became a class for each letter. GBNF starts from a rule named root, so dmog added one that names the first rule. GBNF has no core rules, so ALPHA and SP are written out.
What to watch for #
A notation that has no form for a rule refuses the grammar and names the rule. EBNF has no case-insensitive string:
$ dmog translate --from abnf --to ebnf greeting.abnf; echo $?
{"code":"TARGET_VALUE_UNREPRESENTABLE","message":"the grammar cannot be written as EBNF: the rule \"greeting\" holds the case-insensitive literal \"hello\", which holds a letter beside another character, and no one EBNF terminal spells","row":83,"col":3,"file":"tabnas-ebnf/alchemy/render.alc","output":"none"}
1
The row, col, and file of this failure point into the notation's own render, not into your grammar. The message names the rule in your grammar to change.
Comments and layout are not kept. Each notation's section of the formats reference lists what else its render does not keep.