CSP 506 Comparative Programming Languages

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
2
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";
3
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’);
4
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)
5
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)
6
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 userdefined name
• Potential problem with reserved words: If there are too many,
many collisions occur (e.g., COBOL has 300 reserved words!)
7
Variables
• Six attributes
– Name
– Address
(l-value)
– Type
– Value
(r-value)
– Scope
– Lifetime
8
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
9
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
10
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
11
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" variables
E.g. unit.name
12
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
13
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…
14
ML Structures
structure Fred = struct
val a = 1;
fun f x = x + a;
end;
• 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
15
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 objectoriented languages
16
Example
public class Month {
public static int min = 1;
public static int max = 12;
…
}
• The variables min and max would be
visible within the rest of the class
• Also accessible from outside, as
Month.min and Month.max
17
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
18
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
19
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
20
Dynamic Scope
• 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
21
Scope Example
Big
- declaration of X
Sub1
- declaration of X ...
call Sub2
...
Sub2
...
- reference to X ...
Big calls Sub1
Sub1 calls Sub2
Sub2 uses X
...
call Sub1
…
22
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
23
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.
24
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
25
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
26
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?
27
Expression (con’t)
• Operators
– A unary operator has one operand
b = !a;
– A binary operator has two operands
c = a + b;
– A ternary operator has three
operands
int absValue = (a < 0) ? -a : a;
28
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)
• *, /
• +, 29
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
30
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
31
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
32
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
33
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);
34
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-toright order
35
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
36
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”
37
Relational and Boolean Expressions
• Boolean Expressions
– Operands are Boolean and the result is Boolean
– Example operators
FORTRAN 77
.AND.
.OR.
.NOT.
FORTRAN 90
and
or
not
C
Ada
&&
||
!
and
or
not
xor
38
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)
39
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)
40
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)
41
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)
42
Assignment Statements (con’t)
• Conditional targets (Perl)
($flag ? $total : $subtotal) = 0
Which is equivalent to
if ($flag){
$total = 0
} else {
$subtotal = 0
}
43
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
44
Assignment Statements (con’t)
• Unary assignment operators
– in C-based languages combine
increment and decrement operations
with assignment
– Examples
sum = ++count
sum = count++
count++
-count++
45
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 conditional
value for the while statement
46
Assignment Statements (con’t)
• List Assignment
– Perl and Ruby support list assignments
– Example
($first, $second, $third) = (20, 30, 40);
47
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
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