Control Flow February 9, Lecture 7

Similar documents
LECTURE 17. Expressions and Assignment

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

Chapter 6 Control Flow. June 9, 2015

Lecture 13: Expression Evaluation

! Non-determinacy (unspecified order) Maria Hybinette, UGA 2. 1 Landin adding sugar to a language to make it easier to read (for humans)

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

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

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Lecture 10: Expression Evaluation

G Programming Languages - Fall 2012

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Lab 7 1 Due Thu., 6 Apr. 2017

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

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Chapter 7 Control I Expressions and Statements

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

MIDTERM EXAMINATION - CS130 - Spring 2003

Lecture 13: Complex Types and Garbage Collection

Lambda Calculus. Gunnar Gotshalks LC-1

MIDTERM EXAMINATION - CS130 - Spring 2005

LECTURE 18. Control Flow

Lambda Calculus LC-1

G Programming Languages - Fall 2012

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Logic Programming II & Revision

Concepts of Programming Languages

Expression Evaluation and Control Flow. Outline

Types. What is a type?

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

This book is licensed under a Creative Commons Attribution 3.0 License

Concepts Introduced in Chapter 7

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

INSTITUTE OF AERONAUTICAL ENGINEERING

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

Functional Programming. Big Picture. Design of Programming Languages

CS558 Programming Languages

CS558 Programming Languages

Com S 541. Programming Languages I

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

UNIT 3

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

LECTURE 14. Names, Scopes, and Bindings: Scopes

CS 320: Concepts of Programming Languages

Working of the Compilers

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

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

CSC 533: Organization of Programming Languages. Spring 2005

Expressions and Assignment

Programming Languages 2nd edition Tucker and Noonan"

Informatica 3 Syntax and Semantics

Principles of Programming Languages. Lecture Outline

Types and Type Inference

CS W3134: Data Structures in Java

CS 314 Principles of Programming Languages

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

CS558 Programming Languages

HANDLING NONLOCAL REFERENCES

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

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

Chapter 9 Subprograms

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Programming Languages, Summary CSC419; Odelia Schwartz

Programmiersprachen (Programming Languages)

Lecture 15: Iteration and Recursion

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

Lexical Considerations

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

Principles of Programming Languages

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Types and Type Inference

G Programming Languages - Fall 2012

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

1 Lexical Considerations

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

Programming Languages

Programming Languages

Chapter 8 :: Composite Types

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

Functional Programming Lecture 1: Introduction

Code No: R Set No. 1

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

The PCAT Programming Language Reference Manual

Lexical Considerations

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Formal Languages and Automata Theory, SS Project (due Week 14)

References and pointers

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Transcription:

Chapter 6 Control Flow February 9, Lecture 7

Expressions A simple object Literal constant Named variable Constant Or a function applied to arguments For built-in functions we use the term operator (like +,*) ALGOL family

Expressions a+b is syntactic sugar for actual internal functions:: Ada: + (a,b) C++: a.operator+(b) Where does the name of the function appear? Prefix Infix Postfix For example in LISP we have prefix, with function name inside parentheses

Expressions Where does the name of the function appear? Prefix Infix Postfix For example in LISP we have prefix, with function name inside parentheses

Expressions Where does the name of the function appear? Prefix Infix Postfix Some languages (like R) allow the programmer to define infix functions Some languages use only infix notation Smalltalk

Expressions Conditional expressions (mixfix) Algol C

Expressions Precedence and associativity (only for infix notation) C is very good and intuitive with precedence Pascal not

Expressions Precedence and associativity (only for infix notation) Associativity is more standard Exceptions exist, for example 2**3**4 is not allowed in Fortran

Expressions Expressions provide values to the surrounding context (defun gcd2 (a b) (if (zerop b) a (gcd2 b (mod a b)))) LISP code Functional languages contain only expressions Complex computations are done via recursion

Statements Imperative languages compute by means of side-effects A future computation is influenced in a way other than returning a value to the surrounding context For example changing the value of a variable affects all future parts of the program where the variable is used. In functional languages there are no side-effects The value of an expression is independent of the time it is evaluated A future evaluation will be the same, because there are no side-effects Some imperative languages distinguish between expressions and statements. Statements exist only for their side-effects.

References and values Assignments appear to be simple. However there are differences in their semantics What they mean in each language This has an impact on more complex programs with pointers Reference value Location value

References and values Not all expressions can be l-values 2+3 = a a = 2+3 (when a is a constant) L-values can be very complicated expressions What does is do? Possible in C++ only

Reference vs value model Two semantics Difference in thinking

Boxing The need for boxing, because of the value model Older Java Newer Java

Orthogonality Features are consistent and can be used in any combination Expression-oriented languages Things that look like statements work like expressions Algol 68 In contrast Pascal makes clear distinction

Orthogonality In C the distinction is made But it allows expressions to appear in expression statements Effectively allowing expressions to appear where statements are expected Source of bugs in C This will generate error in other languages

Combination Assignment Operators Common things: a = a+1 b.c[3].d = b.c[3].d*e; Cumbersome and requiring extra work (or compiler work) Why is this dangerous?

Combination Assignment Operators Solution is assignment operators Traversing arrays How does this work?

Multiway assignment In several languages like Clu,ML,Perl, Python,Ruby It s useful in the following context

Initialization Static variables can be initialized by the compiler Heap stored variables must be done at run-time Default may be 0 to be consistent with the operating system Initialize every variable when declared for safety This doesn t help with bugs It rather makes them repeatable Some languages allow types that have constructors So when a variable of that type is defined it is essentially a call to the constructor

Initialization Some languages (Java, C#) require definite assignment They complain if they see no initialization at compile Others do dynamic checks (NaN value), mostly at higher cost Focus on safety rather than speed

Ordering Associativity doesn t always help predict the value This is because of side-effects The order has an effect on efficiency (registers, scheduling) a*b +f(c) Executing in parallel

Ordering and rearranging Most languages leave the order undefined Let the compiler decide, for the shake of efficiency Some languages fix it, say from left to right Rearranging in mathematical expressions Using commutativity This can be dangerous and unwanted. Why?

Short-circuit evaluation The evaluation of some expressions can finish early (a<b) or (b>c) It may be useful to stop early in practice So saving time is one way it can be used

Short-circuit evaluation It changes the semantics Code for searching an element in a list Short-circuiting in C Non Short-circuiting in Pascal

Short-circuit evaluation Solution in Pascal

Short-circuit evaluation Short-circuiting to prevent from going out of array bounds Division by zero

Short-circuit evaluation Some times short-circuiting is desirable Exploiting side-effects Some languages provide both options (cand vs and)