Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Variables & Binding

Similar presentations


Presentation on theme: "Chapter 4 Variables & Binding"— Presentation transcript:

1 Chapter 4 Variables & Binding
Sung-Dong Kim Dept. of Computer Engineering, Hansung University

2 Abstract Identifier Variable Declaration Binding Assignment Statement
Alias (2012-1) Fundamentals of Programming Languages

3 Abstract Scope Initialization Reference Environment
Memory binding & Variable types (2012-1) Fundamentals of Programming Languages

4 Abstract Basic features of variables  attribute (속성) Address Value
Type Scope Lifetime Type checking Initialization (2012-1) Fundamentals of Programming Languages

5 Abstract Binding variable feature_value_1 feature_value_2 …
feature_value_n (2012-1) Fundamentals of Programming Languages

6 Identifier Character string for identifying program’s elements or objects Consideration Maximum length Character set Case sensitiveness (2012-1) Fundamentals of Programming Languages

7 Variable (1) A location in memory Attributes Store data value Name
Address: l-value Scope Lifetime Value: r-value Type (2012-1) Fundamentals of Programming Languages

8 <D.W.Barron’s variable definition>
Example var sum: integer; sum := 0; 이름: sum 유형: 정수 값: 0 주소(참조) <D.W.Barron’s variable definition> 속성 (attribute) 속성 결정 시간 이름 (name) sum 컴파일 시간 유형 (type) 정수형 주소 (address) ? Loading time 값 (value) 실행 시간 (2012-1) Fundamentals of Programming Languages

9 Variable (3) Other attributes Internal representation for integer
16 bit, 32 bit, … 1’s complement, 2’s complement Maximum/minimum value of integer type variable FORTRAN: -32,768 ~ 32,767 Implementation time, design(definition) time All possible operations for integer type variable Definition(design) time (2012-1) Fundamentals of Programming Languages

10 Declaration (1) Declaration Example: int a[10];
Let compiler know the information about data attributes Example: int a[10]; Array’s name: a Type of array a: integer Dimension: 1 # of elements in array: 10 Index range: 0 ~ 9 (2012-1) Fundamentals of Programming Languages

11 Declaration (2) Role Efficient memory management Static type checking
(2012-1) Fundamentals of Programming Languages

12 Declaration (3) Implicit declaration
FORTRAN: variable name beginning with I ~ N  integer BASIC Integer variable: ends with % Character variable: ends with $ Perl: $: scalar @: array (2012-1) Fundamentals of Programming Languages

13 Binding (1) Binding Binding time Assign attribute value Design time
Implementation time Translation time Execution time (2012-1) Fundamentals of Programming Languages

14 Binding (2) Example: min := min + 5; binding 되는 속성 binding time
변수 min의 유형 (type) 변수 min이 가질 수 있는 값의 범위 변수 min의 주소 값 변수 min의 값 연산자 +의 의미의 집합 연산자 +의 연산 종류 5의 내부적 표현 언어 설계 시간 컴파일 시간 언어 설계시간 or 구현시간 프로그램 적재 시간 언어 실행 시간 언어 구현 시간 (2012-1) Fundamentals of Programming Languages

15 Binding (3) Types Dynamic binding Runtime binding Easy programming
Program’s flexibility Interpreter languages: LISP, Perl, Prolog, … (2012-1) Fundamentals of Programming Languages

16 Binding (4) Static binding Compile time binding
Explicit declaration for variables Determine attributes in compile time Efficient code Compile languages: FORTRAN, Pascal, … (2012-1) Fundamentals of Programming Languages

17 Binding (5) Example of flexibility Perl $price = 1000;
$price = $price + 100; $price = “Very expensive”; (2012-1) Fundamentals of Programming Languages

18 Assignment statement (1)
Change variable’s content (value) Perl: ($a, $b) = ($x, $y); l-value Address r-value Value (2012-1) Fundamentals of Programming Languages

19 Assignment statement (2)
Features All variables have l-value and r-value Constant: only r-value Pointer variable: int *ip l-value: ip’s address r-value: other variable’s address (l-value) A[i] l-value: address of A’s ith element r-value: value (2012-1) Fundamentals of Programming Languages

20 int a = 1, b = 2; a = b; Case 1 a: 1 a: 2 b: 2 b: 2
(2012-1) Fundamentals of Programming Languages

21 Alias (1) Alias More than two names for one variable
More than two names for one memory location (2012-1) Fundamentals of Programming Languages

22 Alias (2) Alias generation Pointer variable in C
EQUIVALENCE, COMMON in FORTRAN Parameters void main() { int *ip, i = 3; ip = &i; *ip = 10; printf(“%d”, i); } (2012-1) Fundamentals of Programming Languages

23 Scope (1) Scope Range of statements where the variables can be accessed Visible: variable can be access (referenced) (2012-1) Fundamentals of Programming Languages

24 Scope (2) Example 1 Area A Area B int a, b float c, d int a char b, c
float d float a, b, c, d; void main() { int a, b; a = b = 12; { char b, c; } Area A Area B (2012-1) Fundamentals of Programming Languages

25 Scope (3) Example 2 main: a, b, Large Large: a, b, x, y, Large, sub1
sub1: a, b, x, y, z, Large, sub1 program main; var a, b : integer; procedure Large; var x, y : integer; procedure sub1; var z : integer; begin { sub1 } end; { Large } { main } end. main Large sub1 (2012-1) Fundamentals of Programming Languages

26 Scope (4) Static scope rule Scope: nesting relation between blocks
Non-local variables: variables declared in nearest nesting block Determine scope at compile time C, C++, Pascal, … (2012-1) Fundamentals of Programming Languages

27 Scope (5) Dynamic scope rule Function call order
Determine scope at runtime Non-local variable: search variables in calling functions APL, SNOBOL4, … (2012-1) Fundamentals of Programming Languages

28 Scope (6) Example Static scope rule Dynamic scope rule print(x): 2
program scope_rule; var x: integer; procedure sub1; begin print(x) end; procedure sub2; x := 4; sub1 x := 2; sub2 end. scope_rule sub1 sub2 (2012-1) Fundamentals of Programming Languages

29 Scope (7) Scope & Lifetime Lifetime
Memory allocation time ~ memory return time Block structure language: scope = lifetime static in C, C++ Scope: static, local to function Lifetime: until the program ends (2012-1) Fundamentals of Programming Languages

30 Scope (8) Example 1 Scope and lifetime of temp: if { }
if (a[j] > a[k]) { int temp; temp = a[j]; a[j] = a[k]; a[k] = temp; } (2012-1) Fundamentals of Programming Languages

31 Scope (9) Example 2 void sub() { static int j = 1; int k = 1;
printf(“j = %d k = %d\n”, j, k); j++; k++; } void main() { sub(); j = 1 k = 1 j = 2 k = 1 j = 3 k = 1 (2012-1) Fundamentals of Programming Languages

32 Initialization Assign value when declaring the variable
Memory binding time = value binding time Pascal, Modula-2: no variable initialization (2012-1) Fundamentals of Programming Languages

33 Referential Environment
All visible (usable) names(data, variables, functions) Static scope language Variables in local area + visible variables in parent area Dynamic scope language Variables in local area + variables in other currently activating subprograms (2012-1) Fundamentals of Programming Languages

34 sub1 sub2 sub3 program example; var a, b : integer; … procedure sub1;
var x, y : integer; begin { sub1 } end; { sub1 } procedure sub2; var x : integer; procedure sub3; begin { sub3 } end; { sub3} begin { sub2 } end; { sub2 } begin { example } end. { example } example sub1 sub2 sub3 1 1: sub1의 x, y, example의 a, b 2: sub3의 x, example의 a, b 3: sub2의 x, example의 a, b 4: example의 a, b 2 3 4 (2012-1) Fundamentals of Programming Languages

35 void sub1() { int a, b; … } void sub2() { 1 int b, c; sub1();
void main() { int c, d; sub2(); 1 2 3 1: sub1의 a, b, sub2의 c, main의 d 2: sub2의 b, c, main의 d 3: main의 c, d (2012-1) Fundamentals of Programming Languages

36 Memory binding & Variable Types (1)
Static variables Binding occurs before execution Binding remains until program termination Global variables Efficiency Disadvantages Low flexibility  no recursion Variables can’t share memory location C, C++, Java: static (2012-1) Fundamentals of Programming Languages

37 Memory binding & Variable Types (2)
Stack-dynamic variables Binding on the declaration statement Type: static binding Runtime stack Recursive program Subprogram requires its own memory for its local variables Less efficient: memory allocation/free C, C++: local variables (2012-1) Fundamentals of Programming Languages

38 Memory binding & Variable Types (3)
Explicit heap-dynamic variables Explicit runtime instruction Heap Access by only pointer, reference variables C: malloc(), free() function C++: new, delete operator (2012-1) Fundamentals of Programming Languages


Download ppt "Chapter 4 Variables & Binding"

Similar presentations


Ads by Google