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)
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.
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.
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
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
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>
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.