COP4020 Programming Languages - PowerPoint PPT Presentation

About This Presentation
Title:

COP4020 Programming Languages

Description:

Title: Array Dependence Analysis and Vectorization with the Chains of Recurrences Framework Author: Robert van Engelen Last modified by: surfing Created Date – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 16
Provided by: Robert2044
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: COP4020 Programming Languages


1
COP4020Programming Languages
  • Expression and assignment
  • Prof. Xin Yuan

2
Overview
  • Expression and assignment statement

3
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. agtb?ab in C

4
Operator Precedence and Associativity
  • The use of infix notation sometimes leads to
    ambiguity as to what is an operand of what
  • Fortran example abcde/f
  • ((((ab)c)d)e)/f
  • a(((bc)d)(e/f))
  • a((b(c(de)))/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.

5
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 gt , / gt , - and , -, , /
    are left associated and is right associated
  • Abcde/f ?

6
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 lt B C lt D) If ((A lt B) (C lt
    D))
  • In Pascal if AltB and CltD thenis grouped as
    follows if Alt(B and C)ltD then

7
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)-bcis structured
    as (a-f(b))-(bc)but either (a-f(b)) or (bc)
    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

8
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-cd is rearranged into
    (bd)-c arithmetic overflow occurs 
  • Floating point value of b-cd may differ from
    bd-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

9
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 (ab) (cd) compute ab, compute
    cd, compute (ab) (cd)
  • T1 ab
  • T2 cd
  • T3 T1-T2
  • To evaluate an boolean expression, we may or may
    not evaluate all components. Consider (alt b)
    (cltd)
  • Option 1 (do it like regular arithmetic
    expression)
  • T1 altb
  • T2 cltd
  • T3 T1 T2

10
Boolean expression and short-Circuit Evaluation
  • To evaluate an boolean expression, we may or may
    not evaluate all components. Consider (alt b)
    (cltd)
  • Option 2 do it without evaluate the whole thing
  • T1 alt b
  • If (T1 false) goto 100
  • T2 cltd
  • T3 T1 T2
  • goto 200
  • 100 T3 false
  • 200
  • Compute boolean value for altb, if false, done
    (the whole expression is false). Otherwise,
    compute boolean value for cltd, 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?

11
Short circuit evaluation examples
  • for (i0 (iltN) (ai ! val) i)
  • while ((p! NULL) (p-gtvalue ! val))
    pp-gtnext

12
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 ab evaluates to false, b is not
    evaluated (a b is false)
  • If a in ab evaluates to true, b is not
    evaluated (a b is true).

13
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).

14
L-Values vs. R-Values
  • Consider the assignment of the form a bc
  • 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 a2 or a variable
    foo or a dereferenced pointer p
  • A variable in the right-hand side (bc) of the
    assignment is an r-value that denotes the value. 

15
Value Model vs. Reference Model
  • Two general ways to implement an assignment
  • Languages that adopt the value model of variables
    copy the value of bc 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 bc into a so that a
    and bc 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

Value model
Reference model
4
A
4
A
B 2 C B A BC
2
B
B
2
C
2
C
Write a Comment
User Comments (0)
About PowerShow.com