SlideShare a Scribd company logo
1 of 13
Jul 7, 2014
BNF
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)
 It is essential to distinguish between the
metalanguage terms and the object language terms
BNF
 BNF stands for either Backus-Naur Form or Backus
Normal Form
 BNF is a metalanguage used to describe the
grammar of a programming language
 BNF is formal and precise
 BNF is a notation for context-free grammars
 BNF is essential in compiler construction
 There are many dialects of BNF in use, but…
 …the differences are almost always minor
BNF
 < > indicate a nonterminal that needs to be further
expanded, e.g. <variable>
 Symbols not enclosed in < > are terminals; they
represent themselves, e.g. if, while, (
 The symbol ::= means is defined as
 The symbol | means or; it separates alternatives, e.g.
<addop> ::= + | -
 This is all there is to “plain” BNF; but we will discuss
extended BNF (EBNF) later in this lecture
BNF uses recursion
 <integer> ::= <digit> | <integer> <digit>
or
<integer> ::= <digit> | <digit> <integer>
 Recursion is all that is needed (at least, in a formal
sense)
 "Extended BNF" allows repetition as well as recursion
 Repetition is usually better when using BNF to
construct a compiler
BNF Examples I
 <digit> ::=
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
 <if statement> ::=
if ( <condition> ) <statement>
| if ( <condition> ) <statement>
else <statement>
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
 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 easy way to describe ranges, such as 1 to 31
 No way at all 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 End
http://cuiwww.unige.ch/db-research/Enseignement/analyseinfo/AboutBNF.html

More Related Content

What's hot (20)

Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Language
LanguageLanguage
Language
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentation
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C++
C++C++
C++
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
History of C#
History of C#History of C#
History of C#
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
c++
 c++  c++
c++
 

Viewers also liked

Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
 
Introduction to compiler construction
Introduction to compiler constructionIntroduction to compiler construction
Introduction to compiler constructionYuji Shimojo
 

Viewers also liked (6)

Bnf and ambiquity
Bnf and ambiquityBnf and ambiquity
Bnf and ambiquity
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 
Lecture 21 22
Lecture 21 22Lecture 21 22
Lecture 21 22
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
 
Introduction to compiler construction
Introduction to compiler constructionIntroduction to compiler construction
Introduction to compiler construction
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 

Recently uploaded

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 

Recently uploaded (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 

25 bnf

  • 2. 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)  It is essential to distinguish between the metalanguage terms and the object language terms
  • 3. BNF  BNF stands for either Backus-Naur Form or Backus Normal Form  BNF is a metalanguage used to describe the grammar of a programming language  BNF is formal and precise  BNF is a notation for context-free grammars  BNF is essential in compiler construction  There are many dialects of BNF in use, but…  …the differences are almost always minor
  • 4. BNF  < > indicate a nonterminal that needs to be further expanded, e.g. <variable>  Symbols not enclosed in < > are terminals; they represent themselves, e.g. if, while, (  The symbol ::= means is defined as  The symbol | means or; it separates alternatives, e.g. <addop> ::= + | -  This is all there is to “plain” BNF; but we will discuss extended BNF (EBNF) later in this lecture
  • 5. BNF uses recursion  <integer> ::= <digit> | <integer> <digit> or <integer> ::= <digit> | <digit> <integer>  Recursion is all that is needed (at least, in a formal sense)  "Extended BNF" allows repetition as well as recursion  Repetition is usually better when using BNF to construct a compiler
  • 6. BNF Examples I  <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  <if statement> ::= if ( <condition> ) <statement> | if ( <condition> ) <statement> else <statement>
  • 7. BNF Examples II  <unsigned integer> ::= <digit> | <unsigned integer> <digit>  <integer> ::= <unsigned integer> | + <unsigned integer> | - <unsigned integer>
  • 8. BNF Examples III  <identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>  <block> ::= { <statement list> }  <statement list> ::= <statement> | <statement list> <statement>
  • 9. BNF Examples IV  <statement> ::= <block> | <assignment statement> | <break statement> | <continue statement> | <do statement> | <for loop> | <goto statement> | <if statement> | . . .
  • 10. Extended BNF  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> )
  • 11. 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 ]
  • 12. Limitations of BNF  No easy way to impose length limitations, such as maximum length of variable names  No easy way to describe ranges, such as 1 to 31  No way at all 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