Lecture 10: Expression Evaluation

Similar documents
Lecture 13: Expression Evaluation

LECTURE 17. Expressions and Assignment

Control Flow February 9, Lecture 7

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

Lecture 12: Data Types (and Some Leftover ML)

G Programming Languages - Fall 2012

Lecture 13: Complex Types and Garbage Collection

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

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

MIDTERM EXAMINATION - CS130 - Spring 2003

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

Software II: Principles of Programming Languages. Why Expressions?

Lecture 15: Object-Oriented Programming

Lecture 7: Binding Time and Storage

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

Chapter 7 Control I Expressions and Statements

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

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

Information Science 1

Lectures Basics in Procedural Programming: Machinery

Expressions and Assignment Statements

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Chapter 6 Control Flow. June 9, 2015

Expressions and Assignment

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

Chapter 7. Expressions and Assignment Statements ISBN

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Types II. Hwansoo Han

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Chapter 7. Expressions and Assignment Statements

CS 320: Concepts of Programming Languages

UNIT 3

Expressions & Assignment Statements

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements

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

Programmiersprachen (Programming Languages)

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

Chapter 7 Expressions and Assignment statements

Lab 7 1 Due Thu., 6 Apr. 2017

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Lecture 1: Introduction

Lecture 3 Operators MIT AITI

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

Functional Programming

Reasoning About Imperative Programs. COS 441 Slides 10

Introduction. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CS 242. Fundamentals. Reading: See last slide

Preview of Expressions

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

Types and Type Inference

CS 314 Principles of Programming Languages

HANDLING NONLOCAL REFERENCES

Types and Type Inference

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

CSE 307: Principles of Programming Languages

Lecture 15: Iteration and Recursion

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

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

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

Functional Programming Languages (FPL)

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Concepts of Programming Languages

Lecture 5/6: Scripting and Perl

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CS 314 Principles of Programming Languages

This book is licensed under a Creative Commons Attribution 3.0 License

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

So what does studying PL buy me?

Special Topics: Programming Languages

Java is an objet-oriented programming language providing features that support

MODEL ANSWERS COMP36512, May 2016

Data Types The ML Type System

Programming Paradigms

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

LECTURE 18. Control Flow

Chapter 8 Statement-Level Control Structures

Thomas Jefferson Invitational Open in Informatics 2012

Lecture Set 4: More About Methods and More About Operators

Functional Programming and Haskell

Expression Evaluation and Control Flow. Outline

Lecture Chapter 6 Recursion as a Problem Solving Technique

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Bawar Abid Abdalla. Assistant Lecturer Software Engineering Department Koya University

Imperative Paradigm. Syntax. Role of Programming Languages. Expressions. Expressions. Role of Programming Languages. Symbols.

Programming Languages 2nd edition Tucker and Noonan"

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

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

Lambda Calculus. Gunnar Gotshalks LC-1

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

Stack and Its Implementation

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

SYNERGY INSTITUTE OF ENGINEERING & TECHNOLOGY,DHENKANAL LECTURE NOTES ON DIGITAL ELECTRONICS CIRCUIT(SUBJECT CODE:PCEC4202)

Transcription:

Lecture 10: Expression Evaluation COMP 54 Programming Language Concepts Stephen Olivier February 17, 009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Goal of Talk The goal of this talk is to talk about expressions and the flow of programs

Control Flow Control flow is the order in which a program executes. For imperative languages (e.g., Java), this is fundamental. For other programing paradigms (e.g., functional), the compilers/interpreters take care of ordering. 3

Expression Evaluation An expressions consist of a simple object (e.g., a variable), an operator, or a function applied to a collection of objects and/or operators. Expression evaluation is a crucial component of functional languages. 4

Functional languages are very math-like Expression Evaluation and in math a primary concept is evaluating expressions. An expressions consist of a simple object (e.g., a variable), an operator, or a function applied to a collection of objects and/or operators. Expression evaluation is a crucial component of functional languages. 5

Operators Operators are used in Prefix notation: operators come first (* (+ 1 3) ) Infix notation: operators in middle (1+3)* Postfix notation: operators last a++ 6

Operators-Precedence Precedence rules specify the order in which operators of different precedence levels are evaluated. e.g. Multiplication before addition. Precedence in boolean expressions very important The phrase if A<B and C<D can be read as: if (A<B) and (C<D) if (A< (B and C)) <D 7

Operators--Associativity Associativity rules specify the order in which operators of the same precedence level are evaluated. Usually they are evaluated left-to-right In Fortran, ** associates from right-to-left x ** y = x^y Thus **3**4 is read as ^(3^4) rather than (^3)^4. Also assignment in C a = b = c 8

Assignment The basic operation language is assignment. An assignment places a value into a specific memory location. a = ; X a 036 Before a 036 After 9

As a result, assignments have longevity Assignment and can exist beyond their original The basic operation language is assignment. context. An assignment places a value into a specific memory location. a = ; X a 036 Before a 036 After 10

Context To see the difference between context consider the two following statements. int sum(int n){ int val=0; for(int i=0,i<=n;i++){ val+=i; } return val; } int sum(int n){ if (n<=0) then return 0 else return n+sum(n-1) } Imperative Functional 11

Context In the imperative code the value of val changes within the context of sum To see the difference between context consider the two following statements. int sum(int n){ int val=0; for(int i=0,i<=n;i++){ val+=i; } return val; } int sum(int n){ if (n<=0) then return 0 else return n+sum(n-1) } Imperative Functional 1

Context In the functional code the value of n changes but only between contexts of sum To see the difference between context consider the two following statements. int sum(int n){ int val=0; for(int i=0,i<=n;i++){ val+=i; } return val; } int sum(int n){ if (n<=0) then return 0 else return n+sum(n-1) } Imperative Functional 13

Variables Two ways to model variables: Value model Reference model 14

Value Model Under the value model variables on the left-hand side (called l-values) of equations denote references, and variables on the right-hand side (called r-values) denote values. b = ; a = b; mem(104)= ; mem(036)= ; b 104 b 104 X a 036 a 036 15

Value Model Pascal and C use this model Under the value model variables on the left-hand side (called l-values) of equations denote references, and variables on the right-hand side (called r-values) denote values. b = ; a = b; mem(104)= ; mem(036)= ; b 104 b 104 X a 036 a 036 16

Reference Model Under the reference model variables on both the leftand right-hand side are references. b = ; a = b; 48 48 b 104 X a 036 mem(104)= loc(); mem(036)= mem(104); 48 b 104 48 a 036 17

Reference Model Lisp, Clu use this model. Under the reference model variables on both the leftand right-hand side are references. b = ; a = b; mem(104)= loc(); mem(036)= mem(104); 48 48 b 104 48 b 104 X a 036 48 a 036 18

Expressions: Initialization Variable initialization can be implicit or explicit. Implicit: variables are initialized as they are used (e.g., Perl). $a += 3; Explicit: variables are initialized by the programmer (e.g., C). int a = 0; a += 3; Java, C# require definite assignment Variables must be assigned a value before they are used in expressions 19

Expressions: Orthogonality Orthogonality means that features can be used in any combination and the meaning is consistent regardless of the surrounding features Good idea in principle, but requires careful thought e.g. assignment as an expression unfortunate when combined with poor syntactic choices, as in C: if(a=b){ } if(a==b){ } 0

Expressions: Complication Execution ordering within expressions is complicated by side effects (and code improvements) e.g., in C b=1; int inc(int a) { b+=1; return a+1; } c = (3*b) * inc(b); If inc(b) is evaluated before (3*b), the final value of c is 1. If the (3*b) is evaluated first, then the value is c is 6. 1

Expressions: Short-Circuit Expressions may be executed using short-circuit evaluation p = my_list; while (p && p->key!=val) p=p->next

Expressions: Short-Circuit Expressions may be executed using short-circuit evaluation p = my_list; while (p && p->key!=val) p=p->next if p = nul, then p->key is never checked. Thus, it is short-circuited 3

Expressions: Short-Circuit Expressions may be executed using short-circuit evaluation p = my_list; while (p && p->key!=val) p=p->next p := my_list; while (p<>nil) and (p^.key <> val) do p:=p^.next Since Pascal does not have short circuiting, this will check both. Thus, if p=nil, then p^.key will return an error. 4