You are on page 1of 4

Backus-Naur Form (BNF) is a syntax for describing syntax.

It's used to write a


formal representation of a context-free grammar. If that doesn't sound abstract
enough for you, a grammar doesn't have to be a programming language or even
a human language it can be any syntax that you want to describe.
I first encountered BNF when working on the 1992 ANSI standard for DIBOL,
which contained a complete BNF syntactic specification for that language (it was
11 pages long). The exercise of defining the language in BNF helped the
committee rigorously work out the details of its syntax. One of the language
implementers also fed this BNF into Yet Another Compiler Compiler (yacc) to
help build their compiler for their next version, so they could guarantee that it
complied syntactically with the specification. That's where BNF really shines:
Once you have a solid syntactic definition in BNF, you can use tools to rigorously
parse the syntax it describes, without having to invent code that implements the
specification, or rely on your mastery of Regular Expressions.
BNF uses a declarative syntax that allows you to define terms via "production
rules." Each rule contains terms that each have more concrete rules until you get
down to "terminals," which are terms that we can only describe as specific
characters (literal values). Thus, for instance, if we wanted to describe the syntax
for calling a shell command named "fred" that takes one required non-negative
numeric value as its argument, we could express it like this:

In computer science, BNF (Backus Normal Form or BackusNaur Form) is one of the two
main notation techniques for context-free grammars, often used to describe
thesyntax of languages used in computing, such as computer programming languages, document
formats, instruction sets and communication protocols; the other main technique for writing contextfree grammars is the van Wijngaarden form.[1] They are applied wherever exact descriptions of
languages are needed: for instance, in official language specifications, in manuals, and in textbooks
on programming language theory.

Many extensions and variants of the original BackusNaur notation are used; some are exactly
defined, including Extended BackusNaur Form (EBNF) and Augmented BackusNaur
Form (ABNF).
Extended bnf
in computer science, Extended BackusNaur Form (EBNF) is a family of metasyntax notations,
any of which can be used to express a context-free grammar. EBNF is used to make a formal
description of a formal language which can be a computer programming language. They are
extensions of the basic BackusNaur Form (BNF) metasyntax notation.
The earliest EBNF was originally developed by Niklaus Wirth incorporating some of the concepts
(with a different syntax and notation) from Wirth syntax notation. However, many variants of EBNF
are in use. The International Organization for Standardization has adopted an EBNF standard
(ISO/IEC 14977). This article uses EBNF as specified by the ISO for examples applying to all
EBNFs. Other EBNF variants use somewhat different syntactic conventions.

Example

Grammar #1:
Write a BNF grammar for the language of Canadian postal codes.
Example sentences:
K1N 6N5
M5W 2E4
X0A 1A1

Solution 1 (simple):
<postalcode> ::= <letter> <number> <letter> <number> <letter> <number>

Solution 2 (crazy):
<postalcode>
<localdeliveryunit>
<forwardsortationarea>
<localdeliveryunit>
<provarea>
<loctype>

::= <forwardsortationarea> <space>


::= <provarea> <loctype> <letter>
::= <digit> <letter> <digit>
::= A | B | C | E | G | H | J | K | L | M | N |
P | R | S | T | V | X | Y
::= <rural> | <urban>

<rural>
<urban>
<letter>

::= 0
::= 1 | 2
::= A | B
P | R
::= 0 | 1

<digit>

|
|
|
|

3
C
S
2

|
|
|
|

4
E
T
3

|
|
|
|

5
G
V
4

|
|
|
|

6
H
W
5

|
|
|
|

7
J
X
6

|
|
|
|

8
K
Y
7

|
|
|
|

9
L | M | N |
Z
8 | 9

Grammar #2:
Write a BNF grammar for the language of University of Ottawa course codes.
Example sentences:
CSI3125
MAT2743
PHY1200
EPI6581
CSI9999

Solution:
<coursecode>
<acadunit>
<coursenumber>
<year>
<ugrad>
<grad>
<semesters>
<onesemester>
<frenchone>
<englishone>
<bilingual>
<twosemesters>
<frenchtwo>
<englishtwo>
<digit>

::=
::=
::=
::=
::=
::=
::=
::=
::=
::=
::=
::=
::=
::=
::=

<acadunit> <coursenumber>
<letter> <letter> <letter>
<year> <semesters> <digit> <digit>
<ugrad> | <grad>
0 | 1 | 2 | 3 | 4
5 | 6 | 7 | 9
<onesemester> | <twosemesters>
<frenchone> | <englishone> | <bilingual>
5 | 7
1 | 3
9
<frenchtwo> | <englishtwo>
6 | 8
2 | 4
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Grammar #3:
Write a BNF grammar for the language of palindromes.
Examples (spaces don't count!):
aba
pop
pop a pop
elu par cette crapule
a man a plan a canal panama

Solution:
<palindrome> ::= a <palindrome> a | b <palindrome> b |
c <palindrome> c | d <palindrome> d |
e <palindrome> e | ...
| z <palindrome> z
<palindrome> ::= <letter>
<letter>
::= a | b | c | ... | y | z

You might also like