0% found this document useful (0 votes)
219 views14 pages

Backus-Naur Form

BNF is a metalanguage used to formally describe the grammar of programming languages. It uses symbols like ::=, |, <>, and brackets to concisely define syntax rules through recursion and alternatives. While simple, BNF has limitations like an inability to define length restrictions or require order of elements. Overall, BNF provides a standardized notation for precisely specifying a language's syntactic structure.

Uploaded by

kakkabura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views14 pages

Backus-Naur Form

BNF is a metalanguage used to formally describe the grammar of programming languages. It uses symbols like ::=, |, <>, and brackets to concisely define syntax rules through recursion and alternatives. While simple, BNF has limitations like an inability to define length restrictions or require order of elements. Overall, BNF provides a standardized notation for precisely specifying a language's syntactic structure.

Uploaded by

kakkabura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

BNF

By: Asim Ali Khan

18-May-15
Metalanguages

 A metalanguage is a language used to talk about a language


(usually a different one)
 We can use English as its own metalanguage (e.g. describing English
grammar in English)
 We need a formal, precise means of describing the syntax of
programming languages
 For decades, BNF has met that need
 One of the irritating things about Java is that it has no official BNF
definition—hence, it‟s sometimes hard to tell what is legal syntax
 It is essential to distinguish between the metalanguage terms
and the object language terms
 For example, BNF uses the | symbol, as do many programming
languages—so if we see a |, what is it?
BNF

 BNF stands for either Backus-Naur Form or Backus Normal


Form
 BNF is a metalanguage that is frequently used to describe the
grammar of a programming language
 BNF is formal and precise
 BNF is essential in compiler construction
 If you know about “context-free grammars (CFGs),” BNF is
one way of defining CFGs
 There are many dialects of BNF in use, but…
 …the differences are almost always minor
BNF metasymbols
 Anything enclosed in < > is a nonterminal that needs to be further
expanded, e.g. <variable>
 That is, if you see <variable> in the description of a programming
language, that means “a variable goes here”
 Symbols not enclosed in < > are terminals; they represent
themselves, e.g. if, while, (
 That is, if you see while in the description of a programming language,
that means “the actual word „while‟ goes here”
 The symbol ::= means is defined as
 The symbol | means or; it separates alternatives, for example,
<addop> ::= + | -
 That is, if you see an <addop>, it means “either a „+‟ or a „-‟ goes here”
BNF uses recursion
 <integer> ::= <digit> | <integer> <digit>
or
<integer> ::= <digit> | <digit> <integer>
 Many people find recursion confusing
 “Extended BNF” (which we‟ll talk about shortly)
allows repetition as well as recursion
 Repetition is often easier to implement (with a loop)
than recursion, so Extended BNF has become popular
BNF Examples I
 <digit> ::=
0|1|2|3|4|5|6|7|8|9
 This defines the metasymbol „<digit>‟
 What it means is, “If the description of the syntax says „<digit>‟, you need
an actual digit in this location”
 <if statement> ::=
if ( <condition> ) <statement>
| if ( <condition> ) <statement>
else <statement>
 The symbols if, (, ), and else are terminals—they stand for themselves
 <if statement>, <condition>, and <statement> are metasymbols
 If you see an <if statement>, you should replace it by either
if (<condition>) <statement>
or with
if (<condition>) <statement> else <statement>
 Next, you need to replace <condition> and <statement> with their
definitions
BNF Examples II
 <unsigned integer> ::=
<digit> | <unsigned integer> <digit>

 <integer> ::=
<unsigned integer>
| + <unsigned integer>
| - <unsigned integer>
BNF Examples III

 <identifier> ::=
<letter>
| <identifier> <letter>
| <identifier> <digit>

 <block> ::= { <statement list> }

 <statement list> ::=


<statement>
| <statement list> <statement>
BNF Examples IV
 <statement> ::=
<block>
| <assignment statement>
| <break statement>
| <continue statement>
| <do statement>
| <for loop>
| <goto statement>
| <if statement>
| ...
Extended BNF

 Dialects differ, but the following are pretty standard:


 [ ] enclose an optional part of the rule
 Example:
<if statement> ::=
if ( <condition> ) <statement>
[ else <statement> ]

 { } mean the enclosed can be repeated any number of


times (including zero)
 Example:
<parameter list> ::= ( )
| ( { <parameter> , } <parameter> )
Variations
 The preceding notation is the original and most
common notation
 BNF was designed before we had boldface, color, more
than one font, etc.
 A typical modern variation might:
 Use boldface to indicate multi-character terminals
 Quote single-character terminals (because boldface isn‟t so
obvious in this case)
 Example:
 if_statement ::=
if "(" condition ")" statement [ else statement ]
Limitations of BNF
 No easy way to impose length limitations, such as
maximum length of variable names
 No way to impose distributed requirements, such as, a
variable must be declared before it is used
 Describes only syntax, not semantics
 Nothing clearly better has been devised
 The meta-symbols of BNF are:::=meaning "is defined
as"|meaning "or"< >angle brackets used to surround
category names.The angle brackets distinguish syntax
rules names (also called non-terminal symbols) from
terminal symbols which are written exactly as they are
to be represented.
 A BNF rule defining a nonterminal has the
form:nonterminal ::= sequence_of_alternatives
consisting of strings of terminals or nonterminals
separated by the meta-symbol | For example, the BNF
production for a mini-language is:<program> ::=
program <declaration_sequence> begin
<statements_sequence> end ; This shows that a mini-
language program consists of the keyword "program"
followed by the declaration sequence, then the keyword
"begin" and the statements sequence, finally the
keyword "end" and a semicolon.(end of quotation)

You might also like