Computers/Programming Language

ch7. Expression and Assignment Statements

emzei 2012. 3. 21. 14:17

 Introduction

    - Expressions(수식) are the fundamental means of specifying computation

    - crucial to understand both the syntax and semantics of expressions

    - methods of describing the syntax are described in ch3

    - semantics of expression





▷ Arithmetic Expression

     - to specify an arithmetic computation

     - consist of operator, operand, parentheses, function calls.

♤ operator : unary, binary, ternary


     ♠ Design issue

- operator precedence rules?

- operator associativity rules?

- what is the order of operand evaluation?

- are there restrictions on operand evaluation side effects?

- does the language allow user-defined operator overloading?

- what mode mixing is allowed in expression?


     ♠ Operator Evaluation Order

♤ precedence 

   : operators of different precedence levels are evaluted

♤ associativity

   : the same level of precedence, which operator is evaluated first

♤ parentheses

♤ conditional expression

   : ternary operator :  ? :


     ♠ Precedence and associativity of operators in C


     ♠ Conditional Expression

- syntax in C   

   expr1 ? expr2 : expr3


     ♠ Operand Evaluation Order

- if neither of the operands of an operator has side effects, then operand evaluation order is irrelevant


- Side effect

   : side effect of a function, called a functional side effect, occurs when the function changes either

* one of its parameters, or

* a global variable


   ex> function side effect 1

int a

int f()

{

a = a+1

return 20

}

main()

{

a = 10

a = a+f()

print a // 30? or 31?

}


  - Solutions

* disallow functional side effect

   : difficult and eliminates some flexibility

* state in the language definition that operands in expressions are to be evaluated in a particular order.






 Overloaded Operation : multiple use of an operator

      ♤ Example

▶ operator + for

  - integer addition

  - real addition

  - complex addition

  - string catenation

  - set union

▶ operator & for 

  - bit-wise logical AND

  - address operator


      ♤ difficult error detection (good or bad)






 Type Conversion

     ♤ Narrowing conversion

- double to float

     ♤ Widening conversion

- int to float

     ♤ Coercion in expression

- mixed mode expression - coercion rule

ex. i = i + f

실제 ) 

i = (int) ( (float) i + f )

      강제적 형변환 by compiler ~ coercion

     ♤ explicit type conversion

- cast

     ♤  errors in expression

- underflow, overflow, or division by zero





 Relational and Boolean Expressions

     ♤  Relational expression

- compare the values of two operands

- its value is boolean (or logical)


     ♤ Boolean expression

- consists of boolean variables, boolean constants, relation expression, and boolean operator






 Short-circuit Evaluation (SCE)

    : the expression which the result is determined without evaluating all of the operands and/or operators

ex. (in C)

  (13 * A) * (B / 13 - 1)

  (A >= 0) and (B < 10)


♠ good point of SCE

  - performance : Example

var list[1...listlen] array of integer;

var key : integer;

index := 1;

while ( (index <= listlen) and (list[index] <> key)) do

index := index +1

♠ Problem with SCE

  - problem of allowing side effect

( a > b ) || ( b++ / 3)

//앞에가 참이면 뒤의 기능 수행 안함. 수학적이지않음.


 ♤ Examples

   - Ada allows the programmer to specify SCE of the boolean operators by using two-word operator "and then" and "or else"

   - example

INDEX := 1;

while ( INDEX <= LISTLEN ) and then (LIST(INDEX) /= KEY)

loop

INDEX := INDEX +1;

end loop;






 Assignment Statements

♠ Simple assignments

    ♤ syntax : 

<variable><assignment_operator><expression>

    ♤ semantics :

MEM[ l-value(<variable>) ] <--assign-- r-value(<expression>)


♠ FORTRAN, BASIC, PL/I, C++ use the equal sign =

ex. " A = B = C " (in PL/I) is same as " A = B == C " (in C)

♠ Other languages use :=


♠ Alternatives

    - the design choice of how assignment are used

    - in FORTRAN, Pascal, and Ada 

: it can only  appear as a stand-alone statement and the destination is restricted to a single variable

    - Multiple targets : PL/I, C, C++, COBOL

sum, total = 0;

move zero to sum, total

    - conditional target : C++

flag ? count1 : count2 = 0;

    - compound assignment operators : C, C++

: a shorthand method of specifying a commonly 

  needed form of assignment

sum += value; /* sum = sum+value */

    - unary assignment operator : C, C++

sum = ++count

    - Assignment as an expression

while((ch=getchar()) != EOF) {...}

  Disadvantage of allowing assignment statement to be operands 

      in expression.

♤ expression side effect

  a = b + ( c = d / b++ ) - 1

♤ common error in C

  if ( x = y )...


Alternatives... continue

 Mixed-Mode assignment

♤ Does the type of the expression have to be the same as the type of the variable being assigned or can coercion be used in some cases of type mismatch?


♤ FORTRAN, C, C++

   - many of the possible type mixes are legal with coercion freely applied

♤ Pascal

   - some assignment coercion

♤ Ada, Modula2

   - do not allowed



'Computers > Programming Language' 카테고리의 다른 글

PL/0 Virtual Machine - instruction  (0) 2012.03.31
ch8. Statement-Level Control Structure  (0) 2012.03.22
ch6-2. Data Type  (0) 2012.03.19
ch6-1. Data Type  (0) 2012.03.17
ch5. Names, bindings, type checking, and scope  (0) 2012.03.16