UT Dallas CS 4337 - #Sebesta ch07 expression & assignment (39 pages)

Previewing pages 1, 2, 3, 18, 19, 37, 38, 39 of 39 page document View the full content.
View Full Document

#Sebesta ch07 expression & assignment



Previewing pages 1, 2, 3, 18, 19, 37, 38, 39 of actual document.

View the full content.
View Full Document
View Full Document

#Sebesta ch07 expression & assignment

42 views


Pages:
39
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Chapter 7 Expressions and Assignment Statements Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short Circuit Evaluation to be evaluated partially when there is no need to evaluate an expression to the end e g if 1 x Assignment Statements Mixed Mode Assignment Copyright 2012 Addison Wesley All rights reserved 1 2 Introduction Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation need to be familiar with the orders of operator and operand evaluation Essence of imperative languages is dominant role of assignment statements Copyright 2012 Addison Wesley All rights reserved 1 3 Arithmetic Expressions Arithmetic evaluation was one of the motivations for the development of the first programming languages Arithmetic expressions consist of operators operands parentheses and function calls Copyright 2012 Addison Wesley All rights reserved 1 4 Arithmetic Expressions Design Issues Design issues for arithmetic expressions Operator precedence rules Operator associativity rules Order of operand evaluation Operand evaluation side effects Operator overloading Type mixing in expressions Copyright 2012 Addison Wesley All rights reserved 1 5 Arithmetic Expressions Operators A unary operator has one operand A binary operator has two operands A ternary operator has three operands Copyright 2012 Addison Wesley All rights reserved 1 6 Arithmetic Expressions Operator Precedence Rules The operator precedence rules for expression evaluation define the order in which adjacent operators of different precedence levels are evaluated Typical precedence levels parentheses unary operators if the language supports it Copyright 2012 Addison Wesley All rights reserved 1 7 Arithmetic Expressions Operator Associativity Rule The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules Left to right except which is right to left Sometimes unary operators associate right to left e g in FORTRAN APL is different all operators have equal precedence and all operators associate right to left Precedence and associativity rules can be overriden with parentheses Copyright 2012 Addison Wesley All rights reserved 1 8 Expressions in Ruby and Scheme Ruby All arithmetic relational and assignment operators as well as array indexing shifts and bit wise logic operators are implemented as methods One result of this is that these operators can all be overriden by application programs Scheme and Common LISP All arithmetic and logic operations are by explicitly called subprograms a b c is coded as a b c Copyright 2012 Addison Wesley All rights reserved 1 9 Precedence Associativity in Prolog op Precedence Type Name Declare Name to be an operator of type Type with precedence Precedence Name can also be a list of names in which case all elements of the list are declared to be identical operators Precedence is an integer between 0 and 1200 Precedence 0 removes the declaration Operator definitions in Prolog look like this op Precedence Type Name Copyright 2012 Addison Wesley All rights reserved 1 10 Precedence Associativity in Prolog op Precedence Type Name Type is one of xf yf xfx xfy yfx fy or fx The f indicates the position of the functor while x and y indicate the position of the arguments y should be interpreted as on this position a term with precedence lower or equal to the precedence of the functor should occur For x the precedence of the argument must be strictly lower Copyright 2012 Addison Wesley All rights reserved 1 11 Precedence Associativity in Prolog op Precedence Type Name The precedence of a term is 0 unless its principal functor is an operator in which case the precedence is the precedence of this operator A term enclosed in parentheses has precedence 0 In prolog for example op 500 xf is dead kill marsellus zed is dead X kill X Copyright 2012 Addison Wesley All rights reserved 1 12 Prolog http www cpp edu jrfisher www prolog tutorial contents html Other kinds of sequences can be defined by the user For example to make left associative sequences separated by one might use an operator declaration like this op 500 yfx A B 1 2 3 4 A 1 2 3 B 4 1 2 3 4 Notice how left associativity was what determined the bindings in the second goal To make right associative use xfy op 500 xfy A B 1 2 3 4 A 1 B 2 3 4 1 2 3 4 Precedence Associativity in Prolog op Precedence Type Name op 1200 xfx The precedence of a term is 0 unless its principal op 1200 fx operator functor is an in which case the precedence op 1100 xfy is the precedence of this operator A term enclosed op 1000 xfy in parentheses has precedence 0 op 700 xfx is In prolog for example op 500 yfx op 500 xf is dead op 500 fx kill marsellus zed op 300 xfx mod is dead X kill X op 200 xfy Copyright 2012 Addison Wesley All rights reserved 1 14 Arithmetic Expressions Conditional Expressions Conditional Expressions C based languages e g C C An example average count 0 0 sum count Evaluates as if written as follows if count 0 average 0 else average sum count Copyright 2012 Addison Wesley All rights reserved 1 15 Arithmetic Expressions Operand Evaluation Order Operand evaluation order 1 Variables fetch the value from memory 2 Constants sometimes a fetch from memory sometimes the constant is in the machine language instruction 3 Parenthesized expressions evaluate all operands and operators first 4 The most interesting case is when an operand is a function call Copyright 2012 Addison Wesley All rights reserved 1 16 Arithmetic Expressions Potentials for Side Effects Functional side effects when a function changes a two way parameter or a non local variable Problem with functional side effects When a function referenced in an expression alters another operand of the expression e g for a parameter change a 10 assume that fun changes its parameter b a fun a Copyright 2012 Addison Wesley All rights reserved 1 17 Functional Side Effects Two possible solutions to the problem 1 to disallow functional side effects No two way parameters in functions No non local references in functions Advantage it works Disadvantage inflexibility of one way parameters and lack of non local references 2 to demand that operand evaluation order be fixed consistent manner Disadvantage limits some compiler optimizations Java requires that operands appear to be


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view #Sebesta ch07 expression & assignment and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view #Sebesta ch07 expression & assignment and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?