CS558 Programming Languages

Similar documents
Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

G Programming Languages - Fall 2012

Intermediate Code Generation

CS558 Programming Languages

Lecture 15: Iteration and Recursion

Chapter 6 Control Flow. June 9, 2015

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

CS558 Programming Languages

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Chapter 7 Control I Expressions and Statements

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

LECTURE 18. Control Flow

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

UNIT 3

Motivation was to facilitate development of systems software, especially OS development.

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Lecture Compiler Middle-End

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Flow Control. CSC215 Lecture

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Statement level control structures

Ch. 7: Control Structures

References and Mutable Data Structures

Motivation was to facilitate development of systems software, especially OS development.

CS240: Programming in C

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

CSE 431S Type Checking. Washington University Spring 2013

Handout 9: Imperative Programs and State

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

1007 Imperative Programming Part II

These are notes for the third lecture; if statements and loops.

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Lecture 3 Notes Arrays

Compilers CS S-07 Building Abstract Assembly

CS558 Programming Languages

Lecture 9: Control Flow

Types and Static Type Checking (Introducing Micro-Haskell)

The PCAT Programming Language Reference Manual

Introduction to Object-Oriented Programming

Lecture Notes on Intermediate Representation

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Principles of Programming Languages. Lecture Outline

Com S 541. Programming Languages I

Lecture Notes on Arrays

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

LECTURE 17. Expressions and Assignment

Three-address code (TAC) TDT4205 Lecture 16

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Project Compiler. CS031 TA Help Session November 28, 2011

CS 32. Lecture 3: Reorganizing

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Lecture 6 Sorting and Searching

Control Structures in Java if-else and switch

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

2/20/2018. Chapter 6:: Control Flow. control flow or ordering. basic categories for control flow (cont.): basic categories for control flow:

CS 314 Principles of Programming Languages. Lecture 9

Multi-dimensional Arrays

Programming in C and C++

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Digital Forensics Lecture 4 - Reverse Engineering

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

Control Flow. CSE 307 Principles of Programming Languages Stony Brook University

Programming Languages 2nd edition Tucker and Noonan"

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

G Programming Languages - Fall 2012

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

CS201 - Introduction to Programming Glossary By

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

CSE 230 Computer Science II (Data Structure) Introduction

NOTE: Answer ANY FOUR of the following 6 sections:

printf( Please enter another number: ); scanf( %d, &num2);

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

Types, Expressions, and States

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

LECTURE 1. Overview and History

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Concepts of Programming Languages

Transcription:

CS558 Programming Languages Fall 2017 Lecture 2a Andrew Tolmach Portland State University 1994-2017

Review: Expressions Inspired by familiar mathematical notation Usually have recursive (tree-like) structure Can be used to define values in many domains numbers, booleans, strings, lists, sets, etc. Declarative syntax: tells what to compute rather than how Abstracts away from evaluation order* and use of temporaries compare with, e.g., stack machine * to some extent: depends on language

Imperative Languages Most commonly-used languages are imperative Consist of sequence of commands that alter the state of the world State = values of program variables and external environment (e.g. files, screen, etc.) http://smarteregg.com/dont-tell-me-what-to-do-now-show-me-what-i-need-to-be-doing/

Running Imperative Programs High-level imperative languages mimic style of the underlying Von Neumann machine architecture machine programs are sequences of instructions that modify registers and memory locations Compiling imperative languages to machine code is relatively straightforward variables are mapped to machine locations commands (operations) are mapped to (multiple) machine instructions

Reactive Programs Imperative languages are also natural for writing reactive programs that interact with the real world Examples: Reading mouse clicks and modifying the contents of a display Communicating data on a network link Controlling a set of sensors and relays in an external device Often structured as event-response loops

Assignment Most primitive command: store a value into a location a := 42 In simplest form, location is associated with a variable but might be an array or record element, etc. a[x+2] := 42 In most languages, a variable name means different things on the lefthand side (LHS) and right-hand side (RHS) of an assignment. a := a + 5 On LHS, name denotes the location of the variable, into which the value of the RHS expression is to be stored. Here we say name is an l-value. On RHS, name denotes the current value contained in the location, i.e. it indicates an implicit dereference operation. Here we say the name is an r-value.

Assignment Expressions b := (a := 42) f(c := 10) In some languages, assignment is an expression But every expression must define a value! Common choices: value of RHS special no information value e.g., in Scala: ():Unit C/C++/Java popularized use of plain = for assignment and == for relational equality: a truly bad idea, because both are expressions and are easy to confuse

Order of Operations We ve noted that order of operations for expressions is usually under-specified Parse tree doesn t completely fix order But this causes problems if expressions can be assignments: a = 0; b = (a = a + 1) - (a = a + 2); ANSI C99 What is the result in b? It can be anything! This program has undefined behavior and the compiler can generate anything it wants (for the entire program!) or the compiler could give a warning or error message, but many compilers do not.

Hidden side-effects Even without explicit assignment expressions, expression evaluation order can affect behavior: ANSI C99 int a = 0; int h (int x, int y) { return x; } int f (int z) { a = z; return 0; } h(a,f(2)); // = 0 or 2?? Answer depends on evaluation order for function parameters, which is compiler-dependent (though unspecified rather than undefined ) This flexibility may let compiler generate more efficient code But most modern languages are moving towards precise specification of order (e.g. left-to-right)

Imperative code is infectious ANSI C99 int a = 0; int h (int x, int y) { return x; } int f (int z) { a = z; return 0; } h(a,f(2)); // = 0 or 2?? Root of problem is that imperative code can be hidden within function definitions ( side-effects ) If any part of the code might be imperative, we must worry about order of evaluation in all parts of the code May explore this more later on in the course

Structured Control Flow All modern higher-level imperative languages are designed to support structured programming Syntactic structure of program text corresponds to dynamic flow of control during execution Originally proposed as improvement over unreadable spaghetti code that is easy to produce using labels and jumps Edsger W. Dijkstra, go to statement considered harmful, CACM, 11(3), Mar. 1968, 147-148. blogbv2.altervista.org

Small set of statement kinds Use small collection of (recursively defined) compound statements to describe control flow Sequential composition: do a sequence of commands (Java) { x = 2; y = x + 4;} (Pascal) begin x := 2; y := x + 4; end Selection: do one of several alternative commands (Java) if (x < 0) y = x + 1; else z = y + 2; Iteration: do a command repeatedly (Java) while (x > 10) output(x--); (Pascal) for x := 1 to 12 do output(x*2);

Sequential composition Simplest way to combine commands: just write one after another Order obviously matters! (What about parallel composition?) Can also have sequential composition of expressions e1;e2 means: evaluate e1; throw away the result; then evaluate e2 obviously only interesting if e1 has side-effects

Selection: if Basic selection statement based on booleans if e then s 1 else s 2 compiles to l 1 : s 2 l 2 : evaluate e into t cmp t,true brneq l 1 s 1 br l 2 pseudo assembly code

Selection: case Generalizes boolean conditionals to types with larger domains case e of c 1 : s 1 c 2 : s 2... c n : s n default : s d Note that the ci are constants Choice of most efficient compilation method depends on density of the ci within the domain of possible values for e and on whether e s type is ordered

Sparse case compilation case e of c 1 : s 1 c 2 : s 2... c n : s n default : s d is equivalent to t := e; if t = c 1 s 1 then else if t = c 2 s 2 else... else if t = c n s n else s d then then This is just a linear search (O(n) time) If e s type is ordered, we can do better with a binary search (O(log n) time)

Dense case compilation If labels are dense in the range [c1,cn], it s better to use a jump table (O(1) time): case e of c 1 : s 1 c 2 : s 2... c n : s n default : s d compiles to table: l 1 l 2... l n evaluate e into t cmp t,c 1 brlt l d cmp t,c n brgt l d sub t,c 1,t add table,t,t br *t l 1 : s 1 br done l 2 : s 2 br done... l n : s n br done l d : s d done:

Iteration: while and repeat while e do s compiles to top: done: evaluate e into t cmp t,true brneq done s br top repeat s until e is equivalent to s; while not e do s

Loop exits It can be useful to break out of the middle of a loop loop s 1 ; s 2 end exitif e; compiles to top: s 1 evaluate e into t cmp t,true breq done s 2 br top done: C/C++/Java break is unconditional form of exit They also have a continue statement that jumps back to the top of the loop

Uses for goto? An efficient program using goto int i; for (i = 0; i < n; i++) if (a[i] == k) goto found; n++; a[i] = k; b[i] = 0; found: b[i]++; transfer control to point well past end of the loop In most languages, there is no equivalently efficient program without goto: must add a flag variable

int i; search: { for (i = 0; i < n; i++) } Multi-level break But we can do as well in Java, using a named, multi-level break statement n++; if (a[i] == k) break search; a[i] = k; b[i] = 0; b[i]++; transfer control to point just past end of named block This construct was invented by Don Knuth in the 1960 s but not adopted into a mainstream language for 30 years!

Counted loops Since iterating through a range of numbers is very common, many languages offer a dedicated statement,e.g. for i := e 1 to e 2 do s The detailed semantics vary, and can be tricky (e.g. can s change i?) Many modern languages support generalized iterators through sets (More on these later in the course) C/C++/Java offer a more general-purpose statement for (e 1 ; e 2 ; e 3 ) s; is equivalent to e 1 ; while (e 2 ) { s; e 3 }

The COME FROM statement 10 J = 1 11 COME FROM 20 12 PRINT J STOP 13 COME FROM 10 20 J = J + 2 A notorious joke! R.Lawrence Clark, A Linguistic contribution to GOTO-less programming, Datamation, 19(2), 1973, 62-63. But with a serious point: even with an ordinary GOTO, we must examine the whole label/branch structure of the program to understand its behavior