Programming Paradigms Backus Naur Form and Syntax Diagrams.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
Grade 10 Mathematics Rational Numbers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
COMP205 Comparative Programming Languages Part 1: Introduction to programming languages Lecture 2: Structure of programs and programming languages as communication.
30-Jun-15 BNF. Metalanguages A metalanguage is a language used to talk about a language (usually a different one) We can use English as its own metalanguage.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
2-7: Square Roots and Real Numbers © William James Calhoun, 2001 OBJECTIVE: You must be able to find a square root, classify numbers, and graph solution.
Elementary Algebra Exam 4 Material Exponential Expressions & Polynomials.
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Chapter 2 - Algorithms and Design
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
When we add or subtract integers we can use a number line to help us see what is happening with the numbers.
Syntax Specification and BNF © Allan C. Milne Abertay University v
Physics Day 5 Objectives SWBAT do exponential math Understand factors of 10 Agenda Do Now Notes Worksheet HW2 due tonight HW3 Thursday.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
CPS 506 Comparative Programming Languages Syntax Specification.
1. Understand the application of Pseudo Code for programming purposes 2. Be able to write algorithms in Pseudo Code.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
What is this? : : = Definition of a word. A word is comprised of 2 letters.
Define an integer Whole number? Multiple digits? From 0 – 9?
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Recursion.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
Chapter 3 – Describing Syntax
Chapter 4 – C Program Control
RECURSION.
C++ Basic Syntax – Homework Exercises
What does it mean? Notes from Robert Sebesta Programming Languages
Revision Lecture
Deterministic Finite Automata
Compiler Construction (CS-636)
Backus Naur form.
3.5 Programming paradigms
Deterministic Finite Automata
Context-Free Grammars
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Stacks & Recursion.
R.Rajkumar Asst.Professor CSE
Theory of Computation Languages.
BNF 23-Feb-19.
What are the names of the Meta Languages you have used?
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Chapter 3 Describing Syntax and Semantics.
High-Level Programming Language
Matlab Basics.
Programming Languages
Getting Started With Coding
Presentation transcript:

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.

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

Backus Naur Form (BNF)

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:

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

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.

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.

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

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 ::= ::= = =

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.

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

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

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

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

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

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

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.

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.

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 ::= _ ::= _

BNF definition of a real number E.g ::=. ::=. ::= ::=

Defining integers to stop leading 0’s E.g is not allowed 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.

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 ::= | ::= |

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

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.

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