Download presentation
Presentation is loading. Please wait.
Published byDerick Stone Modified over 8 years ago
1
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY
2
ABSTRACT Identifier Variable Declaration Binding Assignment Statement Alias (2013-1) Understanding of Programming Languages 2
3
ABSTRACT Scope Initialization Reference Environment Memory binding & Variable types (2013-1) Understanding of Programming Languages 3
4
ABSTRACT Basic features of variables attribute ( 속성 ) Address Value Type Scope Lifetime Type checking Initialization (2013-1) Understanding of Programming Languages 4
5
ABSTRACT Binding (2013-1) Understanding of Programming Languages 5 variable feature_value_1 feature_value_2 … feature_value_n
6
IDENTIFIER Character string for identifying program’s elements or objects Consideration Maximum length Character set Case sensitiveness (2013-1) Understanding of Programming Languages 6
7
VARIABLE (1) A location in memory Store data value Attributes Name Address: l-value Scope Lifetime Value: r-value Type (2013-1) Understanding of Programming Languages 7
8
VARIABLE (2) Example (2013-1) Understanding of Programming Languages 8 var sum: integer; sum := 0; 이름 : sum 유형 : 정수 값 : 0 주소 ( 참조 ) 속성 (attribute) 값속성 결정 시간 이름 (name) sum 컴파일 시간 유형 (type) 정수형컴파일 시간 주소 (address) ?Loading time 값 (value) 0 실행 시간 Binding Binding time
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 (2013-1) Understanding of Programming Languages 9
10
DECLARATION (1) Declaration 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 (2013-1) Understanding of Programming Languages 10
11
DECLARATION (2) Role Efficient memory management Static type checking (2013-1) Understanding of Programming Languages 11
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 (2013-1) Understanding of Programming Languages 12
13
BINDING (1) Binding Assign attribute value Binding time Design time Implementation time Translation time Execution time (2013-1) Understanding of Programming Languages 13
14
BINDING (2) Example: min := min + 5; (2013-1) Understanding of Programming Languages 14 binding 되는 속성 binding time 변수 min 의 가능한 변수 유형 변수 min 의 유형 (type) 변수 min 이 가질 수 있는 값의 범위 변수 min 의 주소 값 변수 min 의 값 연산자 + 의 의미의 집합 연산자 + 의 연산 종류 5 의 내부적 표현 언어 설계 시간 컴파일 시간 언어 설계시간 or 구현시간 프로그램 적재 시간 언어 실행 시간 언어 설계 시간 컴파일 시간 언어 구현 시간
15
BINDING (3) Types Dynamic binding Runtime binding Easy programming Program’s flexibility Interpreter languages: LISP, Perl, Prolog, … (2013-1) Understanding of Programming Languages 15
16
BINDING (4) Static binding Compile time binding Explicit declaration for variables Determine attributes in compile time Efficient code Compile languages: FORTRAN, Pascal, … (2013-1) Understanding of Programming Languages 16
17
BINDING (5) Example of flexibility Perl (2013-1) Understanding of Programming Languages 17 $price = 1000; $price = $price + 100; $price = “Very expensive”;
18
ASSIGNMENT STATEMENT (1) Assignment statement Change variable’s content (value) Perl: ($a, $b) = ($x, $y); l-value Address r-value Value (2013-1) Understanding of Programming Languages 18
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 (2013-1) Understanding of Programming Languages 19
20
(2013-1) Understanding of Programming Languages 20 int a = 1, b = 2; a = b; Case 1 1 2 a: b: 2 2 a: b: int *a = 1, *b = 2; a = b; Case 2 a: b: 1 2 a: b: 2 int *a = 1, *b = 2; *a = *b; Case 3 a: b: 1 2 a: b: 2 2
21
(2013-1) Understanding of Programming Languages 21 1 2 a: b: p1: p2: 1 2 :a :b p1: p2: 1 2 :a :b
22
ALIAS (1) Alias More than two names for one variable More than two names for one memory location (2013-1) Understanding of Programming Languages 22
23
ALIAS (2) Alias generation Pointer variable in C EQUIVALENCE, COMMON in FORTRAN Parameters (2013-1) Understanding of Programming Languages 23 void main() { int *ip, i = 3; ip = &i; *ip = 10; printf(“%d”, i); }
24
SCOPE (1) Scope Range of statements where the variables can be accessed Visible: variable can be access (referenced) (2013-1) Understanding of Programming Languages 24
25
SCOPE (2) Example 1 Area A int a, b float c, d Area B int a char b, c float d (2013-1) Understanding of Programming Languages 25 float a, b, c, d; void main() { int a, b; a = b = 12; … { char b, c; … } … } Area A Area B Area A
26
SCOPE (3) Example 2 main: a, b, Large Large: a, b, x, y, Large, sub1 sub1: a, b, x, y, z, Large, sub1 (2013-1) Understanding of Programming Languages 26 program main; var a, b : integer; procedure Large; var x, y : integer; procedure sub1; var z : integer; begin { sub1 } end; begin { Large } end; begin { main } end. main Large sub1
27
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, … (2013-1) Understanding of Programming Languages 27
28
SCOPE (5) Dynamic scope rule Function call order Determine scope at runtime Non-local variable: search variables in calling functions APL, SNOBOL4, … (2013-1) Understanding of Programming Languages 28
29
SCOPE (6) Example Static scope rule print(x): 2 Dynamic scope rule print(x): 4 (2013-1) Understanding of Programming Languages 29 program scope_rule; var x: integer; procedure sub1; begin print(x) end; procedure sub2; var x: integer; begin x := 4; sub1 end; begin x := 2; sub2 end. scope_rule sub1 sub2
30
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 (2013-1) Understanding of Programming Languages 30
31
SCOPE (8) Example 1 Scope and lifetime of temp: if { } (2013-1) Understanding of Programming Languages 31 if (a[j] > a[k]) { int temp; temp = a[j]; a[j] = a[k]; a[k] = temp; }
32
SCOPE (9) Example 2 (2013-1) Understanding of Programming Languages 32 void sub() { static int j = 1; int k = 1; printf(“j = %d k = %d\n”, j, k); j++; k++; } void main() { sub(); } j = 1k = 1 j = 2k = 1 j = 3 k = 1
33
INITIALIZATION Assign value when declaring the variable Memory binding time = value binding time Pascal, Modula-2: no variable initialization (2013-1) Understanding of Programming Languages 33
34
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 (2013-1) Understanding of Programming Languages 34
35
(2013-1) Understanding of Programming Languages 35 program example; var a, b : integer; … procedure sub1; var x, y : integer; begin { sub1 } … end; { sub1 } procedure sub2; var x : integer; … procedure sub3; var x : integer; begin { sub3 } … end; { sub3} begin { sub2 } … end; { sub2 } begin { example } … end. { example } example sub1 sub2 sub3 1 2 3 4 1: sub1 의 x, y, example 의 a, b 2: sub3 의 x, example 의 a, b 3: sub2 의 x, example 의 a, b 4: example 의 a, b
36
(2013-1) Understanding of Programming Languages 36 void sub1() { int a, b; … } void sub2() { 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
37
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 (2013-1) Understanding of Programming Languages 37
38
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 (2013-1) Understanding of Programming Languages 38
39
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 (2013-1) Understanding of Programming Languages 39
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.