What is Backus-Naur Form (BNF)?

7

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

    
asked by anonymous 20.01.2017 / 12:53

2 answers

9

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

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

It demonstrates how tokens 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 tokens can be composed to form other tokens , and so on. In fact, in general, the opposite is true, the more complex tokens are expressed first, and each component of this token is expressed later.

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

See an example that is legal 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> | "-"

Then the token <syntax> can be made up of <rule> , OR for a set of <rule> followed by <syntax> (noted the recursion ?).

To find out how <rule> is, it's down. To see how each of its elements is composed, it looks at each BNF rule listed.

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

There are notation extensions to better express some situations.

EBNF of an EcmaScript version .

    
20.01.2017 / 15:52
2

BNF (Backus-Naur Form), was originally created by John Backus and Peter Naur, is a metassintaxe 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 in programming languages is that BNF was initially developed to specify and document Algol 60 language. Subsequently, it was used to define several 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?

    
27.01.2017 / 13:35