EBNF
2019-11-20
BNF stands for Backus-Naur Form which is a notation (or you could say: a metasyntax language) designed for description of context-free grammars. Many extensions and augmentations exist like EBNF (Extended BNF), ABNF (Augmented BNF) or TBNF (Translational BNF).
EBNF is the easiest one to read for humans. Another interesting one is a more complex TBNF, I'd say it's an Extended BNF Extended - it lets you define a structure of the Abstract Syntax Tree and the output Intermediate Code. Seems powerful, but personally I didn't delve into it; for now EBNF will make do.
It generally is a useful tool for language and protocol specification description. You can even use *BNF flavours to generate lexers and parsers automatically (see: Yacc).
EBNF is more or less defined like this:
Notation Description
= - definition
, - concatenation
; - termination of an expression
| - alternation (OR)
[ ... ] - option, element may be present once or not at all
{ ... } - repetition, element omited or repeated
( ... ) - grouping
" ... " - literal strings
(* *) - comments
? ... ? - special sequence, describes something outside of the EBNF grammar
- - exception, can be used to exclude something from a rule
* - literal repetition (assert 4*"A" == "AAAA")
Here's a wikipedia article with some examples: https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form#Examples
Another great practical example is the Go Programming Language Specification: https://golang.org/ref/spec
Go's formal specification played a huge part in the programming language's adoption and even before that - it was crucial for the first implementation.
Reading *BNF is quite intuitive. Writing it, however, is a tedious task.