Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Intermediate Code Generation
Statement-Level Control Structures
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.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Compiler Construction Intermediate Code Generation.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
P ROGRAMMING L ANGUAGES : C ONTROL 1. S LIDES R EFERENCES Kenneth C. Louden, Control I: Expressions and Statements: Principles and Practice. 2.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Compiler Construction
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ISBN Chapter 8 Statement-Level Control Structures.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Chapter 8 . Sequence Control
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSE 452: Programming Languages Expressions and Control Flow.
ISBN Lecture 08 Statement-Level Control Structures.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
ISBN Chapter 7 Expressions and Assignment Statements.
Compiler Chapter# 5 Intermediate code generation.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Flow of Control Part 1: Selection
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
ISBN Chapter 8 Statement-Level Control Structures.
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.
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Arithmetic Expressions
Exam Delay Exam II will be held during the first fifty (50) minutes of class on 13 November 2003.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Introduction to Java Java Translation Program Structure
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
CS 36 – February 2 Control structures –Sequence √ –Loops √ –Selection If-statement Case-statement Finding loops.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Information and Computer Sciences University of Hawaii, Manoa
7.2 Arithmetic Expressions
Def: A control structure is a control statement and
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Programmazione I a.a. 2017/2018.
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Chapter 6 Intermediate-Code Generation
Control Structures In Text: Chapter 8.
Chap 7. Advanced Control Statements in Java
Chapter 8: Statement Level Control Structures
Controlling Program Flow
CSE 3302 Programming Languages
Presentation transcript:

Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution paths through code: what gets executed, when, and in what order.

Chapter 7Louden, Programming Languages2 Expressions In their purest form, expressions do not involve control issues: subexpressions can be evaluated in arbitrary order, and the order does not affect the result. Functional programming tries to achieve this goal for whole programs. If expressions could have arbitrary evaluation order, programs would become non-deterministic: any of a number of different outcomes might be possible.

Chapter 7Louden, Programming Languages3 Side Effects A side effect is any observable change to memory, input or output. A program without any side effect is useless. Side effects expose evaluation order: class Order { static int x = 1; public static int getX() { return x++; } public static void main(String[] args) { System.out.println( x+getX() ); } } This prints 2, but the corresponding C program will usually print 3! Why?

Chapter 7Louden, Programming Languages4 Strictness An evaluation order for expressions is strict (eager) if all subexpressions of an expression are evaluated, whether or not they are needed to determine the value of the result, non-strict (lazy) otherwise. Arithmetic is almost always strict. Every language has at least a few non-strict expressions (?:, &&, || in Java). Some languages use a form of non-strictness called normal-order evaluation: no expression is ever evaluated until it is needed (Haskell). Also called delayed evaluation. A form of strict evaluation called applicative-order is more common: "bottom-up" or "inside-out". Still leaves open whether left-to-right or not.

Chapter 7Louden, Programming Languages5 Short Circuit Evaluation Suppose Java did not use short-circuit evaluation Problem: table look-up index = 1; while (index <= length) && (LIST[index] != value) index++;

Chapter 7Louden, Programming Languages6 Short Circuit Evaluation C, C++, and Java: use short-circuit evaluation for the usual Boolean operators ( && and || ), but also provide bitwise Boolean operators that are not short circuit ( & and | ) Ada: programmer can specify either (short-circuit is specified with and then and or else) FORTRAN 77: short circuit, but any side-affected place must be set to undefined Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)

Chapter 7Louden, Programming Languages7 Function calls Obey evaluation rules like other expressions. Applicative order: evaluate all arguments (left to right?), then call the procedure. Normal order: pass in unevaluated representations of the arguments. Only evaluate when needed. With side effects, order makes a difference.

Chapter 7Louden, Programming Languages8 Case Statements Rules cases can be constants, constant expressions, constant ranges no overlapping cases error if unspecified case occurs (or may decide to do nothing if unspecfied case) Implemented via jump table: vector stored sequentially in memory - each of whose components is an unconditional jump Need one location in jump table for every value between range of possible values

Chapter 7Louden, Programming Languages9 Switch Statements Implementation Options –Series of conditionals Good if few cases Slow if many –Jump Table Lookup branch target Avoids conditionals Possible when cases are small integer constants –GCC Picks one based on case structure –Bug in example code No default given typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op) { switch (op) { case ADD : return '+'; case MULT: return '*'; case MINUS: return '-'; case DIV: return '/'; case MOD: return '%'; case BAD: return '?'; }

Chapter 7Louden, Programming Languages10 Jump Table Structure Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n–1 Targn-1: Targ0 Targ1 Targ2 Targn-1 jtab: target = JTab[op]; goto *target; switch(op) { case 0: Block 0 case 1: Block 1 case n-1: Block n–1 } Switch Form Approx. Translation Jump Table Jump Targets

Chapter 7Louden, Programming Languages11 A jump table is an array of code addresses: –Tbl[ i ] is the address of the code to execute if the expression evaluates to i. –if the set of case labels have “ holes ”, the correspond jump table entries point to the default case. Bounds checks: –Before indexing into a jump table, we must check that the expression value is within the proper bounds (if not, jump to the default case). –The check lower_bound  exp_value  upper bound can be implemented using a single unsigned comparison.

Chapter 7Louden, Programming Languages12 Jump Table Space Costs jump tables best for large no. of case labels (  8) may take a large amount of space if the labels are not well- clustered. A jump table with max. and min. case labels c max and c min needs  c max – c min entries. This can be wasteful if the entries aren’t “dense enough”, e.g.: switch (x) { case 1: … case 1000: … case : … } Define the density of a set of case labels as density = number of case labels/(c max – c min ) Compilers will not generate a jump table if density below some threshold (typically, 0.5).

Chapter 7Louden, Programming Languages13 Use of Switch Statements if no. of case labels is small (  ~ 8), use linear or binary search. –use no. of case labels to decide between the two. if density  threshold (~ 0.5) : generate a jump table; else : divide the set of case labels into sub-ranges s.t. each sub-range has density  threshold; generate code to use binary search to choose amongst the sub-ranges; handle each sub-range recursively.

Chapter 7Louden, Programming Languages14 Guarded Commands Selection: if -> [] ->... [] -> fi Semantics: when this construct is reached, –Evaluate all boolean expressions –If more than one are true, choose one nondeterministically –If none are true, it is a runtime error

Chapter 7Louden, Programming Languages15 Guarded Commands Idea: if the order of evaluation is not important, the program should not specify one In Haskell, first one that matches is used.

Chapter 7Louden, Programming Languages16 Guarded Commands Loops do -> [] ->... [] -> od

Chapter 7Louden, Programming Languages17 Guarded Commands Semantics: For each iteration: –Evaluate all boolean expressions –If more than one are true, choose one nondeterministically; then start loop again –If none are true, exit loop

Chapter 7Louden, Programming Languages18 Summary Every language has three major program components: expressions, statements, and declarations. Expressions are executed for their values (but may have side effects), and may or may not be sequenced. Statements are executed solely for their side effects, and they must be sequenced. Declarations define names; they can also give values to those names. They may or may not be viewed by a language as expressions or statements.