Chap. 6 :: Control Flow Michael L. Scott.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
CHAPTER 8 SEQUENCE CONTROL Hardware- Von Neumann architecture (sequence from incrementing program counter); loop and conditional from test and jump (branch)
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Chapter 8 . Sequence Control
Statement-Level Control Structures Sections 1-4
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Imperative Programming
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
C H A P T E R S E V E N Expressions and Assignment Statements.
Chapter 1 - Introduction
Cosc 2150: Computer Organization
1 Introduction Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 12.
sequence of execution of high-level statements
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Programming Languages and Paradigms Imperative Programming.
Control Structures sequence of execution of high-level statements.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Expressions and Assignment Statements
1 Iteration and Recursion (Section 6.5 – 6.6) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
1-1 1 Introduction  Programming linguistics: concepts and paradigms syntax, semantics, and pragmatics language processors.  Historical development of.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Expressions and Assignment Statements
Lecture 18 Control Flow.
Why study programming languages?
Expressions and Assignment Statements
Expressions and Assignment
CS 3304 Comparative Languages
CS2403 Programming Languages Expressions and Assignment Statements
CS 363 – Chapter 1 What is a programming language? Kinds of languages
Lecture 14: Iteration and Recursion (Section 6.5 – 6.6)
Chapter 6:: Control Flow
Chap. 6 :: Control Flow Michael L. Scott.
Control Flow.
Control Flow Chapter 6.
Control statements Simple statements Basic structured statements
FP Foundations, Scheme In Text: Chapter 14.
Programming Languages 2nd edition Tucker and Noonan
Final Review In Text: Chapters 1-3, 5-12,
Chapter 6:: Control Flow
Semantic Analysis Chapter 6.
Languages and Compilers (SProg og Oversættere)
Final Review In Text: Chapters 1-3, 5-16.
Principles of Programming Languages
Overview of Programming Paradigms
Chapter 7 Expressions and Assignment Statements.
Final Review In Text: Chapters 1-3, 5-16.
Chapter 6:: Control Flow
Expressions and Assignment Statements
Controlling Program Flow
CSE 3302 Programming Languages
Presentation transcript:

Chap. 6 :: Control Flow Michael L. Scott

Control Flow Basic paradigms for control flow: Sequencing Selection Iteration Procedural Abstraction Recursion Concurrency Exception Handling and Speculation Nondeterminacy

Expression Evaluation Infix, prefix operators Precedence, associativity (see Figure 6.1) C has 15 levels - too many to remember Pascal has 3 levels - too few for good semantics Fortran has 8 Ada has 6 Ada puts and & or at same level Lesson: when unsure, use parentheses!

Ordering of operand evaluation (generally none) Application of arithmetic identities distinguish between commutativity, and (assumed to be safe) associativity (known to be dangerous) (a + b) + c works if a~=maxint and b~=minint and c<0 a + (b + c) does not inviolability of parentheses

Short-circuiting Consider (a < b) && (b < c): If a >= b there is no point evaluating whether b < c because (a < b) && (b < c) is automatically false Other similar situations if (b != 0 && a/b == c) ... if (*p && p->foo) ... if (f || messy()) ...

Variables as values vs. variables as references value-oriented languages C, Pascal, Ada reference-oriented languages most functional languages (Lisp, Scheme, ML) Clu, Smalltalk Algol-68 kind a halfway in-between Java deliberately in-between built-in types are values user-defined types are objects - references

Expression-oriented vs. statement-oriented languages functional languages (Lisp, Scheme, ML) Algol-68 statement-oriented: most imperative languages C kind a halfway in-between (distinguishes) allows expression to appear instead of statement

Orthogonality Initialization Aggregates Features that can be used in any combination Meaning is consistent if (if b != 0 then a/b == c else false) then ... if (if f then true else messy()) then ... Initialization Pascal has no initialization facility (assign) Aggregates Compile-time constant values of user-defined composite types

Assignment statement (or expression) executed for its side effect assignment operators (+=, -=, etc) handy avoid redundant work (or need for optimization) perform side effects exactly once C --, ++ postfix form

Side Effects often discussed in the context of functions a side effect is some permanent state change caused by execution of function some noticable effect of call other than return value in a more general sense, assignment statements provide the ultimate example of side effects they change the value of a variable

SIDE EFFECTS ARE FUNDAMENTAL TO THE WHOLE VON NEUMANN MODEL OF COMPUTING In (pure) functional, logic, and dataflow languages, there are no such changes These languages are called SINGLE-ASSIGNMENT language

Several languages outlaw side effects for functions easier to prove things about programs closer to Mathematical intuition easier to optimize (often) easier to understand But side effects can be nice consider rand()

Side effects are a particular problem if they affect state used in other parts of the expression in which a function call appears It's nice not to specify an order, because it makes it easier to optimize Fortran says it's OK to have side effects they aren't allowed to change other parts of the expression containing the function call Unfortunately, compilers can't check this completely, and most don't at all

Sequencing Sequencing specifies a linear ordering on statements one statement follows another very imperative, Von-Neuman

Selection Selection sequential if statements if ... then ... else if ... then ... elsif ... else (cond (C1) (E1) (C2) (E2) ... (Cn) (En) (T) (Et) )

Selection Fortran computed gotos jump code for selection and logically-controlled loops no point in computing a Boolean value into a register, then testing it instead of passing register containing Boolean out of expression as a synthesized attribute, pass inherited attributes INTO expression indicating where to jump to if true, and where to jump to if false

Jump is especially useful in the presence of short-circuiting Example (section 6.4.1 of book): if ((A > B) and (C > D)) or (E <> F) then then_clause else else_clause

r1 := A -- load Code generated w/o short-circuiting (Pascal) r2 := B r1 := r1 > r2 r2 := C r3 := D r2 := r2 > r3 r1 := r1 & r2 r2 := E r3 := F r2 := r2 $<>$ r3 r1 := r1 $|$ r2 if r1 = 0 goto L2 L1: then_clause -- label not actually used goto L3 L2: else_clause L3:

Code generated w/ short-circuiting (C) r1 := A r2 := B if r1 <= r2 goto L4 r1 := C r2 := D if r1 > r2 goto L1 L4: r1 := E r2 := F if r1 = r2 goto L2 L1: then_clause goto L3 L2: else_clause L3:

Iteration Enumeration-controlled Pascal or Fortran-style for loops scope of control variable changes to bounds within loop changes to loop variable within loop value after the loop

Recursion Recursion equally powerful to iteration mechanical transformations back and forth often more intuitive (sometimes less) naïve implementation less efficient no special syntax required fundamental to functional languages like Scheme

Tail recursion No computation follows recursive call int gcd (int a, int b) { /* assume a, b > 0 */ if (a == b) return a; else if (a > b) return gcd (a - b,b); else return gcd (a, b – a); }