1 / 26

Programming Paradigms

Programming Paradigms. Backus Naur Form and Syntax Diagrams. Learning Objectives. Explain the need for, and be able to apply, BNF (Backus-Naur form) and syntax diagrams. B ackus N aur F orm ( BNF ) or Syntax Diagrams. Specify precisely each language’s set of rules

alcina
Download Presentation

Programming Paradigms

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. 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. • 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 • 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: 0 2 415 3040513002976 0000000123

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

  7. What is a digit? • A digit is 0 or 1 or 2 or … or 9. • <digit> ::= 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 (< and >) like <integer> and <digit>. • As the digits 0, 1, 2, … are found in the code and not defined as something else.

  8. BNF definition of a single digit: • <integer> ::= <digit> • <digit> ::= 0|1|2|3|4|5|6|7|8|9 • How are we going to specify integers of any length? • 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 • <integer> ::= <digit><integer> • E.g. 147 • 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 ).

  10. Recursion • This is a recursive definition as integer is defined in terms of itself. • Applying this definition several times produces the sequence • <integer> ::= <digit><integer> =<digit><digit><integer> =<digit><digit><digit><integer>

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

  12. BNF definition of an unsigned integer <unsigned integer> ::= <digit>|<digit><unsigned integer> <digit> ::= 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 <integer> ::= <unsigned integer>|<signed integer> <signed integer> ::= + <unsigned integer>| - <unsigned integer> <unsigned integer> ::= <digit>|<digit><unsigned integer> <digit> ::= 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 A x sum total24 mass_of_product MyAge

  17. Using tail recursion (as with the integer example earlier) <variable> ::= <letter>|<character><variable> <character> ::= <letter>|<digit>|<under-score>

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

  19. Using Head Recursion in this example <variable> ::= <letter>|<variable><character> • 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. <variable> ::= <letter>|<variable><character> <character> ::= <letter>|<digit>|<under-score> <letter> ::= <uppercase>|<lowercase> <uppercase> ::= A|B|C|D|E|F|G|H|I|J|K|ZL|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z <lowercase> ::= a|b|c|d|e|f|g|h|i|j|k|zl|m|n|o|p|q|r|s|t|u|v|w|x|y|z <digit> ::= 0|1|2|3|4|5|6|7|8|9 <under-score> ::= _

  21. BNF definition of a real number • E.g. 0.347 -2.862 +14.34 00235.006 <real number> ::= <integer> . <unsigned integer>

  22. Defining integers to stop leading 0’s • E.g. • 00135 is not allowed • 0 is allowed. • So an integer has to be either: • zero digit • non-zero 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.

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

  24. Syntax Definition of integers to stop leading 0’s This arrow allows the definition to “begin with a 0”. This arrow allows the definition to “begin with a non-zero digit”. 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, a point, and a two digit number or • A point and a two digit number • A positive integer has been defined as <INTEGER> • A digit is defined as <DIGIT>::= 0/1/2/3/4/5/6/7/8/9. • Define, using Backus Naur form, the variable <AMOUNT OF MONEY> • Using the previously defined values of INTEGER and DIGIT, draw a syntax diagram to define AMOUNT OF MONEY.

  26. Plenary • BNF • <AMOUNTOFMONEY>::=$<INTEGER> / $<INTEGER>.<DIGIT><DIGIT> / $.<DIGIT><DIGIT> • Syntax Diagram • AMOUNTOFMONEY $ INTEGER . DIGIT DIGIT

More Related