Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Paradigms Backus Naur Form and Syntax Diagrams.

Similar presentations


Presentation on theme: "Programming Paradigms Backus Naur Form and Syntax Diagrams."— Presentation transcript:

1 Programming Paradigms Backus Naur Form and Syntax Diagrams

2 Learning Objectives Explain the need for, and be able to apply, BNF (Backus-Naur form) and syntax diagrams.

3 Backus Naur Form (BNF) or Syntax Diagrams Specify precisely each language’s set of rules Each language uses a different set. Each language uses a different set. So for example a Visual Basic compiler would not understand C++ syntax and vice versa. So for example a Visual Basic compiler would not understand C++ syntax and vice versa. E.g. A loop statement: Visual Basic: For count = 1 To 10 Visual Basic: For count = 1 To 10 C++: for (count = 1, count <= 10, count++) C++: for (count = 1, count <= 10, count++)

4 Backus Naur Form (BNF)

5 All languages use integers An integer is a sequence of the digits 0, 1, 2, …, 9. The number of digits in an integer is arbitrary as it could be any number. Valid integers could be: 0241530405130029760000000123

6 What is an integer? An integer can be a single digit and so: ::= ::= However, what is a digit? See next slide for answer. See next slide for answer. defined as

7 What is a digit? A digit is 0 or 1 or 2 or … or 9. ::= 0|1|2|3|4|5|6|7|8|9 ::= 0|1|2|3|4|5|6|7|8|9 The vertical line is read as or. Note: All the digits have to be specified and that they are not inside angle brackets ( ) like and. All the digits have to be specified and that they are not inside angle brackets ( ) like and. As the digits 0, 1, 2, … are found in the code and not defined as something else. As the digits 0, 1, 2, … are found in the code and not defined as something else.

8 BNF definition of a single digit: ::= ::= ::= 0|1|2|3|4|5|6|7|8|9 ::= 0|1|2|3|4|5|6|7|8|9 How are we going to specify integers of any length? See next slide. See next slide.

9 How are we going to specify integers of any length? All integers of more than one digit start with a single digit and are followed by an integer and the final integer is a single digit integer. An indefinitely long integer is defined as ::= ::= E.g. 147 Is a single digit integer ( 1 ) followed by the integer 47. Is a single digit integer ( 1 ) followed by the integer 47. 47 is a single digit integer ( 4 ) followed by a single digit integer ( 7 ). 47 is a single digit integer ( 4 ) followed by a single digit integer ( 7 ).

10 Tail Recursion This is a recursive definition as integer is defined in terms of itself. It is known as Tail recursion as it happens at the end (tail). It is known as Tail recursion as it happens at the end (tail). Applying this definition several times produces the sequence ::= ::= = =

11 Solving Recursion To stop this we use the fact that, eventually, is a single digit: ::= | ::= | i.e. is a or a followed by an. is a or a followed by an. At any time can be replaced by and the recursion stops. At any time can be replaced by and the recursion stops.

12 BNF definition of an unsigned integer ::= | ::= | ::= 0|1|2|3|4|5|6|7|8|9 ::= 0|1|2|3|4|5|6|7|8|9 unsigned integer as this definition does not deal with a leading plus sign ( + ) or minus sign ( - )

13 Syntax diagram definition of an unsigned integer This arrow allows the definition to “loop around digit”. This arrow allows the definition to “loop around digit”.

14 BNF full definition of an signed integer in BNF Signed integer = unsigned integer preceded by a + or – sign by a + or – sign ::= | ::= | ::= +|- ::= +|- ::= | ::= | ::= | ::= | ::= 0|1|2|3|4|5|6|7|8|9 ::= 0|1|2|3|4|5|6|7|8|9

15 Syntax Diagram full definition of an unsigned or a signed integer in BNF This arrow allows the definition to “omit a + or - sign”.

16 Another example of defining using BNF and a syntax diagram Suppose we define a variable as a sequence of one or more characters starting with a letter. The characters can be any letter, digit or the underscore. Valid examples are Axsumtotal24mass_of_productMyAge

17 Using tail recursion (as with the integer example earlier) ::= | ::= | ::= | | ::= | |

18 Problem with Tail Recursion in this example 2Sum is valid (it shouldn’t be!) as (tail recursion) allows it. i.e. = 2 and = Sum = 2 and = Sum 2, S and u for and then m for. 2, S and u for and then m for. Tail recursion forces the variable to end with a letter not start with one. Tail recursion forces the variable to end with a letter not start with one.

19 Using Head Recursion in this example ::= | ::= | So the last time it is called it will be a letter and this will be at the head of the variable.

20 Full Definition of a variable which is a sequence of one or more characters starting with a letter. ::= | ::= | ::= | | ::= | | ::= | ::= | ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z ::= 0|1|2|3|4|5|6|7|8|9 ::= 0|1|2|3|4|5|6|7|8|9 ::= _ ::= _

21 BNF definition of a real number E.g.0.347-2.862+14.3400235.006 ::=. ::=. ::= ::=

22 Defining integers to stop leading 0’s E.g. 00135 is not allowed 00135 is not allowed 0 is allowed. 0 is allowed. So an integer has to be either: zero digit zero digit non-zero digit non-zero digit non-zero digit followed by any digit. non-zero digit followed by any digit. Meaning an integer is defined as: zero or digits where digits must start with a non-zero digit. zero or digits where digits must start with a non-zero digit.

23 BNF definition of integers to stop leading 0’s ::= | ::= | must be a single non-zero digit or a non-zero digit followed by any digits. must be a single non-zero digit or a non-zero digit followed by any digits. ::= | ::= | ::= 0 ::= 0 ::= 1|2|3|4|5|6|7|8|9 ::= 1|2|3|4|5|6|7|8|9 ::= | ::= |

24 Syntax Definition of integers to stop leading 0’s This arrow allows the definition to “begin with a non- zero digit”. This arrow allows the definition to “begin with a 0”. This arrow allows the definition to “omit a second digit”.

25 Plenary An amount of money can be defined as A$ sign followed by either A positive integer or A positive integer or A positive integer, a point, and a two digit number or A positive integer, a point, and a two digit number or A point and a two digit number A point and a two digit number A positive integer has been defined as A positive integer has been defined as A digit is defined as ::= 0/1/2/3/4/5/6/7/8/9. Define, using Backus Naur form, the variable Define, using Backus Naur form, the variable Using the previously defined values of INTEGER and DIGIT, draw a syntax diagram to define AMOUNT OF MONEY.

26 Plenary BNF ::= $ ::= $ ::=. ::=. ::= | | ::= | | Syntax Diagram AMOUNTOFMONEY AMOUNTOFMONEY $ INTEGER. DIGIT DIGIT


Download ppt "Programming Paradigms Backus Naur Form and Syntax Diagrams."

Similar presentations


Ads by Google