Presentation is loading. Please wait.

Presentation is loading. Please wait.

BNF. What is BNF? BNF stands for “Backus-Naur Form,” after the people who invented it BNF is a metalanguage--a language used to describe another language.

Similar presentations


Presentation on theme: "BNF. What is BNF? BNF stands for “Backus-Naur Form,” after the people who invented it BNF is a metalanguage--a language used to describe another language."— Presentation transcript:

1 BNF

2 What is BNF? BNF stands for “Backus-Naur Form,” after the people who invented it BNF is a metalanguage--a language used to describe another language BNF is a precise way of describing the syntax of a computer language BNF is really, really easy!

3 The syntax of BNF Any word or words enclosed in angle brackets,, is a nonterminal -- it stands for some string (it’s like a string variable) The symbol ::= means “is defined as” The symbol | means “or” Anything else is a terminal -- it stands for itself That’s all!

4 Examples ::= a | e | i | o | u ::= b | c | d | f | g | h | j | k | l | m | n | p | q | r | s | t | v | w | x | y | z ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= | a | b | c | d | e | f

5 Recursion Suppose that an “unsigned integer” consists of one or more digits (but there is no limit to the number of digits) ::= | BNF makes heavy use of recursion

6 More examples Suppose that (in some computer language) a variable consists of letters, digits, and underscores, but must begin with a letter ::= | | | _

7 Length limitations Suppose that a variable is as before, but can only be six characters long ::= | | | | | ::= | | _ BNF isn’t great for setting limits on lengths!

8 Recursive structures A while loop can contain any number of other statements, including other while loops ::= while ( ) { } ::= |...other statements... ::= | ::=

9 What BNF cannot do BNF can only describe local syntax; it cannot refer to another part of the program –Example: BNF cannot say that variables must be declared before they are used BNF cannot do numeric comparisons –Example: BNF cannot say that int values must be between and

10 Extended BNF After 40 years, BNF is still the preferred way of describing syntax There are two commonly used extensions: –[ ] to indicate the bracketed thing is optional –{ } to indicate zero or more of the enclosed thing Example: ::= [+ | -] { }

11 Trivial Variations Sometimes different fonts are used to distinguish terminals from nonterminals (instead of marking nonterminals with ) Sometimes different symbols are used, such as = for ::= Sometimes definitions end with a period However you write it, it’s still BNF!

12 The End


Download ppt "BNF. What is BNF? BNF stands for “Backus-Naur Form,” after the people who invented it BNF is a metalanguage--a language used to describe another language."

Similar presentations


Ads by Google