CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.

Slides:



Advertisements
Similar presentations
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Advertisements

Programming Languages and Paradigms
Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Chapter 9: Subprogram Control
CSC321: Programming Languages Names Chapter 4: Names 4.1 Syntactic Issues 4.2 Variables 4.3 Scope 4.4 Symbol Table 4.5 Resolving References 4.6 Dynamic.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 5 Names, Bindings, and Scopes.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
ISBN Chapter 10 Implementing Subprograms.
CHAPTER 8. MEMORY MANAGEMENT SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Data Types In Text: Chapter 6.
Chapter 4 Variables & Binding
Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
Presentation transcript:

CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY

ABSTRACT  Identifier  Variable  Declaration  Binding  Assignment Statement  Alias (2013-1) Understanding of Programming Languages 2

ABSTRACT  Scope  Initialization  Reference Environment  Memory binding & Variable types (2013-1) Understanding of Programming Languages 3

ABSTRACT  Basic features of variables  attribute ( 속성 ) Address Value Type Scope Lifetime Type checking Initialization (2013-1) Understanding of Programming Languages 4

ABSTRACT  Binding (2013-1) Understanding of Programming Languages 5 variable feature_value_1 feature_value_2 … feature_value_n

IDENTIFIER  Character string for identifying program’s elements or objects  Consideration Maximum length Character set Case sensitiveness (2013-1) Understanding of Programming Languages 6

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

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

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

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

DECLARATION (2)  Role Efficient memory management Static type checking (2013-1) Understanding of Programming Languages 11

DECLARATION (3)  Implicit declaration FORTRAN: variable name beginning with I ~ N  integer BASIC Integer variable: ends with % Character variable: ends with $ Perl: $: array (2013-1) Understanding of Programming Languages 12

BINDING (1)  Binding Assign attribute value  Binding time Design time Implementation time Translation time Execution time (2013-1) Understanding of Programming Languages 13

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

BINDING (3)  Types Dynamic binding Runtime binding Easy programming Program’s flexibility Interpreter languages: LISP, Perl, Prolog, … (2013-1) Understanding of Programming Languages 15

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

BINDING (5)  Example of flexibility Perl (2013-1) Understanding of Programming Languages 17 $price = 1000; $price = $price + 100; $price = “Very expensive”;

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

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

(2013-1) Understanding of Programming Languages 20 int a = 1, b = 2; a = b; Case 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

(2013-1) Understanding of Programming Languages a: b: p1: p2: 1 2 :a :b p1: p2: 1 2 :a :b

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

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); }

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

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

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

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

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

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

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

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; }

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

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

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

(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 sub : sub1 의 x, y, example 의 a, b 2: sub3 의 x, example 의 a, b 3: sub2 의 x, example 의 a, b 4: example 의 a, b

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

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

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

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