COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.

Slides:



Advertisements
Similar presentations
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
ISBN Chapter 7 Expressions and Assignment Statements.
COEN Expressions and Assignment
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,
CS2403 Programming Languages Expressions and Assignment Statements Chung-Ta King Department of Computer Science National Tsing Hua University (Slides are.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Evaluation and Assignments
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
Compiler Chapter# 5 Intermediate code generation.
1 Control Flow: Expressions, Sequencing & Selection (Section ) A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 12.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
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.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
COP4020 Programming Languages Control Flow Prof. Robert van Engelen.
Semantics (1).
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
Chapter Seven: Expressions and Assignment Statements Lesson 07.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
ISBN Chapter 7 Expressions and Assignment Statements.
Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions & Assignments
Chap. 6 :: Control Flow Michael L. Scott.
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
College of Computer Science and Engineering
COP4020 Programming Languages
Chap. 6 :: Control Flow Michael L. Scott.
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements.
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Presentation transcript:

COP4020 Programming Languages Expression and assignment Prof. Xin Yuan

COP4020 Spring /25/2015 Overview Expression and assignment statement

COP4020 Spring /25/2015 Expression Syntax An expression consists of  An atomic object, e.g. number or variable  An operator applied to a collection of operands (or arguments) that are expressions Common syntactic forms for operators:  Function call notation, e.g. somefunc(A, B, C)  Infix notation for binary operators, e.g. A + B  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

COP4020 Spring /25/2015 Operator Precedence and Associativity The use of infix notation sometimes leads to ambiguity as to what is an operand of what  Fortran example: a+b*c**d**e/f ((((a+b)*c)**d)**e)/f a+(((b*c)**d)**(e/f)) a+((b*(c**(d**e)))/f) A programming language must be able to disambiguate such expressions  Add parentheses  Define operator precedence and associativity in the language that specify the order of evaluation in the absence of parentheses.

COP4020 Spring /25/2015 Operator 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 parenthesis when composed (Ada power operator **) In Fortran: ** > {*, /} > {+, -} and +, -, *, / are left associated and ** is right associated  A+b*c**d**e/f = ?

COP4020 Spring /25/2015 Operator Precedence and Associativity In a language, the number of operators can be quite large.  E.g. C/C++  The designer 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 is grouped as follows if A<(B and C)<D then

COP4020 Spring /25/2015 Evaluation Order of Expressions 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 structured as (a-f(b))-(b*c) but either (a-f(b)) or (b*c) can be evaluated first The evaluation order of arguments in function and 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

COP4020 Spring /25/2015 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 parenthesis are used, e.g. write (b-c)+d to avoid problems Design choices:  Java: expressions 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 anc C++: expression evaluation is unspecified and overflow detection is implementation dependent

COP4020 Spring /25/2015 Boolean expression and short- Circuit Evaluation Boolean expressions is 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 an boolean expression, we may or may not 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

COP4020 Spring /25/2015 Boolean expression and short- Circuit Evaluation  To evaluate an boolean expression, we may or may not evaluate all components. Consider (a< b) && (c<d) Option 2: do it without evaluate the whole thing T1 = a< b If (T1 == false) goto 100 T2 = c<d T3 = T1 && T2 goto : T3 = false 200: Compute boolean value for a<b, if false, done (the whole expression is false). Otherwise, compute boolean value for c<d, etc.  Computing the boolean value of an expression without evaluating the whole expression (option 2) is called short circuit evaluation. Do we assume short circuit evaluation in C/C++ coding?

Short circuit evaluation examples for (i=0; (i<N) && (a[i] != val); i++); while ((p!= NULL) && (p->value != val)) p=p->next; COP4020 Spring /25/2015

COP4020 Spring /25/2015 Short-Circuit Evaluation 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 (require) 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).

COP4020 Spring /25/2015 Assignments and Expressions The use of expression 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 values of variables  Pure functional languages: computation consists of entirely of expression evaluation. A function is idempotent in a functional language: it always returns the same value given the same arguments because of the absence of side- effects (no memory state is changed implicitly in such a function).

COP4020 Spring /25/2015 L-Values vs. R-Values Consider the assignment of the form: a := b+c  The left-hand side a 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  A variable in the right-hand side (b+c) of the assignment is an r-value that denotes the value.

COP4020 Spring /25/2015 Value Model vs. Reference Model 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)  Languages that adopt the reference model of variables copy references, resulting in shared data values via multiple references Clu copies the reference of b+c into a so that a and b+c refer to the same object Java is a mix: it uses the value model for built-in types and the reference model for class instances  Example: B = 2 C= B A = B+C A B C Value modelReference model A B C 4 2