Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bachus Naur Form

AllenHeard
November 02, 2016

Bachus Naur Form

Year 13 Lesson

AllenHeard

November 02, 2016
Tweet

More Decks by AllenHeard

Other Decks in Education

Transcript

  1. Syntax ▪ The syntax of a high level language is

    the set of rules that has to be obeyed when a program is written. These rules must be unambiguous, every instruction must have a single meaning. ▪ The computer must not have to make any decisions about what actions it is to perform. ▪ The syntax of a high level language can be defined in two ways: – Syntax diagrams – Backus Naur Form (BNF)
  2. Syntax Diagrams ▪ A diagrammatic way of expressing the syntax

    of a language. ▪ For example, the PASCAL definition of an identifier is: ▪ Any path from left to right spells out a valid identifier. e.g. FRED, F3, R2D2 are all valid identifiers but 3D and 77 are not.
  3. The Bachus Naur Form (BNF) Metalanguage ▪ The BNF (Backus

    Normal Form or Backus-Naur Form) metalanguage has been in widespread use for defining the syntax of programming languages and other computing notations since it was introduced in the late 1950s, during the development of Algol 60. ▪ Technically, BNF is a notation for defining context-free grammars, formal sets of rules for defining and structuring the sets of strings and phrases that are associated with a language.
  4. BNF ▪ BNF defines the syntax of a high level

    language using these symbols: ▪ Example definition: <hexdigit> ::= 0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F ::= ‘is defined by’ | OR <> Define a meta variable
  5. BNF ▪ One definition can then be used as part

    of another: <hexnumber> ::= <hexdigit> | <hexdigit> <hexnumber> ▪ This is an example of a recursive definition - one where the definition uses the thing being defined. <hexdigit> ::= 0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F
  6. BNF ▪ Which are the recursive definitions? 1 <digit> ::=

    0|1|2|3|4|5|6|7|8|9 2 <letter> ::= A|B|C|D|E|......|X|Y|Z 3 <integer> ::= <digit> | <digit> <integer> 4 <word> ::= <letter> | <letter> <word> 5 <variable> ::= <letter> | <letter> <integer> 6 <relation> ::= <|>|= 7 <condition> ::= <variable> <relation> <integer>
  7. Parsing ▪ Part of the task of a compiler is

    to check the syntax of every statement in a high level language program. (remember the second stage of compilation is 'Syntax Analysis') ▪ Parsing is testing whether the rules of syntax have been obeyed. ▪ Example of parsing (using the previous definitions) - Test if 'B8>145' is a valid 'condition'. Rules used: condition 7 variable relation integer 5,3 letter integer digit integer 3 digit integer 3 digit 2,1,6 B 8 > 1 4 5
  8. Task ▪ Using the definitions above as a guide. Create

    a new set of definitions for a <password> that follows the following rules: – can contain any number of characters – of which the first and last characters must be numbers – the remaining characters can be either letters or numbers 1 <digit> ::= 0|1|2|3|4|5|6|7|8|9 2 <letter> ::= A|B|C|D|E|......|X|Y|Z 3 <integer> ::= <digit> | <digit> <integer> 4 <word> ::= <letter> | <letter> <word> 5 <variable> ::= <letter> | <letter> <integer> 6 <relation> ::= <|>|= 7 <condition> ::= <variable> <relation> <integer>
  9. Solution 1 <digit> ::= 0|1|2|3|4|5|6|7|8|9 2 <letter> ::= A|B|C|D|E|......|X|Y|Z 3

    <text> ::= <digit>|<letter>|<text> 4 <password> ::= <digit><text><digit>
  10. Example Syntax Diagram ▪ The syntax diagram opposite highlights definitions

    for the syntax of a language consisting of the components a, b, c where the following are allowed. ▪ a or (b followed by c) ▪ (a or b) followed by c
  11. Task ▪ A warehouse stores a large number of mobile

    phone components. Each component has a component code consisting of 6 digits and a description. The description is made up of upper case letters only and may be of any length. ▪ Examples of valid codes are: – 621402DIGITISER – 506742USBPORT ▪ (i) Describe the purpose of Backus-Naur Form (BNF). ▪ (ii) Produce an appropriate BNF definition for a component code. ▪ (iii) Produce an appropriate syntax diagram for a component code.
  12. Answers ▪ (i) BNF is used to describe (unambiguously) the

    syntax / grammar / rules of a programming / computer language ▪ (ii) ▪ (iii)