1 / 12

BNF

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 BNF is a precise way of describing the syntax of a computer language BNF is really, really easy!. The syntax of BNF.

lundy
Télécharger la présentation

BNF

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • <vowel> ::= a | e | i | o | u • <consonant> ::= b | c | d | f | g | h | j | k | l | m | n | p | q | r | s | t | v | w | x | y | z • <letter> ::= <vowel> | <consonant> • <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 • <hex digit> ::= <digit> | 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) • <unsigned integer> ::= <digit> | <unsigned integer> <digit> • 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 • <variable> ::= <letter> | <variable> <letter> | <variable> <digit> | <variable> _

  7. Length limitations • Suppose that a variable is as before, but can only be six characters long • <variable> ::= <letter> | <letter> <v2> | <letter> <v2> <v2> | <letter> <v2> <v2> <v2> | <letter> <v2> <v2> <v2> <v2> | <letter> <v2> <v2> <v2> <v2> <v2> • <v2> ::= <letter> | <digit> | _ • 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 loop> ::= while ( <condition> ) { <statements> } • <statement> ::= <while loop> | ...other statements... • <statements> ::= <empty> | <statements> <statement> • <empty> ::=

  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 -2147483648 and 2147483647

  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: <integer> ::= [+ | -] <digit> { <digit> }

  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

More Related