Title: Programming Languages
1ProgrammingLanguages
CSCI-4430 CSCI-6969February 22, 2008
- David Goldschmidt, Ph.D.
- Computer Science
- The College of Saint Rose
2Names
- A name is a string of characters that
identifiessome entity in a program - Variables
- Labels
- Subprograms
- Functions, methods, ....
- Classes, structures, ....
- etc.
double f(double q) return q q main()
double x, y here scanf("lf", x) if (x lt
0.0) goto here y f(x) PI
printf("lf\n", y)
3Design Issues for Names
- Acceptable characters?
- Start with A-Za-z, then A-Za-z0-9
- Allow underscore (and other) characters?
- Allow (and ignore) spaces in names?
- Are names case-sensitive?
e.g. Radius Of Circle RadiusOfCircle
e.g. LENGTH Length length
4Design Issues for Names
- Are special words reserved words or keywords?
- A reserved word is a special word of a
programming language that cannot be used as a
name - A keyword is special only in certain contexts
e.g. int x 5 int do 10
e.g. Integer X Integer 10 X 15
Integer
5Name Lengths
- Examples of maximum length constraints
- BASIC maximum of 2 characters (e.g. X, X1, PI)
- FORTRAN I maximum of 6 characters (e.g. RATE)
- FORTRAN 90 and ANSI C maximum of 31 characters
- COBOL maximum of 30 characters
- C no limit, but implementers often impose one
- Ada and Java no limit and all are significant
6Variables
- A variable abstracts a collection of memory cells
- Based on the von Neumann architecture
- Variables are characterized by
- Name (not always)
- Memory address
- Data type
- Value
- Lifetime
- Scope
7Binding
- A binding is a semantic association in a program
- Data type of count is bound to int at compile
time - Memory address of count is bound at run time
- The meaning of the assignment () and addition
() operators are bound at compile time
int count 15 count count 5
8Binding Time
- Binding time is the time at which a binding
occurs - Language design time
- bind specific symbols (e.g. , , ltgt, , !)
tospecific semantically defined operations - Language implementation time
- e.g. bind floating-point data type toa specific
representation format
9Binding Time
- Binding time is the time at which a binding
occurs - Compile time
- e.g. bind a variable to a data type in C/C/Java
- Load time
- e.g. bind a FORTRAN 77 or a static variable
in C/C/Java to a memory location - Execution or run time
- e.g. bind a non-static (dynamic) local variable
to a memory location
10Static Binding
- A binding is static if it occurs before run time
and remains unchanged throughout program
execution - Defining constants
- Binding variables to theirrespective data types
const int Q 15 // C/C const double PI
3.1415927
public static final int JACK 11 //
Java public static final int QUEEN 12 public
static final int KING 13
int count, x, y, z // C/C char letter double
radius, area, volume
11Static Binding
- A binding is static if it occurs before run time
and remains unchanged throughout program
execution - Static code (e.g. Java)
public static void main(String args)
System.out.println("Do something.") int y
calculate(10) public static int calculate(int
x) return x x x
12Dynamic Binding
- A binding is dynamic if it occurs during run time
or can change throughout program execution - Variable binding toa memory address
- (can be static, too)
- Binding a value toa variable
- Dynamic memory allocation
char c, d, e // C/C int count, x, y,
z double radius, area, volume
char c 'Q' // C/C count 5 radius
47.556
char cptr // C/C cptr (char
)malloc(SIZE)
13Dynamic Binding
- A binding is dynamic if it occurs during run time
or can change throughout program execution - Object creation
public class Fraction // Java public
static final int ZERO 0 public static final
int ONE 1 private double numerator
private double denominator public double
toDecimal() return numerator /
denominator
14Explicit Declaration
- An explicit declaration is a program statement
that declares the data type of a variable
/ C/C/Java / int count char letter double
radius double area double circumference
15Implicit Declaration
- An implicit declaration is a mechanism for
specifying data types of variables based on their
first appearance - e.g. BASIC
- e.g. Perl
10 LET X 10 15 LET PI 3.1415 20 LET Y "HI"
numDays 29 scalars month
"February" _at_list ("abc", "def")
arrays _at_q1days (31, 29, 31) q1mapping (
hashes "January" gt 31,
"February" gt 29, "March" gt 31 )
16Implicit Declaration
- An implicit declaration is a mechanism for
specifying data types of variables based on their
first appearance - e.g. JavaScript
x 47 // numbers r 14.45 PI
3.1415927 y 'Welcome' // strings z
"Welcome" name "Shirley Ann" done true
// boolean valid false enough (x gt 35)
17Lifetimes of Variables
- The lifetime of a variable is the time during
whichit is bound to a particular memory location - Variables begin their lifetimeupon declaration
- Variables end their lifetimewhen they go out of
scope - or when the program endsexecution....
/ C/C/Java / int count 100 while (count
gt 0) int xyz / ... / count--
18Lifetimes of Variables
- Memory is allocated from a pool of available
memorywhen the variable is first bound - Memory is deallocated whena variable ends its
lifetime - The allocated memory isgiven back to the pool
- Categories of Variables by Lifetime
- Static
- Stack-Dynamic
- Explicit Heap-Dynamic
- Implicit Heap-Dynamic
19Static Variables
- Static variables are bound to memory locations
before run time - And remain bound to thesame memory
locationthroughout execution - e.g. FORTRAN 77
- e.g. C static variables
- Sneaky global variables?
20Stack-Dynamic Variables
- Stack-dynamic variables are variables whose
storage bindings are created during run time when
their declaration statements are elaborated - e.g. variables defined in methods in Java,
C, C
main() / C / int x ... while (f gt
0) int q 0 ...
21Explicit Heap-Dynamic Variables
- Explicit heap-dynamic variables are nameless
variables whose storage bindings are allocated
(and deallocated) during run time at the
discretion of the programmer - Use pointers and references variables to access
- e.g. dynamic objects in C, objects in
Java, use of malloc() in C
String name // Java name new
String("Hi.") name null // optional
int x // C x new int delete x //
avoid memory leaks!
int x / C / x (int )malloc(sizeof(int
))
22Implicit Heap-Dynamic Variables
- Implicit heap-dynamic variables are variables
whose storage bindings are allocated (and
deallocated)only when they are assigned values - Names that adapt tohowever they are used
- e.g. variables in APL, strings and arrays
in Perl and JavaScript (and hashes in
Perl)
// JavaScript numbers 5, 7, 9, 11 numbers
"Hello." numbers 100
Perl q2mapping ( "April" gt 30,
"May" gt 31, "June" gt 30
) q2mapping ( "q2" gt 4000 )
23Type Checking
- Type checking ensures that the operands of an
operator are of compatible types - Types are compatible when
- it is legal to use the type with the operator
- or the type is allowed under implicit conversion
rules of the language (a process called
coercion) - Type errors occur when an operator is applied to
an operand of an inappropriate type
24Type Checking
- Type checking occurs with variable binding
- If type bindings are static, nearly all type
checkingcan be static (i.e. at compile time) - If type bindings are dynamic, type checking
mustbe dynamic (i.e. at run time) - A programming language is strongly typed iftype
errors are always detected - Detection occurs at both compile time and run
time - Impacts the reliability of a language
25Scope
- Variable scope defines the range of
statementsover which a variable is visible - Local variables of a program unit X are variables
thatare declared within X and therefore visible
within X - Non-local variables of a program unit X are
variablesthat are visible but not declared
within X
26Static Scope
- Static scope indicates that the scope of a
variablecan be determined before execution - How?
- Search local declarations first,then search in
increasinglylarger enclosing scopes, untila
valid declaration is found - Variable x becomes hidden
- Not valid in Java or C
main() / C / int x 5
printf("d\n", x) int x 10 int y
20 printf("d\n", x) int x
15 int z 20 printf("d\n", x)
27Static Scope
- Static scope indicates that the scope of a
variablecan be determined before execution - JavaScript and PHP do not support nested static
scopes
var abc false if ( s "HELLO" ) abc
true var abc 500 document.write("abc is "
abc) document.write("abc is still " abc)
28Dynamic Scoping
- Dynamic scoping defines a variables scope
basedon the calling sequences of subprograms - Used in APL, SNOBOL4, early versions of LISP
Call Main, which calls Sub1 Call Main, which
calls Sub2, which then calls Sub1
procedure Main is X Integer procedure Sub1
is begin -- of Sub1 ... X ... end
-- of Sub1 procedure Sub2 is X Integer
begin -- of Sub2 ... end -- of
Sub2 begin -- of Main ... end -- of Main
29HW 2/22
- Consider a typeless language (e.g. Smalltalk,
Ruby) - What are the advantages and disadvantages of
having no types in a programming language? - Write test programs in C, Java, C, Perl, and
JavaScript to determine the scope of a variable
declared as part of the initializer of a for
statement - if the languageallows it
for ( int i 0 i lt 100 i ) //
... // Is variable i visible down here?
30HW 2/22
- Given this Ada code
- Assume execution ofthis code is Main calls
Sub1 Sub1 calls Sub2 Sub2 calls Sub3 - Using static scoping,which X is referencedin
Sub1? In Sub2?In Sub3?
procedure Main is X Integer procedure
Sub3 -- allows Sub1 to --
to call Sub3 procedure Sub1 is X
Integer procedure Sub2 is begin -- of
Sub2 ... end -- of Sub2 begin
-- of Sub1 ... end -- of Sub1
procedure Sub3 is begin -- of Sub3 ...
end -- of Sub3 begin -- of Main ...
end -- of Main
repeat usingdynamic scoping
31HW 2/22
- Given this Ada code
- Assume programexecution usingstatic-scoping
rules - What is the outputof this program?
- What is the outputusing dynamic-scopingrules?
procedure Main is X Integer procedure SubA
is X Integer begin -- of SubA
Put(X) -- i.e. output X end -- of SubA
procedure SubB is X Integer begin --
of SubB X 10 SubA end -- of
SubB begin -- of Main X 5 SubB end
-- of Main
32HW 2/22
- Given this Ada code
- Assume program executionusing static-scoping
rules - For Sub1, Sub2, and Sub3,list the visible
variables andthe program units within whichthey
are declared
procedure Main is X, Y, Z Integer
procedure Sub1 is A, Y, Z Integer
procedure Sub2 is A, B, Z Integer
begin -- of Sub2 ... end -- of
Sub2 begin -- of Sub1 ... end --
of Sub1 procedure Sub3 is A, X, W
Integer begin -- of Sub3 ... end
-- of Sub3 begin -- of Main ... end --
of Main
33HW 2/22
- Given this C code
- For each of the markedpoints A, B, C, and D,
list each visible variableand the number of
thedeclaration statement
void doSomething() int a, b, c / 1 /
... while ( ... ) int b, c, d / 2
/ ... Point A while ( ... )
int c, d, e / 3 / ... Point
B ... Point C ... Point
D
34HW 2/22
- Given this C-like code
- Assume dynamic-scoping rules
- Given the following call sequences,what
variables are visible duringexecution of the
last function called ? - Also specify the name of the functionin which
each variable was defined - main() calls f1(), which calls f3()
- main() calls f2(), which calls f3(),
which calls f1() - main() calls f3(), which calls f1()
void main() int a, b, c ... void f1()
int b, c, d ... void f2() int c, d,
e ... void f3() int d, e, f ...
35Reading Assignments
- Read for this week
- Chapters 5 and 6