What is Backus-Naur Form (BNF)?

I was reading an answer here and came across this term, so what does this term mean, and what is the relationship/influence in current languages?

Author: Maniero, 2017-01-20

2 answers

Is a notation for expressing context-free grammars. In general it is used in grammars of programming languages. But it is also used to express communication protocols, data format, other types of languages, etc. It is not usually used in natural language because this notation is not suitable for context-dependent grammars.

In a certain way we can say that it is a language for writing languages ( specification, not implementation of language).

It demonstrates how the tokens they can be used validly in the text that will be produced with that language or format.

It has some rules that indicate which characters are accepted, which are the most basic tokens and gives a name for each group of these characters. It can also be expressed how these tokenscan be composed to form other tokens, and so on. Actually in general it is expressed otherwise, the more complex tokens are expressed first and each component of that token is expressed later.

The notation allows you to tell which sequences of tokens are valid, whether it can be repeated or not, or even omitted, or whether there are alternatives for use. It generally allows recursion in the use of tokens . Obviously for this you need to express in a way that the recursion is not infinite, through the alternative.

See an example that is cool because the BNF notation itself can be expressed in BNF, as per Wikipedia article :

 <syntax>         ::= <rule> | <rule> <syntax>
 <rule>           ::= <opt-whitespace> "<" <rule-name> ">" <opt-whitespace> "::=" <opt-whitespace> <expression> <line-end>
 <opt-whitespace> ::= " " <opt-whitespace> | ""
 <expression>     ::= <list> | <list> <opt-whitespace> "|" <opt-whitespace> <expression>
 <line-end>       ::= <opt-whitespace> <EOL> | <line-end> <line-end>
 <list>           ::= <term> | <term> <opt-whitespace> <list>
 <term>           ::= <literal> | "<" <rule-name> ">"
 <literal>        ::= '"' <text1> '"' | "'" <text2> "'"
 <text1>          ::= "" | <character1> <text1>
 <text2>          ::= "" | <character2> <text2>
 <character>      ::= <letter> | <digit> | <symbol>
 <letter>         ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
 <digit>          ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
 <symbol>         ::=  "|" | " " | "-" | "!" | "#" | "$" | "%" | "&" | "(" | ")" | "*" | "+" | "," | "-" | "." | "/" | ":" | ";" | "<" | "=" | ">" | "?" | "@" | "[" | "\" | "]" | "^" | "_" | "`" | "{" | "|" | "}" | "~"
 <character1>     ::= <character> | "'"
 <character2>     ::= <character> | '"'
 <rule-name>      ::= <letter> | <rule-name> <rule-char>
 <rule-char>      ::= <letter> | <digit> | "-"

There the token <syntax> it can consist of a <rule>, or by a set of <rule> followed by a <syntax> (noticed the recursion?).

To know how <rule> is composed, it is below. To know how each of its elements is composed, go looking at each BNF rule listed.

Some are terminators, that is, no longer have to look. This is the case in this example of <letter>, <digit> and <symbol>. Terminators are also optionally used in other rules.

There are extensions of the notation to better express some situations.

EBNF from a version of EcmaScript.

 10
Author: Maniero, 2020-04-15 14:52:56

BNF (Backus-Naur Form), originally created by John Backus and Peter Naur, is a meta-syntax used to express Context-free grammars. With it, you can specify which symbol sequences constitute a syntactically valid program in a given language.

The relationship/influence on programming languages is that BNF was initially developed to specify and document the Algol 60 language. Subsequently, it was used for definition of various languages including C, Pascal and Ada.

The imaster blog, has an interesting article on the Subject: What is BNF and why should developers be import?

 2
Author: Taisbevalle, 2017-01-27 12:35:20