cps 506 comparative programming languages names, scope, expression

49
CPS 506 Comparative Programming Languages Names, Scope, Expression

Upload: marilyn-mason

Post on 27-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CPS 506 Comparative Programming Languages Names, Scope, Expression

CPS 506Comparative Programming

LanguagesNames, Scope,

Expression

Page 2: CPS 506 Comparative Programming Languages Names, Scope, Expression

Names

• Design issues– Case sensitivity– Special Words (Keywords, Reserve words)

• Length– If too short, they cannot be meaningful– Language examples:

• FORTRAN 95: maximum of 31• C99: no limit but only the first 63 are significant; also,

external names are limited to a maximum of 31• C#, Ada, and Java: no limit, and all are significant• C++: no limit, but implementers often impose one

Page 3: CPS 506 Comparative Programming Languages Names, Scope, Expression

Names

• Special characters–PHP• Variable names begin with dollar signs• Case sensitive• Starting with a letter or underscore$var = 'Bob';$Var = 'Joe';echo "$var, $Var";

Page 4: CPS 506 Comparative Programming Languages Names, Scope, Expression

Names

• Special characters– Perl: all variable names begin with special

characters, which specify the variable’s type• Case sensitive• Starting with a letter or underscore• $ for scalar variable$yname="test";

• @ for array variable@years=(2003,2004,2005);

• % for hash variable%navBar=('1' => 'index‘,'2' => 'perl‘,'3' => 'php’);

Page 5: CPS 506 Comparative Programming Languages Names, Scope, Expression

Names

• Special characters–Ruby: • Local variables start with lowercase letter • Global variables start with $• Instance variables begin with @• Class variables begin with @@

– Python• Case sensitive• Starting with a letter or underscore

(Details in their own sessions)

Page 6: CPS 506 Comparative Programming Languages Names, Scope, Expression

Names

• Case sensitivity–Disadvantage: readability (names

that look alike are different)• Names in the C-based languages are case sensitive• Names in others are not•Worse in C++, Java, and C# because predefined names are mixed case (e.g. IndexOutOfBoundsException)

Page 7: CPS 506 Comparative Programming Languages Names, Scope, Expression

Names

• Special words

– An aid to readability; used to delimit or separate statement clauses

• A keyword is a word that is special only in certain contexts, e.g., in Fortran

– Real VarName (Real is a data type followed with a name, therefore Real is a keyword)

– Real = 3.4 (Real is a variable)

• A reserved word is a special word that cannot be used as a user-

defined name

• Potential problem with reserved words: If there are too many, many collisions occur (e.g., COBOL has 300 reserved words!)

Page 8: CPS 506 Comparative Programming Languages Names, Scope, Expression

Variables

• Six attributes–Name

– Address (l-value)

– Type

– Value (r-value)

– Scope

– Lifetime

Page 9: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope

• The scope of a variable is the range of statements over which it is visible

• The nonlocal variables of a program unit are those that are visible but not declared there

• The scope rules of a language determine how references to names are associated with variables

Page 10: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope

• Static scope–Scope of a variable can be statically

determined prior to execution–C, Pascal, Java, Scheme, ML, Haskell

• Dynamic Scope– Is based on the calling sequence of

subprograms, and thus can be determined only at run-time–Original LISP, logo, Emacs LISP

• Common LISP and Perl have both

Page 11: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope (con’t)

• To connect a name reference to a variable, you (or the compiler) must find the declaration

– Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name

– Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent

Page 12: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope (con’t)

• Variables can be hidden from a unit by having a "closer" variable with the same name–Ada allows access to these "hidden" variablesE.g. unit.name

Page 13: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope (con’t)

• Example in C:

void sub() {

int count;

while (...) {

int count;

count++;

...

}

}

- Note: legal in C and C++, but not in Java and C# - too error-prone

Page 14: CPS 506 Comparative Programming Languages Names, Scope, Expression

Labeled Namespaces

• A labeled namespace is any language construct that contains – Definitions

– A region of the program where those definitions apply

– A name that can be used to access those definitions from outside the construct

• ML has one called a structure…

Page 15: CPS 506 Comparative Programming Languages Names, Scope, Expression

ML Structures

• A little like a block: a can be used anywhere from definition to the end

• But the definitions are also available outside, using the structure name: Fred.a and Fred.f

structure Fred = struct val a = 1; fun f x = x + a;end;

Page 16: CPS 506 Comparative Programming Languages Names, Scope, Expression

Other Labeled Namespaces

• Namespaces that are just namespaces:–C++ namespace–Modula-3 module–Ada package– Java package

• Namespaces that serve other purposes too:–Class definitions in class-based

object-oriented languages

Page 17: CPS 506 Comparative Programming Languages Names, Scope, Expression

Example

• The variables min and max would be visible within the rest of the class

• Also accessible from outside, as Month.min and Month.max

public class Month { public static int min = 1; public static int max = 12; …}

Page 18: CPS 506 Comparative Programming Languages Names, Scope, Expression

Namespace Advantages

• Two conflicting goals:–Use memorable, simple names like max–For globally accessible things, use

uncommon names like maxSupplierBid, names that will not conflict with other parts of the program

• With namespaces, you can accomplish both:–Within the namespace, you can use max–From outside, SupplierBid.max

Page 19: CPS 506 Comparative Programming Languages Names, Scope, Expression

Declaration Order

• C, C++, Java, and C# allow variable declarations to appear anywhere a statement can appear

– In C, C++, and Java, the scope of all local variables is from the declaration to the end of the block

– In C#, the scope of any variable declared in a block is the whole block, regardless of the position of the declaration in the block

» However, a variable still must be declared before it can be used

Page 20: CPS 506 Comparative Programming Languages Names, Scope, Expression

Declaration Order (con’t)

• In C++, Java, and C#, variables can be declared in for statements• The scope of such variables is

restricted to the for construct

Page 21: CPS 506 Comparative Programming Languages Names, Scope, Expression

• Based on calling sequences of program units, not their textual layout (temporal versus spatial)

• References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point

Dynamic Scope

Page 22: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope ExampleBig - declaration of X Sub1 - declaration of X - ... call Sub2 ...

Sub2 ... - reference to X - ...

... call Sub1 …

Big calls Sub1Sub1 calls Sub2Sub2 uses X

Page 23: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope Example (con’t)• Static scoping

• Reference to X is to Big's X

• Dynamic scoping

• Reference to X is to Sub1's X

• Evaluation of Dynamic Scoping:

• Advantage: convenience

• Disadvantages:

– While a subprogram is executing, its variables are visible to all subprograms it calls

– Impossible to statically type check

– Poor readability- it is not possible to statically determine the type of a variable

Page 24: CPS 506 Comparative Programming Languages Names, Scope, Expression

Scope and Lifetime

• Scope and lifetime are sometimes closely related, but are different concepts

• The scope of a variable specifies where a variable’s name can be used in a program

• The lifetime of a variable specifies how long the storage for that variable exists in memory.

• Consider a static variable inside a C function– Its scope is static and local to that function– Its lifetime extends over the entire execution of the

program.

Page 25: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression

• 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

Page 26: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• Arithmetic Expression

– Arithmetic evaluation was one of the motivations for the development of the first programming languages

– Arithmetic expressions consist of• Operators• Operands• Parentheses• function calls

Page 27: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• Design issues for arithmetic expressions–Operator precedence rules?–Operator associativity rules?–Order of operand evaluation?–Operand evaluation side effects?

A + FUN(A)int b = 10; System.out.println((b=3) + b);

–Operator overloading?–Type mixing in expressions?

Page 28: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• Operators–A unary operator has one operandb = !a;

–A binary operator has two operandsc = a + b;

–A ternary operator has three operandsint absValue = (a < 0) ? -a : a;

Page 29: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• 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)

• *, /

• +, -

Page 30: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• 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 and Smalltalk have no operator precedence rules– APL: strictly right to left

– Smalltalk: strictly left to right

– Precedence and associativity rules can be overridden with parentheses

Page 31: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• Expressions in 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 overridden by application programs

Page 32: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• Conditional Expressions

– C-based languages (e.g., C, C++)

– An example:

average = (count == 0)? 0 : sum / count

– Evaluates as if written like

if (count == 0)

average = 0

else

average = sum /count

Page 33: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• Operand evaluation order

– Variables: fetch the value from memory

– Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction

– Parenthesized expressions: evaluate all operands and operators first

– The most interesting case is when an operand is a function call

Page 34: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• 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);

Page 35: CPS 506 Comparative Programming Languages Names, Scope, Expression

Expression (con’t)

• Two possible solutions to the problem

– Write the language definition 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

– Write the language definition to demand that operand evaluation order be fixed

• Disadvantage: limits some compiler optimizations

• Java requires that operands appear to be evaluated in left-to-right order

Page 36: CPS 506 Comparative Programming Languages Names, Scope, Expression

Overloaded Operators

• Use of an operator for more than one purpose is called operator overloading

• Some are common (e.g., + for int and float)

• Some are potential trouble (e.g., * in C and C++)

– Loss of compiler error detection (omission of an operand should be a detectable error)

– Some loss of readability

• C++ and C# allow user-defined overloaded operators

• Potential problems:

– Users can define nonsense operations

– Readability may suffer, even when the operators make sense

Page 37: CPS 506 Comparative Programming Languages Names, Scope, Expression

Relational and Boolean Expressions

• Relational Expressions– Use relational operators and operands of various types– Evaluate to some Boolean representation– Operator symbols used vary somewhat among

languages (!=, /=, ~=, .NE., <>, #)

• JavaScript and PHP have two additional relational operator, === and !==– Similar to their cousins, == and !=, except that they

check both value and type– PHP

“0 == false” “0 === false”

Page 38: CPS 506 Comparative Programming Languages Names, Scope, Expression

Relational and Boolean Expressions

• Boolean Expressions

– Operands are Boolean and the result is Boolean

– Example operators

FORTRAN 77 FORTRAN 90 C Ada

.AND. and && and

.OR. or || or

.NOT. not ! not

xor

Page 39: CPS 506 Comparative Programming Languages Names, Scope, Expression

Relational and Boolean Expressions

• Boolean type in C

– C89 has no Boolean type--it uses int type with 0 for false and nonzero for true

– One odd characteristic of C’s expressions

a < b < c is a legal expression, but the result is not what you might expect

• Left operator is evaluated, producing 0 or 1

• The evaluation result is then compared with the third operand (i.e., c)

Page 40: CPS 506 Comparative Programming Languages Names, Scope, Expression

Short Circuit Evaluation• An expression in which the result is determined without evaluating all

of the operands and/or operators

• Example: (13*a) * (b/13–1)

– If a is zero, there is no need to evaluate (b/13-1)

• Problem with non-short-circuit evaluation

index = 1;

while (index <= length) && (LIST[index] != value)

index++;

– When index=length, LIST [index] will cause an indexing

problem (assuming LIST has length -1 elements)

Page 41: CPS 506 Comparative Programming Languages Names, Scope, Expression

Short Circuit Evaluation (con’t)

• C, C++, and Java

– use short-circuit evaluation for the usual Boolean operators (&& and ||)

• Ada

– programmer can specify either short-circuit is specified with “and then” and “or else”

• Short-circuit evaluation exposes the potential problem of side effects in expressions

e.g. (a > b) || (b++ / 3)

Page 42: CPS 506 Comparative Programming Languages Names, Scope, Expression

Assignment Statements

• The general syntax

<target_var> <assign_operator> <expression>

• The assignment operator

= FORTRAN, BASIC, the C-based languages

:= ALGOLs, Pascal, Ada

= can be bad when it is overloaded for the relational operator for equality (that’s why the C-based languages use == as the relational operator)

Page 43: CPS 506 Comparative Programming Languages Names, Scope, Expression

Assignment Statements (con’t)

• Conditional targets (Perl)

($flag ? $total : $subtotal) = 0

Which is equivalent to

if ($flag){

$total = 0

} else {

$subtotal = 0

}

Page 44: CPS 506 Comparative Programming Languages Names, Scope, Expression

Assignment Statements (con’t)

• Compound Operators– A shorthand method of specifying a commonly

needed form of assignment

– Introduced in ALGOL; adopted by C

– Example

a = a + b

is written as

a += b

Page 45: CPS 506 Comparative Programming Languages Names, Scope, Expression

Assignment Statements (con’t)

• Unary assignment operators – in C-based languages combine

increment and decrement operations with assignment

–Examplessum = ++count

sum = count++

count++

-count++

Page 46: CPS 506 Comparative Programming Languages Names, Scope, Expression

Assignment Statements (con’t)

• Assignment as an Expression– In C, C++, and Java, the assignment statement

produces a result and can be used as operands

– An example:

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

ch = getchar() is carried out; the result(assigned to ch) is used as a conditionalvalue for the while statement

Page 47: CPS 506 Comparative Programming Languages Names, Scope, Expression

Assignment Statements (con’t)

• List Assignment–Perl and Ruby support list

assignments

–Example

($first, $second, $third) = (20, 30, 40);

Page 48: CPS 506 Comparative Programming Languages Names, Scope, Expression

Exercises

1. Indicate the return value of x by calling function f2 using static and dynamic scoping.

int x = 0;int f1() { return (x + 1); }int f2() { int x = 1; return f1(); }

2. Using an example in C++ show the difference between scope and lifetime of a variable (for instance in a function that call another function).

48

Page 49: CPS 506 Comparative Programming Languages Names, Scope, Expression

Exercises

3. What are the results of the following expression in APL and Java?

2 + 3 * 4 – 10 / 5

4. Write a function that includes following sequence of statements in C, C++, C# and Java, run and compare the results.x = 21;int x;x = 42;

49