2. BNF notation

BNF uses a small set of symbols to describe a grammar rule.

The authors of any programming language (e.g. C++, Visual Basic) need to write down the grammatical rules of that language. They often follow the BNF notation to describe these rules. The compiler authors can then read this document and make their compiler program conform to it.

Some of the BNF notation include:

 < > used to enclose a syntactic item 
 ::= means "is defined by" or "consists of"
 | when placed between two items means an OR choice between them
 { } curly brackets means zero or more repetitions of the content.    

Example:

A programmer is about to develop a new database. However, other people will be writing other parts of the program and will need to know the grammatical rules that will be used to form a postal address.

The original programmer might write the grammatical rules in a document for others to follow

BNF grammar example

In English this says "A postal address consists of a name, a street address and a post code with a semi-colon separating them".

Thus, any programmers wishing to use 'postal address' as part of their module need to follow this rule. If they don't, a syntax error will be generated.

The <name> item has the following rule

Backus-Naur Form BNF example

In English this becomes "A name is defined by a first name, followed by a comma, then possibly a middle name, followed by a comma and finally a surname"

As you can see, writing a grammar rule using BNF is a lot more concise and accurate than stating the same rule in English.

English (or any other language!) is wonderful for human communication but pretty hopeless to guarantee accuracy and objectivity!

The other handy thing is that BNF can easily be understood internationally, so making the rule available to compiler writers anywhere in the world..

 

Copyright © www.teach-ict.com