Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

:: INT304 :: Programming Syntax & Semantics

:: INT304 :: Programming Syntax & Semantics

The Concept of Binding

(1)

The Concept of Binding

(2)

Definition: A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Definition: Binding time is the time at which a binding takes place.
Possible binding times: - Language design time e.g., bind operator symbols to operations - Language implementation time e.g., bind fl. pt. type to a representation - Compile time e.g., bind a variable to a type in C or Java - Load time e.g., bind a FORTRAN 77 variable to a memory cell (or a C static variable) - Runtime e.g., bind a non-static local variable to a memory cell

Definition: A binding is static if it first occurs before run time and remains unchanged throughout program execution. Def: A binding is dynamic if it first occurs during execution or can change during execution of the program.

Copyright Khaitong-2-5-0-9. 2003

Page: 1 of 12

Copyright Khaitong-2-5-0-9. 2003

Page: 2 of 12

:: INT304 :: Programming Syntax & Semantics

:: INT304 :: Programming Syntax & Semantics

Type Binding
How is a type specific? When does the binding take place?

(1)

Type Binding
Specified through an assignment statement e.g. JavaScript
o list = [2, 4.33, 6, 8] ; o list = 17.3 ;

(2)

Dynamic Type Binding (VB, JavaScript, PHP)

If static, the type may be specified by either an explicit or an implicit declaration Def: An explicit declaration is a program statement used for declaring the types of variables Def: An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program) FORTRAN, PL/I, BASIC, and Perl provide implicit declarations
FORTRAN77
Sum = 0 READ *, ListLen DO 10 ICOUNT = 0, ListLen READ *, InList(icount) SUM= Sum + InList(iCount) Continue ICount = 29 Implicit Declaration (: )

Advantage:
o flexibility (generic program units)

Disadvantages:
o High cost (dynamic type checking and interpretation) o Type error detection by the compiler is difficult
VB: Name = Peter FulltTmeSalary = 12590 PartTimeSalary = 25840 FulltTmeSalary = Name IF FullTime < PartTime Then Static type binding: Compile Time Error Dynamic type binding: Run-time Error (Type Checking be done at runtime)

10

C Language
double sum = 0 ; int list_len, icount ; scanf(%d, &list_len); for icount = 0,i<list_len;i++) { scanf(%d, inlist(icount); sum = sum + inlist(icount); } sum = 125,000.00 ;

Explicit Declaration

Copyright Khaitong-2-5-0-9. 2003

Page: 3 of 12

Copyright Khaitong-2-5-0-9. 2003

Page: 4 of 12

:: INT304 :: Programming Syntax & Semantics

:: INT304 :: Programming Syntax & Semantics

Storage Binding & Life time


Allocation: getting a cell from some pool of available cells

(1)

Storage Binding & Life time


Stack-Dynamic Storage bindings are create for variables when their declaration statements are elaborated. Explicit Heap-Dynamic

(2)

De-allocation: putting a cell back into the pool Def: The lifetime of a variable is the time during which it is bound to a particular memory cell Static Dynamic
o Stack-Dynamic o Explicit Heap-Dynamic o Implicit Heap-Dynamic
Static: - bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. e.g. all FORTRAN 77 variables, C static variables Advantages: - efficiency (direct addressing), history-sensitive subprogram support Disadvantage: - Lack of flexibility (no recursion)

Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution Implicit Heap-Dynamic Allocation and deallocation caused by assignment statements
Stack-Dynamic: - All attributes except address are statically bound e.g. local variables in C subprograms and Java methods Advantage: allows recursion; conserves storage Disadvantages: Overhead of allocation and deallocation Subprograms cannot be history sensitive Explicit Heap Dynamic: - Referenced only through pointers or references e.g. dynamic objects in C++ (via new and delete), all objects in Java Advantage: provides for dynamic storage management Disadvantage: inefficient and unreliable Implicit Heap Dynamic: - All strings and arrays in Perl and JavaScript Advantage: flexibility Disadvantages: Inefficient, because all attributes are dynamic Loss of error detection

Copyright Khaitong-2-5-0-9. 2003

Page: 5 of 12

Copyright Khaitong-2-5-0-9. 2003

Page: 6 of 12

:: INT304 :: Programming Syntax & Semantics

:: INT304 :: Programming Syntax & Semantics

Arrays
Arrays are objects in Java. They are bounds checked and provide access to length variable arr Primitive array int[] arr = new int[5]; for (int i=0; i<arr.length; i++) arr[i] = i * i ; //value Object array Date[] od = new Date[6]; for (int i=0; i<od.length; i++) od[i] = new Date(); //ref od length 6 length 5

Multidimensional Array:
Declaration & Instantiate
int [][] table ; table = new int[4][5] table[1][3] = 9; table[3][0] = 1;

(1)
length
0 0 5 0 0 0

table length 4
0 1 2 3 4
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 1 4 9 16

length
0 0

5 0 5 0 0 0

0 1 2 3

length
1
0

9
0

Copyright Khaitong-2-5-0-9. 2003

Page: 7 of 12

Copyright Khaitong-2-5-0-9. 2003

Page: 8 of 12

:: INT304 :: Programming Syntax & Semantics

:: INT304 :: Programming Syntax & Semantics

Multidimensional Array:
Declaration & Instantiate
Student []tb[] ; tb = new Student[4]; tb[0] tb[1] tb[2] tb[3] = = = = new new new new Student[3] Student[5] Student[6] Student[4]

(1)
length
3

Array Initialization:
Initializer List table length 4
0 1 2 3 4
1 5 9 0 2 6 9 0 3 7 9 0 4 8 9 0 0 0 9 0

(1)
length
1 2 5 3 4 0

tb length 4

int [][] table = length


6

length

{ {1,2,3,4,0}, {5,6,7,8,0}, {9,9,9,9,9}, {0,0,0,0,0} }

length
9 9

5 9 5 0 0 0 9 9

0 1 2 3

0 1 2 3 4 5

0 1 2 3
0

length
0 0

tb[2][5] = new Student()

id name

Summary: - Array Object - Array - Array () ( ) - index () 0 (length-1) Exception: ArrayIndexOutOfBoundsException - Array - Array attribute length Array ( String method length() ) : - / Array - ( ) - Array - Selection sort (/) - Insertion sort ( ) - Array - Linear (sequential) search () - Binary Search ()

Copyright Khaitong-2-5-0-9. 2003

Page: 9 of 12

Copyright Khaitong-2-5-0-9. 2003

Page: 10 of 12

:: INT304 :: Programming Syntax & Semantics

:: INT304 :: Programming Syntax & Semantics

Assignments:
Matrix ADT: Attributes: table: 2 Dimension Array row, column Constructors: Matrix() Matrix(int row, int column) Matrix(double[] ritem, double[] citem) Matrix(double[][] item) Matrix(Matrix m)

(1)

Java API
Arrays import java.util.*;
static int binarySearch(int[] a, int key) static int binarySearch(double[] a, double key) static int binarySearch(Object[] a, Object key) static boolean equals(int[] a, int[] b) static boolean equals(double[] a, double[] b) static boolean equals(Object[] a, Object[] b)

(1)

Arrays.binarySearch()

Arrays.equals()

static void equals(int[] a, int val) static void equals(int[] a, int from, int to,int val) static void equals(Object[] a, Object val) static void sort(int[] a) static void sort(int[] a, int from, int to) static void sort(Object[] a) Example: String name[] = { new String(Somchai), new String(Anuchart), new String(Khunjaew), new String(The matrix) }; int pos = Arrays.binarySearch(name, Khunjaew); Arrays.sort(name); System.out.println(name[0]); Arrays.binarySearch(name, 1, 2, Unknown);

Arrays.fill()

Arrays.sort()

Methods: setMatrix(int rpos, int cpos, double val) ; setMatrix(double []ritem, double [] citem) ; setMatrix(double [][] item) ; matrixCopy(Matrix m) ; add (Matrix m) ; substract (Matrix m) ; multiply (Matrix m) ; equals(Matrix m);

System.arraycopy()

Copyright Khaitong-2-5-0-9. 2003

Page: 11 of 12

Copyright Khaitong-2-5-0-9. 2003

Page: 12 of 12

You might also like