Chapter 3 of Programming Languages by Ravi Sethi

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
Program Proving Notes Ellen L. Walker.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
Weakest pre-conditions and towards machine consistency Saima Zareen.
CS 355 – Programming Languages
What is an Algorithm? (And how do we analyze one?)
Axiomatic Semantics Dr. M Al-Mulhem ICS
Chapter 8 . Sequence Control
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Describing Syntax and Semantics
Fundamentals of Python: From First Programs Through Data Structures
CSE 755, part3 Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP: ::=skip | | | | ; | | Only integer vars; no procedures/fns; vars declared.
Invitation to Computer Science, Java Version, Second Edition.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Principle of Programming Lanugages 2: Imperative languages Isao Sasano Department of Information Science and Engineering ( structured programming, control.
Chapter 8: Statement-Level Control Structures
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Languages and Compilers
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
ALGORITHMS.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Imperative Programming Statements and invariants.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 4 – C Program Control
Applied Discrete Mathematics Week 2: Functions and Sequences
DDC 2423 DATA STRUCTURE Main text:
Principle of Programming Lanugages 2: Imperative languages
Introduction To Repetition The for loop
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Lecture 7: Repeating a Known Number of Times
Python: Control Structures
Chapter 5: Control Structures II
Chapter 7 Arrays.
Java Programming: Guided Learning with Early Objects
Chapter 5 Structures.
Topics Introduction to File Input and Output
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Predicate Transformers
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Chapter 4 - Program Control
Topics Introduction to File Input and Output
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Chapter 3 of Programming Languages by Ravi Sethi

Chapter 3 Structured Programming 3.1 The Need for Structured programming Values of variables can change The basic units of imperative programming are actions, which can change the values of variables (assignments, procedure calls, read(x)

Static Programs, Dynamic Computations Programs specify computations A sequential computation consists of a sequence of actions, such as writeln (1, 1 * 1) writeln (2, 2 * 2) writeln (3, 3 * 3)

The text is static in a program, but the computation is dynamic which occurs every time a program runs. The gap between the static program and dynamic execution has motivated the need for structured programming. So the reader of a program can understand the actions that occur when a program runs

Design Principles for Imperative Languages Built with structure and efficiency in mind Structured Programming: The structure of the program text should help us understand what the program does. (easier to modify and tune for efficiency)

Efficiency A language must allow an underlying assignment-oriented machine to be used directly and efficiently. A Running Example: Skip duplicates - removal of adjacent duplicates Invariants: Program Design Invariants relate programs and computations - tells us about the property of its computations

* Programming language design must deal at some level with program design. The purpose is to make programming easier. -- An Invariant at some point in a program is an assertion that holds whenever that point is reached at run time, that is, whenever control reaches that point -- An Assertion is a true / false condition about the state of a computation. An example is the condition X > Y, which relates the value of X and Y

3.2 Syntax-Directed Control Flow --Structured statements are by definition single-entry and single-exit --Structured control flow: A program is structured if the flow of control through the program is evident from the syntactic structure of the program text Composition of Statements: Control flows sequentially through a sequence of statements, as in:

temp = x; x = y; y = temp Selection: Conditional Statements A conditional statement selects one of two alternative substatements for execution Looping constructs, divided into two forms: Definite (for) Indefinite (while, repeat until) Selection: case statements - case constants, appear in any order

Implementation of Case Statements Except for case statements, the statement constructs in imperative languages can be used without thinking about how they were implemented some implementations recommend only be used when the case constants are essentially adjacent

3.3 Design Consideration: Syntax Syntax affects usability Sequences: separators versus terminators, e.g., semicolons Fewer programming errors are believed to occur if semicolons terminate statements than if they separate statements Avoiding dangling else

3.4 Handling Special Cases in loops Break and continue statements in loops -- A break statement sends control out of the enclosing loop to the statement following the loop. -- A continue statement repeats the enclosing loop by sending control to the beginning of the loop Return statements Go to Statements

3.5 Programming with Invariants constructed program is defined as single-entry/single-exit. Precondition and postcondition -- attached just before and after a statement; both are assertions.

{ x >= 0 and y> 0} - precondition while x >= y do { y > 0 and x >= y} invariant x := x - y { x >= 0 and y > 0} postcondition Linear Search Linear search proceeds by examining all the elements until either x is found or no elements remain to be examined. Invariants can describe data structures -A Table Organized Around an invariant

An approach to implementing linear search while elements remain to be examined do begin if this element is x then return its position else not found, so return 0

Linear Search with a sentinel The post condition after the search x = A[i] and x is not in A[i + 1 … n] and 0<= i <= n The condition for staying within the while loop is A[i] != x.

The final developed program fragment: A[0] := x; i := n; while x != A[i] do i := i - 1; return i ;

Your chance at correct high-level pseudocode You are to determine whether the sorted array X[1…N] contains the element T. Use a binary search

A Possible Answer first = 1; last = N; while(first <= last) { middle = (first + last) /2 if( T == X[middle] ) return middle; else if (T < X[middle]) last = middle -1; else first = middle + 1; } print “not found”;

Proof rules for partial correctness Proof rules -- the theory behind invariants -- good training for dealing with subtle code. Partial correctness -- the program is correct if it terminates Assertions and formulas -- P (pre), Q(post) ,^(and), v(or) ,!(this is my not since PP cannot easily do the “hook”

Some examples of proof rule and axioms Rule for statement composition S1; S2 {P} S1 {Q}, S2 {R} (<- given formula) {P} S1; S2 {R} ( <- conclusion) Rule for conditionals {P^E} S1 {Q}, {P ^ !E} S2 {Q} {P} if E then S1 else S2 {Q} Rule for while statement {P ^ E} S {P} {P} while E do S {P ^ !E}

P implies P’, {P’} S {Q’} ,Q’ implies Q {P} S {Q} Rule for Assignments {Q [E / x]} x := E {Q} (assignment axiom - since it is an atomic statement, its proof rule has no formulas above the line) -- The relationship between the postassignment value and preassignment value of E is of x Rule for Simplification (predicates ex. I>1) P implies P’, {P’} S {Q’} ,Q’ implies Q {P} S {Q}

Statements in Pascal control flow in Pascal is purely syntax-directed; the flow of control through a construct can be described purely in terms of components of the construct.

3.7 Control Flow in C Comparison of C to Pascal C Pascal if ( E ) S if E then S if ( E ) s1 else s2 if E then s1 else s2 while ( E ) s while E do s Assignment Operations =, ==, !=

Assignments within Expressions c = getchar() can appear as a sub expression within a larger expression: while ( (c = getchar()) != EOF) For loops in C: Indefinite Iteration --An empty test expression is assumed to be true for ( ; ; ) can be read as “forever” (the expressions E1, E2, E3 are optional in a for loop)

Break and continue statements in loops example: for( ; ; c = getchar( ) ) { if ( c == ‘ ‘ || c == ‘\t’ ) continue; if ( c != ‘\n’ ) break; ++lineno; }