Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Intermediate Code Generation
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.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
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
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Lecture 07 Expressions and Assignment Statements.
1 Control Flow Aaron Bloomfield CS 415 Fall 2005.
Language Evaluation Criteria
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.
C H A P T E R S E V E N Expressions and Assignment Statements.
CS 330 Programming Languages 09 / 04 / 2008 Instructor: Michael Eckmann.
Chapter 1 - Introduction
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
1 Control Flow: Expressions, Sequencing & Selection (Section ) A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
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
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
Programming Languages
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.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
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.
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.
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
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
Chap. 6 :: Control Flow Michael L. Scott.
Chapter 6:: Control Flow
Semantic Analysis Chapter 6.
Languages and Compilers (SProg og Oversættere)
Principles of Programming Languages
Chapter 6:: Control Flow
Controlling Program Flow
Presentation transcript:

Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott

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

Copyright © 2009 Elsevier 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!

Copyright © 2009 Elsevier Expression Evaluation Figure 6.1 Operator precedence levels in Fortran, Pascal, C, and Ada. The operator s at the top of the figure group most tightly.

Copyright © 2009 Elsevier Expression Evaluation 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

Copyright © 2009 Elsevier Expression Evaluation 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())...

Copyright © 2009 Elsevier Expression Evaluation 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 kinda halfway in-between –Java deliberately in-between built-in types are values user-defined types are objects - references

Copyright © 2009 Elsevier Expression Evaluation Expression-oriented vs. statement-oriented languages –expression-oriented: functional languages (Lisp, Scheme, ML) Algol-68 –statement-oriented: most imperative languages –C kinda halfway in-between (distinguishes) allows expression to appear instead of statement

Copyright © 2009 Elsevier Expression Evaluation Orthogonality –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

Copyright © 2009 Elsevier Expression Evaluation 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

Copyright © 2009 Elsevier Expression Evaluation 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

Copyright © 2009 Elsevier Expression Evaluation 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 languages

Copyright © 2009 Elsevier Expression Evaluation 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()

Copyright © 2009 Elsevier Expression Evaluation 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

Copyright © 2009 Elsevier Sequencing –specifies a linear ordering on statements one statement follows another –very imperative, Von-Neuman Sequencing

Copyright © 2009 Elsevier Selection –sequential if statements if... then... else if... then... elsif... else (cond (C1) (E1) (C2) (E2)... (Cn) (En) (T) (Et) ) Selection

Copyright © 2009 Elsevier 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 Selection

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

Copyright © 2009 Elsevier Code generated w/o short-circuiting (Pascal) r1 := A-- load 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: Selection

Copyright © 2009 Elsevier Code generated w/ short-circuiting (C) r1 := A r2 := B if r1 r2 goto L1 L4: r1 := E r2 := F if r1 = r2 goto L2 L1: then_clause goto L3 L2: else_clause L3: Selection

Copyright © 2009 Elsevier 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 Iteration

Copyright © 2009 Elsevier 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

Copyright © 2009 Elsevier Recursion 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); }