LECTURE 17. Expressions and Assignment

Similar documents
COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Control Flow February 9, Lecture 7

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

Expressions and Assignment

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

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

Lecture 13: Expression Evaluation

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

Principles of Programming Languages

G Programming Languages - Fall 2012

Chapter 6 Control Flow. June 9, 2015

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

Expression Evaluation and Control Flow. Outline

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

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

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

Chapter 7. Expressions and Assignment Statements ISBN

MIDTERM EXAMINATION - CS130 - Spring 2003

Expressions & Assignment Statements

Software II: Principles of Programming Languages. Why Expressions?

This book is licensed under a Creative Commons Attribution 3.0 License

Programming in C++ 5. Integral data types

1 Lexical Considerations

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

A Simple Syntax-Directed Translator

LECTURE 18. Control Flow

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

Concepts Introduced in Chapter 6

Operators. Java operators are classified into three categories:

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

Expressions and Assignment Statements

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

Lecture 10: Expression Evaluation

Chapter 7. Expressions and Assignment Statements

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

The PCAT Programming Language Reference Manual

Chapter 7. Expressions and Assignment Statements ISBN

Lexical Considerations

Prefix/Infix/Postfix Notation

Expressions and Statements. Department of CSE, MIT, Manipal

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

Chapter 7. Expressions and Assignment Statements

UNIT IV INTERMEDIATE CODE GENERATION

Functional Programming Languages (FPL)

Chapter7 Expression and Assignment Statement. Introduction

Lexical Considerations

Intermediate Code Generation

CSE 401 Midterm Exam 11/5/10

CS 314 Principles of Programming Languages

Concepts Introduced in Chapter 6

Operators and Expressions

The SPL Programming Language Reference Manual

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

COP4020 Programming Assignment 1 - Spring 2011

Lecture Chapter 6 Recursion as a Problem Solving Technique

Operators & Expressions

Visual C# Instructor s Manual Table of Contents

HANDLING NONLOCAL REFERENCES

Chapter 3: Operators, Expressions and Type Conversion

Operators and Expressions:

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

A simple syntax-directed

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 7 Control I Expressions and Statements

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

CS 314 Principles of Programming Languages

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 7 Expressions and Assignment statements

2.2 Syntax Definition

Languages and Compiler Design II IR Code Generation I

Functional programming with Common Lisp

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

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Typescript on LLVM Language Reference Manual

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Review of the C Programming Language

G Programming Languages - Fall 2012

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming with Math and Logic

SECTION II: LANGUAGE BASICS

Garbage In/Garbage SIGPLAN Out

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Constants and Variables

Problem with Scanning an Infix Expression

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

low and not larger than high. 18 points

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

The New C Standard (Excerpted material)

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

3. Java - Language Constructs I

Review of the C Programming Language for Principles of Operating Systems

Operators And Expressions

Transcription:

LECTURE 17 Expressions and Assignment

EXPRESSION SYNTAX An expression consists of An atomic object, e.g. number or variable. An operator (or function) applied to a collection of operands (or arguments) each of which are also expressions. Note: conventionally, the term operator refers to a built-in function with special syntax. For example a + b, might really stand for a function call like sum(a, b) or a.sum(b).

EXPRESSION SYNTAX Common syntactic forms for operators: Function call notation, e.g. somefunc(a, B, C) Infix notation for binary operators, e.g. A + B Short for + (A, B) in Ada and A.operator+(B)in C++. Prefix notation for unary operators, e.g. A Postfix notation for unary operators, e.g. i++ Cambridge Polish notation (Lisp) Prefix notation, function inside parentheses: (* (+ 1 3) 2) "Multi-word" infix, e.g. a > b? a : b in C

PRECEDENCE AND ASSOCIATIVITY The use of infix notation sometimes leads to ambiguity as to what operands an operation should be applied to. Fortran example: a+b*c**d**e/f ((((a+b)*c)**d)**e)/f? or a+(((b*c)**d)**(e/f))? or a+((b*(c**(d**e)))/f)? A programming language must be able to disambiguate such expressions. Two options: Add parentheses. Define operator precedence and associativity in the language that specify the order of evaluation in the absence of parentheses.

PRECEDENCE AND ASSOCIATIVITY Operator precedence: higher operator precedence means that a collection of operators group more tightly in an expression than operators of lower precedence. Operator associativity: determines grouping of operators of the same precedence. Left associative: operators are grouped left-to-right (most common). Right associative: operators are grouped right-to-left (Fortran power operator **, C assignment operator = and unary minus). Non-associative: requires parentheses when composed (Ada power operator **). In Fortran: ** > {*, /} > {+, -} and +, -, *, / are left associative and ** is right associative. a+b*c**d**e/f =?

PRECEDENCE AND ASSOCIATIVITY In a language, the number of operators can be quite large. E.g. C/C++ has around 18 different precedence levels. On the other hand, Pascal has about 4. The programmer of a language must be careful about the precedence and associativity. In C/C++, if (A < B && C < D) { } = if ((A < B) && (C < D)) { } In Pascal, if A<B and C<D then = if A<(B and C)<D then and is of higher precedence than equality comparisons.

EVALUATION ORDER Precedence and associativity state the rules for grouping operators in expressions, but do not determine the operand evaluation order! Expression a - f(b) b * c is evaluated as (a - f(b)) - (b * c) but either (a - f(b)) or (b * c) could be evaluated first The evaluation order of arguments in subroutine calls may differ, e.g. arguments evaluated from left to right or right to left. Knowing the operand evaluation order is important. Side effects: suppose f(b) above modifies the value of b (f(b) has a side effect ), then the value will depend on the operand evaluation order. Code improvement: compilers rearrange expressions to maximize efficiency, e.g. a compiler can improve memory load efficiency by moving loads up in the instruction stream. We may want to evaluate f(b) first so that we don t need to save b*c during its execution.

REARRANGING MATHEMATICAL EXPRESSIONS Some languages allow the compiler to rearrange mathematical expressions in accordance with commutative, associative, or distributive laws in order to gain some improvement in efficiency. For example, a = b + c d = c + e + b may be rewritten as a = b + c d = b + c + e which allows the common subexpression to be reused in the intermediate code.

EXPRESSION OPERAND REORDERING ISSUES Rearranging expressions may lead to arithmetic overflow or different floating point results. Assume b, d, and c are very large positive integers, then if b-c+d is rearranged into (b+d)-c arithmetic overflow occurs. Floating point value of b-c+d may differ from b+d-c. Most programming languages will not rearrange expressions when parentheses are used, e.g. write (b-c)+d to avoid problems.

EXPRESSION OPERAND REORDERING ISSUES Design choices Java: expression evaluation is always left to right in the order operands are provided in the source text and overflow is always detected. Pascal: expression evaluation is unspecified and overflows are always detected. C and C++: expression evaluation is unspecified and overflow detection is implementation dependent.

BOOLEAN EXPRESSIONS Boolean expressions are not quite the same as the regular arithmetic expressions. To evaluate an arithmetic expression, all components must be evaluated. To evaluate (a + b) (c + d): compute a + b, compute c + d, compute (a + b) (c + d). T1 = a+b T2 = c+d T3 = T1-T2 To evaluate a boolean expression, we may or may not need to evaluate all components. Consider (a < b) && (c < d). Option 1 (do it like regular arithmetic expression): T1 = a<b T2 = c<d T3 = T1 && T2

SHORT-CIRCUIT EVALUATION To evaluate an boolean expression, we may or may not need to evaluate all components. Consider (a< b) && (c<d). Option 2 (do it without evaluating the whole thing): T1 = a<b if (T1 == false) goto 100 T2 = c<d T3 = T1 && T2 goto 200 100: T3 = false 200: Compute the boolean value for a<b. If false, we re done (the whole expression is false). Otherwise, compute the boolean value for c<d, etc. Is this how C/C++ works?

SHORT-CIRCUIT EVALUATION Computing the Boolean value of an expression without evaluating the whole expression (option 2) is called short-circuit evaluation. With short-circuit evaluation of Boolean expressions, the result of an operator can be determined from the evaluation of just one operand. C, C++, and Java use short-circuit conditional and/or operators. If a in a&&b evaluates to false, b is not evaluated (a && b is false). If a in a b evaluates to true, b is not evaluated (a b is true).

SHORT-CIRCUIT EVALUATION Consider these examples. Why might short-circuit evaluation be so important here? for (i=0; (i<n) && (a[i]!= val); i++)... ; while ((p!= NULL) && (p->value!= val)) p=p->next;

ASSIGNMENTS AND EXPRESSIONS The use of expressions and assignments is the fundamental difference between imperative and functional languages. Imperative: "computing by means of side effects. Computation is an ordered series of changes to values of variables in memory (state) and statement ordering is influenced by run-time testing of values of variables. Pure functional languages: computation consists entirely of expression evaluation. A function always returns the same value given the same arguments because of the absence of sideeffects (no memory state is changed implicitly in such a function).

L-VALUES VS. R-VALUES Consider the assignment of the form: a = b + c The left-hand side of the assignment is an L-value which is an expression that should denote a location. e.g. array element a[2] or a variable foo or a dereferenced pointer *p. The right-hand side of the assignment is an R-value which denotes the value. e.g. an expression composed of variables and/or literals.

VALUE MODEL VS. REFERENCE MODEL There are two general ways to implement an assignment. Languages that adopt the value model of variables copy the value of b+c into the location of a (e.g. Ada, Pascal, C). That is, a is synonymous with some address in memory. Languages that adopt the reference model of variables copy references, resulting in shared data values via multiple references. B = 2 C = B A = B+C A B C 4 2 2 A B C 4 2