Chapter 4 Variables & Binding

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.
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
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?
Chapter 9: Subprogram Control
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?
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.
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
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.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
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.
CHAPTER 8. MEMORY MANAGEMENT SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
CHAPTER 4 VARIABLES & BINDING 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
Names, Bindings, Type Checking, and Scopes
Implementing Subprograms
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Chapter 8. Memory Management
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Structure of Programming Languages
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Implementing Subprograms
Names, Bindings, Type Checking, and Scopes
Scope, Visibility, and Lifetime
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
Types and Related Issues
Implementing Subprograms
Presentation transcript:

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

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

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

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

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

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

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

<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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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