Exercises Software Development I. 02 Algorithm Testing & Language Description Manual inspection, test plan, grammar, metasyntax notations (BNF, EBNF)

Similar documents
Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Syntax. A. Bellaachia Page: 1

CPS 506 Comparative Programming Languages. Syntax Specification

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Lecture 4: Syntax Specification

Week 2: Syntax Specification, Grammars

This book is licensed under a Creative Commons Attribution 3.0 License

Syntax. In Text: Chapter 3

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

Structured Programming. Jon Macey

Programming Logic and Design Sixth Edition

Programming Language Definition. Regular Expressions

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

3. Context-free grammars & parsing

Lecture 10 Parsing 10.1

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

AIMMS Function Reference - Date Time Related Identifiers

Visual Basic for Applications

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Institute For Arts & Digital Sciences SHORT COURSES

CSE 3302 Programming Languages Lecture 2: Syntax

Objectives/Outcomes. Introduction: If we have a set "collection" of fruits : Banana, Apple and Grapes.

Programming Lecture 3

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

INFORMATION TECHNOLOGY SPREADSHEETS. Part 1

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

CSCI312 Principles of Programming Languages!

6. Control Statements II

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CSE 305 Programming Languages Spring, 2010 Homework 5 Maximum Points: 24 Due 10:30 AM, Friday, February 26, 2010

Calendar Excel Template User Guide

Chapter 3. Describing Syntax and Semantics

Scheduling. Scheduling Tasks At Creation Time CHAPTER

September 2015 Calendar This Excel calendar is blank & designed for easy use as a planner. Courtesy of WinCalendar.com

Complex data structures. Cedric Saule

Dr. D.M. Akbar Hussain

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

Calendar PPF Production Cycles Non-Production Activities and Events

COP 3402 Systems Software Syntax Analysis (Parser)

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

CHAPTER 5 FLOW OF CONTROL

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Math in Focus Vocabulary. Kindergarten

Syntax and Grammars 1 / 21

Sequential Search (Searching Supplement: 1-2)

CSCE 314 Programming Languages

Describing Syntax and Semantics

Informatica 3 Syntax and Semantics

CS101 Lecture 04: Binary Arithmetic

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Building Compilers with Phoenix

MONITORING REPORT ON THE WEBSITE OF THE STATISTICAL SERVICE OF CYPRUS DECEMBER The report is issued by the.

Chapter 6 Reacting to Player Input

ADVANCED ALGORITHMS TABLE OF CONTENTS

Read and fill in this page now

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

Computer Grade 5. Unit: 1, 2 & 3 Total Periods 38 Lab 10 Months: April and May

Basic Device Management

DEPARTMENT OF ACADEMIC UPGRADING

EECS 6083 Intro to Parsing Context Free Grammars

DECISION CONTROL AND LOOPING STATEMENTS

Information Science 1

Consider a description of arithmetic. It includes two equations that define the structural types of digit and operator:

afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

We first learn one useful option of gcc. Copy the following C source file to your

Lexical Considerations

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

Software Testing. 1. Testing is the process of demonstrating that errors are not present.

Chapter 3. Describing Syntax and Semantics

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Aerospace Software Engineering

Organizing and Summarizing Data

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

B.2 Measures of Central Tendency and Dispersion

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

YEAR 8 STUDENT ASSESSMENT PLANNER SEMESTER 1, 2018 TERM 1

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

HP Project and Portfolio Management Center

CALENDAR OF FILING DEADLINES AND SEC HOLIDAYS

Introduction to Parsing. Lecture 8

Syntax Intro and Overview. Syntax

Highline Excel 2016 Class 09: Date Functions

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Homework & Announcements

Programming Language Concepts, cs2104 Lecture 04 ( )

CS Compiler Construction West Virginia fall semester 2014 August 18, 2014 syllabus 1.0

Programming Assignment #2

Formative Benchmark 1

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Formal Languages. Formal Languages

Transcription:

Exercises Software Development I 02 Algorithm Testing & Language Description Manual inspection, test plan, grammar, metasyntax notations (BNF, EBNF) October 15th, 2014 Software Development I Winter term 2014/2015 Priv.-Doz. Dipl.-Ing. Dr. Andreas Riener Institute for Pervasive Computing Johannes Kepler University Linz riener@pervasive.jku.at

I Remember: Slides marked this way contain important concepts and should be carefully reviewed! Algorithm Testing: Manual Inspection In order to understand an algorithm (especially one written by another person), it is necessary to understand how it works. To get an idea of its structure, operation sequences, etc. (i.e., what it does ), manual inspection is a reasonable approach Code review Dry run ( Schreibtischtest ) Procedure Put yourself in the place of a computer and pass through the algorithm in a gradual manner (step by step) Examine all important variables and note down their values on specific, neuralgic code positions (e.g., before a branch, in the body of a loop, etc.) Software Development I // Exercises // 02 Testing and Language Description // 2

I Example: Manual Inspection of Algorithm Sum Sum() Input: 1, 9, 6, 8, 0 sum = 0 read( number) while number > 0 sum = sum + number read( number) write( sum) Position 0 Position 1 Position 2 Variables: sum, number Neuralgic code positions Result: Position sum number 0 0??? 1 0 1 2 1 9 2 10 6 2 16 8 2 24 0 Software Development I // Exercises // 02 Testing and Language Description // 3

Manual Code Inspection: Advantages and Disadvantages Advantages Accurate Warranty to find and identify all errors ( bugs ) Disadvantages Time consuming Not feasable as it comes to complex algorithms (e.g., deep recursions, nested loops, etc.) advice: simulate only central parts of an algorithm manually/by hand Software Development I // Exercises // 02 Testing and Language Description // 4

Algorithm Testing: Automatic Inspection Source-level debuggers can be used for assisted or automatic inspection of algorithms Step by step execution Variable inspection Flow control Breakpoint toggling etc. Live Demo Debugging of Sum.java Debug view of Sum.java in the Eclipse framework Software Development I // Exercises // 02 Testing and Language Description // 5

Algorithm Testing: Test Plan for Sum Quite simple to generate: Input Expected Output Program Results 0 0-1 0 any integers 1 9 6 8 0 1 9 0 6 8 10 sum of the integers up to the first nonpositive value 24. Text as input, for example characters Non integer numbers 1.0000...... Tell the user this is an invalid input and ask them to re-enter a valid integer: e.g., Value not valid, re-enter: Tell the user this is an invalid input and ask them to re-enter a valid integer: e.g., Value not valid, re-enter: Software Development I // Exercises // 02 Testing and Language Description // 6

Algorithm Testing: Test Plan for Sum Quite simple to generate: Input Expected Output Program Results 0 0 0-1 0 0 any integers 1 9 6 8 0 Text as input, for example characters Non integer numbers 1.0000 sum of the integers up to the first nonpositive value 24 Why? Should this not cause a These errors are covered by the Input class. If you crash, use program error, etc.? System.in.* 1 9 0 6 8 to read-in variable 10 values, you have to 10 take. care of input errors by yourself. For that reason, you are requested to cover all possible input errors already now in your programs! Tell the user this is an invalid input and ask them to re-enter a valid integer: e.g., Value not valid, re-enter: Tell the user this is an invalid input and ask them to re-enter a valid integer: e.g., Value not valid, re-enter:......... 24 Not a valid int, please try again: Not a valid int, please try again: Software Development I // Exercises // 02 Testing and Language Description // 7

I SWE1 UE02 PrimeFact.zip Algorithm Testing: Prime Factor Decomposition (PrimeFact.java) Algorithm We are looking for an algorithm that determines and displays the prime factors of a number entered by the user. For more details on the prime factor decomposition see http://de.wikipedia.org/wiki/primfaktorzerlegung Input: positive integer numbers Output: prime factors of the entered number, in ascending order Examples Input: 2 Output: 2 Input: 6 Output: 2 * 3 (2x3) Ask yourself What are special cases of this algorithm? Try to generate a complete table Based on this table, create the test plan for the algorithm Software Development I // Exercises // 02 Testing and Language Description // 8

I Algorithm Testing: Prime Factor Decomposition (PrimeFact.java) Group 1: Special cases, boundary values -1 invalid input (only positive integers); has no prime factors 0 has no prime factors 1 has no prime factors 2 smallest value with prime factors (only one) 3 number with one prime factor 4 smallest input with two prime factors (same) 6 smallest value with two different prime factors 8 smallest value with more than 2 prime factors 256 multiple, same prime factors 8085 multiple prime factors, different size 1031 greater number with only one prime factor (= prime number) Group 2: Illegal inputs -1 invalid (negativ) hallo not a number 123hallo not a number 1.12 not integer 2147483648 exceeds max. input number Software Development I // Exercises // 02 Testing and Language Description // 9

I Algorithm Testing: Test Plan for PrimeFact.java Aim (Group 1) Input Expected output Program output smallest number w/o prime factor 0 0 has no prime factors? first number >0 with no prime factors 1 1 has no prime factors? smallest value with one prime factor 2 2? second value with one prime factor 3 3? smallest value with two prime factors 4 2 * 2? smallest value with two different prime factors 6 2 * 3? smallest value with more than 2 prime factors 8 2 * 2 * 2? smallest value with more than two prime factors (> 2) 27 3 * 3 * 3? number with multiple (equal) prime factors 256 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2? number with mutiple different prime factors 8085 3 * 5 * 7 * 7 * 11? larger number with only one prime factor 1031 1031? Software Development I // Exercises // 02 Testing and Language Description // 10

I Algorithm Testing: Test Plan for PrimeFact.java (2) Aim (Group 1, cont.) Input Expected output Program output common case 10 2 * 5? common case 6534 2 * 3 * 3 * 3 * 11 * 11? common case 13332 2 * 2 * 3 * 11 * 101? Aim (Group 2) Input Expected output Program output negative number (smallest) -1 invalid, no prime factors? invalid, text instead of integer hallo valid number required? invalid, valid number folllowed by text 123hallo valid number required? invalid, float value 123.789 valid number required? no/empty input (finalized with \n) valid number required? largest integer number 2147483647 2147483647? largest integer number + 1 2147483648 valid number required? largest negative integer number -2147483648 invalid, no prime factors? Software Development I // Exercises // 02 Testing and Language Description // 11

I Algorithm Testing: Test Plan...highly relevant for assignments in most of the exercise sheets we call for the inclusion of a test plan. Before actually testing your program (i.e., implementation of an algorithm) you have to think about WHICH behavior, what special cases should be tested? HOW to reach these cases in the program (input values)? WHAT is the expected result (i.e., output values)? Testplan (tabular form) should contain Case, input, expected result, program output for some normal cases all (!) special cases and boundary values (over-/underflow, division by zero, infinity, etc.) illegal input configurations For additional information see http://www.software-kompetenz.de/?21531 (German language only) Software Development I // Exercises // 02 Testing and Language Description // 12

(Programming) Language Description: Metasyntax

Programming Languages: Representation / Description Notation of algorithms (see last week!) Textual form (prose) Graphical representation (Nassi-Shneiderman diagram) flow chart structure chart Programming language The programming language itself calls also for a representation/description Grammar = syntactical rules of a (programming) language Syntax = sentence structure, order of symbols, instructions Semantics = the meaning of a program, code fragment, expression Such a rule base enables the compiler to detect and announce programming errors/mistakes already at compile time Software Development I // Exercises // 02 Testing and Language Description // 14

I Language Descriptions: Types of Notations Metasyntax notations Backus-Naur-Form (BNF) Extended Backus-Naur-Form (EBNF) BNF vs. EBNF BNF is a formal, mathematical oriented notation form to describe the syntax of the programming language ALGOL60 BNF only defines terminal symbols, nonterminal symbols (production rules) and alternatives EBNF is an extension of BNF to better (easier) define complex syntax (take use of e.g. recursive definitions) - EBNF extends BNF by adding additional constructs (see next slide) - readability of EBNF as apposed to BNF is much improved as well - functional range of BNF is similar to EBNF (each EBNF description can also be represented with BNF) Syntax diagram Finite-state machine more details on Turing machines etc. in other lectures Software Development I // Exercises // 02 Testing and Language Description // 15

I Metasyntax Notation: Example Positive Integers in BNF A positive Integer number is represented by at least one, but in general an arbitrary number of digits; no leading sign; zero (0) number included, e.g. 12 00 3140 0000100007 Nonterminal symbols Alternative RECURSION PosIntBNF = number number PosIntBNF number = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" Terminal symbols Software Development I // Exercises // 02 Testing and Language Description // 16

I Metasyntax Notation: Extended Backus-Naur-Form (EBNF) Terminal symbols are literal, final characters that cannot be broken down into smaller units (also called tokens ) if, while, for, int, class, etc. special characters, such as ;,. - % = " + Nonterminal symbols. Using production rules, nonterminal symbols can be refined by different combinations of terminal symbols, further nonterminal symbols and by using meta-symbols, e.g. Paragraph = Sentence { Sentence } Sentence = Subject Predicate Object EBNF defines several meta symbols also called control symbols : =,., [], {},, () (as opposed to BNF, which only defines the alternative or ) = Production rule. End of production rule [] occurrence: one time or never {} occurrence: optional (0, 1,, ) alternative (left or right side) () precedence (group for priority) Software Development I // Exercises // 02 Testing and Language Description // 17 Valid meta symbols in EBNF.

I Metasyntax Notation: Example Positive Integers in EBNF A positive Integer number is represented by at least one, but in general an arbitrary number of digits; no leading sign; zero (0) number included, e.g. 12 00 3140 0000100007 altered representation of the recursion production rules are terminated by a period PosIntEBNF = number {number}. number = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9". Software Development I // Exercises // 02 Testing and Language Description // 18

Derivation Tree (BNF): Example Positive Integers PosIntBNF = number number PosIntBNF number = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" Wanted: Derivation tree for the number 3140 PosIntBNF number PosIntBNF 3 number PosIntBNF 3 1 number PosIntBNF 3 1 4 number 3 1 4 0 3140 Software Development I // Exercises // 02 Testing and Language Description // 19

Metasyntax Notation: Examples for BNF, EBNF Representation Example 1: Decimal numbers A decimal number is positive or negative has a leading minus sign or is unsigned has any number of pre-decimal places (but at least 1) has an optional decimal symbol (".") followed by any number of decimal places (again at least one digit) Examples 2-12.1 3.1415-122443.00 Software Development I // Exercises // 02 Testing and Language Description // 20

Metasyntax Notation: Examples for BNF, EBNF Representation BNF representation: Decimal number BNFDec = "-" Decnumber Decnumber Decnumber = Digitlist Digitlist "." Digitlist Digitlist = Digit Digit Digitlist Digit = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" Software Development I // Exercises // 02 Testing and Language Description // 21

Metasyntax Notation: Examples for BNF, EBNF Representation EBNF representation: Decimal number EBNFDec = ["-"] Digitlist ["." Digitlist]. Digitlist = Digit {Digit}. Digit = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9". Software Development I // Exercises // 02 Testing and Language Description // 22

Derivation Tree (BNF, EBNF): Example Decimal Numbers BNFDec = "-" Decnumber Decnumber Decnumber = Digitlist Digitlist "." Digitlist Digitlist = Digit Digit Digitlist Digit = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" EBNFDec = ["-"] Digitlist ["." Digitlist]. Digitlist = Digit {Digit}. Digit = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9". Try on your own: Derivation tree for the number PI (3.1415) in BNF or EBNF Software Development I // Exercises // 02 Testing and Language Description // 23

Derivation Tree (BNF, EBNF): Example Decimal Numbers BNF representation: 3.1415 Software Development I // Exercises // 02 Testing and Language Description // 24

Derivation Tree (BNF, EBNF): Example Decimal Numbers EBNF representation: 3.1415 Software Development I // Exercises // 02 Testing and Language Description // 25

Metasyntax Notation: Examples for BNF, EBNF Representation Example 2: Date formats (Prose) Two date formats: (a) short form, (b) extensive version Short form: Day (numeric) followed by the month (numeric), and finally the year (numeric, last two digits only); the three values are separated by a. or / symbol (mixed usage not allowed) Extensive version: Identifier of the day of the week (English language) followed by a comma; after that, the date is continued with a month identifier (English language) and the day of the month in numeric format; after another comma the year (numeric, last two digits only) concludes a date In both notations it is allowed to insert any number of spaces ( ) between the individual values and symbols Examples short: 15.10.14, 15/ 10/ 14 long: Wednesday, October 15, 14 Software Development I // Exercises // 02 Testing and Language Description // 26

Metasyntax Notation: Examples for BNF, EBNF Representation EBNF representation: Date formats Date = ShortDate LongDate. ShortDate = Space Number Space ( "." Space Number Space "." "/" Space Number Space "/" ) Space YearNumber. LongDate = Day Space "," Space Month Space Number Space "," Space YearNumber. Day = "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday". Month = "January" "February" "March" "April" "May" "June" "July" "August" "September" "October "November" "December". Number = Digit [ Digit ]. YearNumber = Digit Digit. Digit = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9". Space = {" "}. Software Development I // Exercises // 02 Testing and Language Description // 27

Language Descriptions: Types of Notations Syntax diagram Terminal symbols circles or ovals Nonterminal symbols rectangles '/' 'Monday' LongDate Alternatives illustrated as branches, T-junctions Software Development I // Exercises // 02 Testing and Language Description // 28

Syntax Diagram: Date Formats Date ShortDate Space Number Digit Digit LongDate YearNumber Digit Digit ShortDate Space Number Space 9. Space Number Space. / Space Number Space / Digit 8... Space YearNumber 0 Software Development I // Exercises // 02 Testing and Language Description // 29

Syntax Diagram: Date Formats (continued) LongDate Space Day Space, Space Month Space Number Space, Space YearNumber Monday January Day Tuesday... Month February... Sunday December Software Development I // Exercises // 02 Testing and Language Description // 30

Exercises Software Development I 02 Algorithm Testing & Language Description Manual inspection, test plan, grammar, metasyntax notations (BNF, EBNF) October 15th, 2014 Software Development I Winter term 2014/2015 Priv.-Doz. Dipl.-Ing. Dr. Andreas Riener Institute for Pervasive Computing Johannes Kepler University Linz riener@pervasive.jku.at