Expressions and Assignment

Similar documents
Chapter 7. Expressions and Assignment Statements

Expressions and Assignment Statements

Expressions & Assignment Statements

Chapter 7. Expressions and Assignment Statements ISBN

Software II: Principles of Programming Languages. Why Expressions?

Chapter 7. Expressions and Assignment Statements

Chapter 7. Expressions and Assignment Statements ISBN

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

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

Chapter7 Expression and Assignment Statement. Introduction

Expression Evaluation and Control Flow. Outline

Chapter 7 Expressions and Assignment statements

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

LECTURE 17. Expressions and Assignment

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

Concepts of Programming Languages

G Programming Languages - Fall 2012

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Chapter 6 Control Flow. June 9, 2015

HANDLING NONLOCAL REFERENCES

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Programming Languages, Summary CSC419; Odelia Schwartz

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

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

Operators. Java operators are classified into three categories:

Operators and Expressions

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

MIDTERM EXAMINATION - CS130 - Spring 2005

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Introduction to Programming Using Java (98-388)

The role of semantic analysis in a compiler

Functional Programming. Pure Functional Programming

Lexical Considerations

1 Lexical Considerations

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

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

CSE 431S Type Checking. Washington University Spring 2013

NOTE: Answer ANY FOUR of the following 6 sections:

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

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Computers Programming Course 6. Iulian Năstac

LECTURE 18. Control Flow

Chapter 3: Operators, Expressions and Type Conversion

Intermediate Code Generation

Data Structures and Algorithms

Lexical Considerations

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

Expressions and Statements. Department of CSE, MIT, Manipal

Evaluation of postfix

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

UNIT- 3 Introduction to C++

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Names, Bindings, Scopes

22c:111 Programming Language Concepts. Fall Types I

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

COP4020 Programming Assignment 1 - Spring 2011

Types and Type Inference

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

syntax tree - * * * * * *

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

Lecture Set 4: More About Methods and More About Operators

MIDTERM EXAMINATION - CS130 - Spring 2003

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Lecture Set 4: More About Methods and More About Operators

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

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

MIDTERM EXAM (Solutions)

The Arithmetic Operators

The SPL Programming Language Reference Manual

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

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Stating the obvious, people and computers do not speak the same language.

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Pierce Ch. 3, 8, 11, 15. Type Systems

Stacks, Queues and Hierarchical Collections

Type Bindings. Static Type Binding

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

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

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

Functional programming with Common Lisp

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

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

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

SOFTWARE ARCHITECTURE 6. LISP

UMBC CMSC 331 Final Exam

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Prefix/Infix/Postfix Notation

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

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

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Arithmetic Operators. Portability: Printing Numbers

Transcription:

Expressions and Assignment COS 301: Programming Languages Outline Other assignment mechanisms Introduction Expressions: fundamental means of specifying computations Imperative languages: usually RHS of assignment statements Functional languages: just the function evaluation Need to understand order of operator, operand evaluation Maybe only partially specified by associativity, precedence If not completely specified maybe different results in different implementations Introduction Other issues: type mismatches, coercion, shortcircuit evaluation For imperative languages: dominant role of assignment to memory cells

Outline Other assignment mechanisms Arithmetic expressions Arithmetic expression evaluation primary motivation for first programming languages Parts: operators operands parentheses (grouping) function calls Design issues for arithmetic Operator precedence Associativity Operand evaluation order Operand evaluation side effects? Overloading? Type mixing Operator arity Unary operators Binary operators Ternary operators n-ary operators

Operator precedence rules Define order in which adjacent operators are evaluated Typical precedence levels: parentheses unary operators ** (if present) *, / +, - relational operators Operator precendence FORTRAN, ADA ** Ruby ** C-based postfix ++, -- *, /, % +,-, unary - unary +,- *, /, % unary +, -prefix ++,--, not *, /, % +,- +,- Associativity Operator associativity rules order adjacent operators at same precedence level are eval d Typical: Left to right, except for ** Unary ops: sometimes right left (e.g., FORTRAN) APL: all operators have same precedence, all associate right left Smalltalk: binary methods ( operators ) have same precedence, left associativity Parentheses: can override precedence, associativity Operator associativity Left-assoc relational ops: a < b < c okay but in C if (a<b) then (1<c) else (0<c) not (a<b) && (b<c) Non-assoc ops: things like a < b < c are illegal Lang +,-,*,/ Unary - **!=, ==,<. C-like L R n/a L Ada L non-assoc non-assoc non-assoc FORTRAN L R R L VB L R L non-assoc

Operator associativity Optimizing compilers may reorder associative +,* E.g.: x * y * z * w can be evaluated in any order If x, z very large, y, w very small makes a difference! Floating point: lose precision, produce infinities Integers: overflow, wraparound Can always override with parentheses Expressions in Ruby, Smalltalk No operators per se All operators implemented as methods of objects Includes arithmetic, relational, assignment operators Includes array indexing, shifts, bitwise ops Can override all within application programs Ternary conditional operator Conditional ternary operators: in most C-like languages Format: Ex: Same as: condition? then-stmt : else-stmt average = count == 0? 0 : sum/count; if (count == 0) else average = 0; average = sum/count; Ternary conditional operator E.g., VB s IIf() function: PF = IIf(grade >= 60, Pass, Fail ) Similar to FORTRAN s arithmetic IF statement IF (I-3) 10, 20, 30 More general Also a value (r-value)

Ternary conditional operator Not just r-values, but also l-values in some languages E.g., C, C++, Java (but not JavaScript): ((x == y)? a : b) = 1; Operand evaluation order If a variable fetch from memory If a constant: Statically-bound already in code Dynamic fetch from memory Parentheses affect order, of course Evaluation order: Generally irrelevant except when operand is a function with side effects Operand evaluation order Example: int foo(int* val) { } *val = *val * *val; return (*val); a = 10; b = a * foo(&a); If a eval d first, then b = 10 * 100 = 1000; If foo(a) eval d first, then b = 100 * 100 = 10,000! Side effects In general: A subroutine that returns a value is a function A subroutine that does not is a procedure Functions should not have side-effects One opinion: Procedures really shouldn t have any side-effects other than modifying one or more arguments (not as widely-accepted) Most languages: no way to enforce this

Possible solutions 1. Write language to disallow side-effects No pass by reference to function No non-local references allowed in function Advantage: works! Disadvantage: inflexible 2. Write language to demand that operand order be fixed Disadvantage: eliminates some compiler optimizations Java, Lisp: Operands eval d from left right C, C++: no fixed order Referential transparency Expression is referentially transparent if it can be replaced by its value without changing the program ans1 = (fun(a) + b) / (fun(a) + c); temp = fun(a); ans2 = (temp + b) / (temp + c); Referentially transparent if ans2 = ans1 Absence of functional side-effects is necessary (but not sufficient) for referential transparency More in functional languages Outline Other assignment mechanisms Relationship of operators to operands Most languages: infix notation what we use in arithmetic operators between operands e.g., 3 + 4 Some languages: prefix notation operators first, then operands e.g., + 3 4 Some languages: postfix notation operators last, after operands e.g., 3 4 +

Infix expressions Infix inherently ambiguous without defined associativity and precedence Different parse trees from different precedence, associativity as specified in grammar E.g., a + b - c * d Smalltalk: ((a + b) - c) * d APL: a + (b - (c * d)) Usually means (a + b) - (c * d) Prefix & postfix Both prefix and postfix are unambiguous Infix: (a + b) - (c * d) Prefix: - + a b * c d Postfix: a b + c d * - Postfix also known as Reverse Polish Notation (RPN) introduced by Polish mathematician Jan Lukasiewicz in early 20th century Obtaining postfix/prefix Consider expression tree for intended meaning: Prefix: preorder traversal Postfix: postorder traversal Infix: inorder traversal (and use depth for precedence, associativity from language definition) Evaluating postfix read token; while (not EOF) { } if token is an operand then push token onto stack; else // for n-ary operators endif pop top n operands from stack; perform operation; push result onto stack; read token; pop result from stack;

Postfix (RPN) example Input: 3 4 5 * - (Infix 3 4 * 5) Token Action Stack 3 Push (3) 4 Push (3 4) 5 Push (3 4 5) * Pop 5, Pop 4; Push 4*5 = 20 (3 20) - Pop 20, Pop 3 Push 3 20 = -17 (-17) EOF Pop and return -17-3 * 4 5 Postfix example Input: 2 3 * 12 3 / + 5 3 * 6 - + Token Action Stack 2 Push (2) 3 Push (2 3) * Pop 3, Pop 2; Push 2 * 3 = 6 (6) 12 Push (6 12) 3 Push (6 12 3) / Pop 3, Pop 12; Push 12/3 = 4 (6 4) + Pop 4, Pop 6 Push 6+4 = 10 (10) 5 Push (10 5) 3 Push (10 5 3) * Pop 3, Pop 5; Push 5*3 = 15 (10 15) 6 Push (10 15 6) - Pop 6, Pop 15 Push 15-6 = 9 (10 9) + Pop 9, Pop 10 Push 10+9 = 19 (19) EOL Pop and return 19 Languages using postfix RPN calculators Forth e.g., a square function in Forth: : squareit dup * ; 3 squareit. 9 Postscript (and PDF) start here, 11/13/2014 Unary operators in postfix or prefix Can t have same operator be both unary and n-ary Don t know how many to take off stack, e.g. (postfix) One solution: use different operators Another solution: Cambridge Polish notation Parenthesized E.g., for Cambridge Polish prefix notation: (+ a b c d) = a + b + c + d

Languages using prefix notation Logo Lisp & Scheme pretty much Cambridge Polish prefix notation E.g.: Infix: 3 + (4 * 5) - (-23) Prefix: - + 3 * 4 5 ~ 23 Lisp: (- (+ 3 (* 4 5) -23)) or (- (+ 3 (* 4 5) (- 23))) Outline Mixed-mode assignment Overloaded operators Using operator for more than one thing operator overloading Common: + for int & float (e.g.) Problematic: E.g., * in C/C++ Can t really detect missing operator: a = *foo * 2; a = foo * 2; User-defined overloading Some languages allow user-defined overloads (C++, C#, Ada ) E.g., Ada: function + (a,b : complex) return complex;

User-defined overloading Functional languages E.g., Lisp (defvar *old+* #'+) (defmethod + ((a number) &rest args) (apply *old+* (cons a args))) (defmethod + ((a string) &rest args) (apply #'concatenate (cons string (cons a args)))) OO languages Ruby Smalltalk Problematic overloading JavaScript, Python: + is addition & concatenation E.g., JavaScript var x = 10 ; var y = x + 5; // y = 105 var z = x - 3; // z = 7 E.g., Python x = 10 y = x + hi # 10hi z = x + 3 # error z = eval(x) + 3 # 13 Outline Other assignment mechanisms Type conversions: Reprise narrowing conversion: long integer integer float integer widening conversion: integer long integer integer float (i.e., float can represent (at least approximately) all integers in range considered widening if conversion retains magnitude, even if it loses precision

Mixed mode conversions Operands of different types mixed-mode expression Requires coercion by language Decreases error detection ability of compiler Most languages: all numeric types are automatically coerced in expressions use widening conversions Ada: virtually no coercion in expressions, however: forces user to do casting Coercion and casting inefficient require runtime code Casting Explicit type conversions E.g., C: (int) angle E.g., Ada: Float (Sum) Ada, many other languages: look like function call Outline Mixed-mode assignment Relational expressions Relational expressions: Relational operators, operands of various types Evaluate to Boolean Operators vary: e.g.,!=, /=, ~=,.NE., <>, # JavaScript, PHP: Two additional operators: === and!== Like == and!=, but no coercion of operands

Boolean expressions Operands, operators are Boolean Examples: FORTRAN 77 FORTRAN 90 C Ada.AND. and && and.or. or or.not. not! not xor Languages without Booleans C uses int: 0 = false, nonzero = true reprise: 5 < 3 < 4 legal expression, but odd left-associative (5 <3) < 4 0 < 4 1 3 < 5 < 4 1 < 4 1 Languages without Booleans Python: Originally no True/False: 0,, (), [], {} nil, other true Now: 0, 1 but also True/False Perl: 0, 0, (), etc. false; else true Lisp: nil, t anything not nil true Other assignment mechanisms

Short-circuit evaluation : stop executing the (Boolean) expression when some condition met For and expressions: stop when false encountered For or expressions: stop when true encountered E.g.: Node p = head; while (p!= null && p.info!= key) p = p.next; if (p == null) // not in list... else // found it... If p null doesn t try to eval p.info Short-circuit evaluation Without short-circuit, would have to do, e.g.: boolean found = false; while (p!= null &&! found) { if (p.info == key) found = true; else p = p.next; } Short-circuit evaluation Not all languages support it C, C++, Java: yes, for && and Ada, VB (.Net): Programmer can specify Use: and then, or else Short-circuit evaluation Lisp: or, and (or (and (numberp a) (> a b)) (and (stringp a) ) Often used in lieu of conditionals Can use to prevent some problems: if (count!= 0 && total/count > 10)

APL: a 2 3 4 5 Short-circuit evaluation Potential problem: not calling functions whose side-effects you want: if f(a, b) && g(y) { /* do something */ } Never calls g() if f() returns false E.g., if ((a > b) (b++ / 3)){ } If you were counting on b++ Outline Other assignment mechanisms Assignment statements General syntax target assign-op expression Assignment operator: FORTRAN, BASIC, C-based languages: foo = bar * baz; ALGOL, Pascal, Ada: foo := bar * baz; Assignment statements Lisp assignments are function-like (set a 3) (setq a 3) (setf a 3) (setf (cdr b) 4)

Targets Usually a variable: l-value address Can also be a conditional assignment target in some languages E.g., recall C s ternary operator, also in Perl: ($flag? $total : $subtotal) = 0; Either $total or $subtotal 0, depending on $flag Compound assignment Shorthand for commonly-needed assignment idioms a = a + b; // replace by: a += b; Introduced in ALGOL, adopted by all later C-based languages & VB Can be used with almost any binary operator: a += b; a -= b; a /= b; a &&= b; a = b; a *= b; // careful! easy to confuse with *a = b y <<= 1; $s.= PHP string concatenation ; Multiple assignments Some languages: multiple assignments/statement Simple: C: a = b = 0 Python: a, b = b, a Lisp: (setf a 3 b 4 ) Outline Other assignment mechanisms

Unary assignment Most C-based languages: pre- and postoperators ++ and -- Assignment operators of a sort: change the value of a variable Derived from INC, DEC machine instructions Examples: sum = ++count; //inc count then add to sum sum = count++; //add to sum then inc count count--; //dec count, same as -count; n = -count++; // same as (count++) x = *p++; // inc pointer p after dereference x = (*p)++; // dereference then inc value Assignment expressions C, C++, Java: assignment statement returns a value while ((ch = getchar())!= EOF) { } void strcopy (char *s, char *t) { while (*s++ = *t++); } Functional languages (of course) (cond ((and (setq ch (read-char t nil :EOF)) (not (eq :EOF ch))) Assignment expressions Assignment operator usually has low precedence need parentheses in assignment expressions Assignment expressions a type of side-effect readability issues a = b + (c = d / b) - 1; Useful for multiple assignment: total = subtotal = count = 0; Note: assignment has to be right-associative for this to work like this! List assignments Perl, Ruby: ($first, $second, $third) = (20, 30, 40); Swapping variables: ($first, $second) = ($second, $first); Lisp: (destructuring-bind (a b) '(2 3) (list b a))