SlideShare uma empresa Scribd logo
1 de 47
Baixar para ler offline
Chapter 5: Names, Bindings, and Scopes 
Principles of Programming Languages
Contents 
•Names 
•Variables 
•The Concept of Binding 
•Scope and Lifetime 
•Referencing Environments 
•Named Constants
Introduction 
•Imperative languages are abstractions of von Neumann architecture 
–Memory  variables 
•Variables characterized by attributes, among of them is type 
•Scope and lifetime of variables are important issues when designing type
Variable Attributes 
•Name 
•Address 
•Value 
•Type 
•Lifetime 
•Scope
Names (Identifiers) 
•Design issues for names: 
–Are names case sensitive? 
–Are special words reserved words or keywords?
Name Forms 
•Length 
–If too short, they cannot be connotative 
–Language examples: 
•FORTRAN I: maximum 6 
•FORTRAN 95: maximum 31 
•C89: unlimited but first 31 is significant 
•Ada, Java, C#: no limit, and all are significant 
•C++: no limit, but implementers often impose one
Name Forms 
•Most of languages: a letter followed by s string consisting of letters, digits, or underscore 
•Special forms: 
–PHP: variables begin with $ 
–Perl: special beginning characters denote type $, @, % 
–Ruby: @ instance variable, @@ class variable
Name Forms 
•Case sensitivity 
–Many languages, including C-based languages, are case sensitive 
–Disadvantage: 
•readability (names that look alike are different) 
•writability: C++ and Java’s predefined names are mixed case (e.g. IndexOutOfBoundsException)
Special Words 
•Special words 
–An aid to readability; used to delimit or separate statement clauses 
–A keyword is a word that is special only in certain contexts, e.g., in Fortran 
– A reserved word is a special word that cannot be used as a user-defined name 
–Which one is better? …, but…
Variables 
•A variable is an abstraction of a memory cell 
•Variables can be characterized as 6 attributes: 
–Name 
–Address 
–Type 
–Value 
–Lifetime 
–Scope
Variables Attributes 
•Name - not all variables have them 
•Address - the memory address with which it is associated 
–A variable may have different addresses at different times during execution 
–l-value 
–Two variable names can be used to access the same memory: aliases 
•created via pointers, reference variables, subprogram parameters 
•harmful to readability
Variables Attributes 
•Type - determines the range of values of variables and the set of operations that are defined for values of that type 
•Value - the contents of the memory cell(s) associated with the variable 
–Memory cell here is abstract cell, not physical cell 
–r-value
The Concept of Binding 
•A binding is an association 
–an attribute and an entity 
–an operation and a symbol 
•Binding time is the time at which a binding takes place.
Possible Binding Times 
•Language design time -- bind operator symbols to operations 
•Language implementation time -- data type is bound to range of possible values 
•Compile time -- bind a variable to a data type in Java 
•Load time -- bind a variable to a memory cell for C static variable 
•Link time -- call to library subprogram to its code 
•Runtime -- bind a non-static local variable to a memory cell
Example 
•Consider the C assignment statement 
count = count + 5; 
–Type of count 
–Set of possible values of count 
–Meaning of + 
–Internal representation of 5 
–Value of count
Static and Dynamic Binding 
•A binding is static if it first occurs before run time and remains unchanged throughout program execution. 
•A binding is dynamic if it first occurs during execution or can change during execution of the program
Binding 
•Let’s discuss two types of binding: 
–Type binding: variable to data type 
–Storage binding: variable to its address
Type Binding 
•How is a type specified? 
•When does the binding take place? 
•If static, the type may be specified by either an explicit or an implicit declaration
Explicit/Implicit Declaration 
•An explicit declaration is a program statement used for declaring the types of variables 
•An implicit declaration is a default mechanism for specifying types of variables 
–Ex. 
•FORTRAN: I-N: Integer, others real 
•Perl: $a: scalar, @a: array 
–Advantage: writability 
–Disadvantage: reliability (less trouble with Perl)
Dynamic Type Binding 
•JavaScript and PHP 
•Specified through an assignment statement e.g., JavaScript 
list = [2, 4.33, 6, 8]; 
list = 17.3; 
–Advantage: flexibility (generic program units) 
–Disadvantages: 
•High cost (dynamic type checking and interpretation) 
•Type error detection by the compiler is difficult
Type Inference 
•Ex. ML 
fun circumf(r) = 3.14159 * r * r; 
fun square(x) = x * x; 
fun square( x : real) = x * x; 
fun square(x) = (x : real) * x; 
fun square(x) = x * (x : real); 
fun square(x) : real = x * x;
Storage Binding & Lifetime 
•Storage binding 
–Allocation - getting a cell from some pool of available cells 
–Deallocation - putting a cell back into the pool 
•The lifetime of a variable is the time during which it is bound to a particular memory cell
Categories of Variables by Lifetimes 
•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)
Categories of Variables by Lifetimes 
•Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. 
–If scalar, all attributes except storage are statically bound 
•local variables in C subprograms and Java methods 
•Advantage: allows recursion; conserves storage 
•Disadvantages: 
–Overhead of allocation and deallocation 
–Subprograms cannot be history sensitive 
–Inefficient references (indirect addressing)
Categories of Variables by Lifetimes 
•Explicit heap-dynamic -- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution 
•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
Categories of Variables by Lifetimes 
•Implicit heap-dynamic--Allocation and deallocation caused by assignment statements 
–all variables in APL; all strings and arrays in Perl and JavaScript 
•Advantage: flexibility 
•Disadvantages: 
–Inefficient, because all attributes are dynamic 
–Loss of error detection
Variable Attributes: Scope 
•The scope of a variable is the range of statements over which it is visible 
•The scope rules of a language determine how references to names are associated with variables 
•Local vs. nonlocal variables
Static Scope 
•Based on program text 
•To connect a name reference to a variable, you (or the compiler) must find the declaration 
•Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name 
•Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
Scope (continued) 
•Variables can be hidden from a unit by having a "closer" variable with the same name 
•C++ and Ada allow access to these "hidden" variables 
–In Ada: unit.name 
–In C++: class_name::name
Example -- Ada 
procedure Big is 
X : Integer; 
procedure Sub1 is 
X : Integer; 
begin -- of Sub1 
... 
end; -- of Sub1 
procedure Sub2 is 
begin -- of Sub2 
... X ... 
end; -- of Sub2 
begin -- of Big 
... 
end; -- of Big
Blocks 
•A method of creating static scopes inside program units--from ALGOL 60 
•Examples: C or C++ (not Java or C#) 
void sub() { 
int count; 
... 
while ( … ) { 
int count; 
count++; 
... 
} 
... 
} 
•Global variables are defined in outermost block
var A, B, C: real; //1 
procedure Sub1 (A: real); //2 
var D: real; 
procedure Sub2 (C: real); 
var D: real; 
begin 
… C:= C+B; … 
end; 
begin 
… Sub2(B); … 
end; 
begin 
… Sub1(A); … 
end. 
Declaration 
Scope 
A:real //1 
Main 
B:real //1 
Main,Sub1,Sub2 
C:real//1 
Main,Sub1 
A:real //2 
Sub1,Sub2 
…
•Assume MAIN calls A and B 
A calls C and D 
B calls A and E 
MAIN 
MAIN 
E 
A 
C 
D 
B 
A 
B C 
D E 
Evaluation of Static Scoping
Static Scope Example 
MAIN 
MAIN 
A 
B 
C 
D 
E 
A 
C 
B 
E 
D
Static Scope (continued) 
•Suppose the spec is changed so that D must now access some data in B 
•Solutions: 
–Put D in B (but then C can no longer call it and D cannot access A's variables) 
–Move the data from B that D needs to MAIN (but then all procedures can access them) 
•Same problem for procedure access 
•Overall: static scoping often encourages many globals
Dynamic Scope 
•Based on calling sequences of program units, not their textual layout 
•References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point 
•APL, SNOBOL4, early LISP (Perl, Common LISP)
Example 
procedure Big is 
X : Integer; 
procedure Sub1 is 
X : Integer; 
begin -- of Sub1 
... 
end; -- of Sub1 
procedure Sub2 is 
begin -- of Sub2 
... X ... 
end; -- of Sub2 
begin -- of Big 
... 
end; -- of Big 
Big --> Sub1 --> Sub2 
Big --> Sub2
Evaluation 
•Disadvantages: 
–Local variables are not private anymore, less reliable 
–Cannot statically type check 
–Readability 
–Reading overhead 
•Advantage: 
–No need to pass parameters
Scope and Lifetime 
•Scope and lifetime are sometimes closely related, but are different concepts 
•Examples: 
–A variable declared in a Java method that contains no method calls 
–A variable declared in C/C++ function with static specifier 
–Subprogram calls
Referencing Environments 
•The referencing environment of a statement is the collection of all names that are visible in the statement 
•In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes
procedure Example is 
A, B : Integer; 
... 
procedure Sub1 is 
X, Y : Integer; 
begin -- of Sub1 
... <-------------------------- 1 
end; -- of Sub1 
procedure Sub2 is 
X : Integer; 
... 
procedure Sub3 is 
X: Integer; 
begin -- of Sub3 
... <----------------------- 2 
end; -- of Sub3 
begin -- of Sub2 
... <------------------------ 3 
end; -- of Sub2 
begin -- of Example 
... <-------------------------- 4 
end. -- of Example
Referencing Environment 
•A subprogram is active if its execution has begun but has not yet terminated 
•In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms
void sub1() { 
int a, b; 
... <---------------- 1 
} /* end of sub1 */ 
void sub2() { 
int b, c; 
... <-------------- 2 
sub1; 
} /* end of sub2 */ 
void main() { 
int c, d; 
... <---------------- 3 
sub2(); 
} /* end of main */ 
main --> sub2 --> sub1
Named Constants 
•A named constant is a variable that is bound to a value only when it is bound to storage 
•Advantages: readability and modifiability 
•Used to parameterize programs 
•The binding of values to named constants can be either static (called manifest constants) or dynamic 
•Languages: 
–FORTRAN 90: constant-valued expressions 
–Ada, C++, and Java: expressions of any kind
Variable Initialization 
•The binding of a variable to a value at the time it is bound to storage is called initialization 
•Initialization is often done on the declaration statement, e.g., in Java 
int sum = 0;
Summary 
•Case sensitivity and the relationship of names to special words represent design issues of names 
•Variables are characterized by the sextuples: name, address, value, type, lifetime, scope 
•Binding is the association of attributes with program entities 
•Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic
Summary (continued) 
•Static scoping provides a simple, reliable, and efficient method of allowing visibility of nonlocal variables in subprograms 
•Dynamic scoping provides more flexibility than static scoping but at expense of readability, reliability, and efficiency 
•Referencing environment of a statement is the collection of all the variables that are visible to that statement

Mais conteúdo relacionado

Mais procurados (20)

Unit 4
Unit 4Unit 4
Unit 4
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Plc part 3
Plc  part 3Plc  part 3
Plc part 3
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Compiler1
Compiler1Compiler1
Compiler1
 
Unit 2
Unit 2Unit 2
Unit 2
 
Compiler design
Compiler designCompiler design
Compiler design
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Frp
FrpFrp
Frp
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Compilers
CompilersCompilers
Compilers
 
Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilers
 
Unit 5
Unit 5Unit 5
Unit 5
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
 

Destaque

Bus tracking application in Android
Bus tracking application in AndroidBus tracking application in Android
Bus tracking application in Androidyashonil
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8Kousuke Ruichi
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta LanguageKelly Bauer
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicornbaran19901990
 
Tìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google MapTìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google Mapbaran19901990
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesTeerawat Issariyakul
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentationbaran19901990
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and DynamicSneh Pahilwani
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sưbaran19901990
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apachebaran19901990
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notesVIKAS SINGH BHADOURIA
 
Bus tracking application project report
Bus tracking application project reportBus tracking application project report
Bus tracking application project reportAbhishek Singh
 
Bus Tracking Application in Android
Bus Tracking Application in AndroidBus Tracking Application in Android
Bus Tracking Application in AndroidAbhishek Singh
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scopesuthi
 

Destaque (15)

Control structure
Control structureControl structure
Control structure
 
Bus tracking application in Android
Bus tracking application in AndroidBus tracking application in Android
Bus tracking application in Android
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta Language
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicorn
 
Tìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google MapTìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google Map
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variables
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sư
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apache
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notes
 
Bus tracking application project report
Bus tracking application project reportBus tracking application project report
Bus tracking application project report
 
Bus Tracking Application in Android
Bus Tracking Application in AndroidBus Tracking Application in Android
Bus Tracking Application in Android
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
 

Semelhante a Names Bindings Scopes Chapter

5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and ScopesMunawar Ahmed
 
chapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guidechapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guideSanjeevSaharan5
 
codingtechniques1.ppt
codingtechniques1.pptcodingtechniques1.ppt
codingtechniques1.pptazida3
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
Java Closures
Java ClosuresJava Closures
Java ClosuresBen Evans
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.ASIT Education
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesteach4uin
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"Kazuhiro Sera
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Simon Ritter
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleNoam Kfir
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 
Programming Language
Programming  LanguageProgramming  Language
Programming LanguageAdeel Hamid
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////MeghaKulkarni27
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptxrayanbabur
 

Semelhante a Names Bindings Scopes Chapter (20)

chapter 5.ppt
chapter 5.pptchapter 5.ppt
chapter 5.ppt
 
5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes
 
chapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guidechapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guide
 
codingtechniques1.ppt
codingtechniques1.pptcodingtechniques1.ppt
codingtechniques1.ppt
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.
 
Java introduction
Java introductionJava introduction
Java introduction
 
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in Pharo
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
8. data types
8. data types8. data types
8. data types
 

Mais de baran19901990

Mais de baran19901990 (15)

How to build a news website use CMS wordpress
How to build a news website use CMS wordpressHow to build a news website use CMS wordpress
How to build a news website use CMS wordpress
 
Lexical
LexicalLexical
Lexical
 
Introduction
IntroductionIntroduction
Introduction
 
Datatype
DatatypeDatatype
Datatype
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
How to install git on ubuntu
How to install git on ubuntuHow to install git on ubuntu
How to install git on ubuntu
 
Ruby notification
Ruby notificationRuby notification
Ruby notification
 
Rails notification
Rails notificationRails notification
Rails notification
 
Linux notification
Linux notificationLinux notification
Linux notification
 
Lab4
Lab4Lab4
Lab4
 
Lab5
Lab5Lab5
Lab5
 
09
0909
09
 
Báo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnBáo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạn
 
MDA Framework
MDA FrameworkMDA Framework
MDA Framework
 

Último

Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012rehmti665
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationLinaWolf1
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Sonam Pathan
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一Fs
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一Fs
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Dana Luther
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhimiss dipika
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Sonam Pathan
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一Fs
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一z xss
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITMgdsc13
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Magic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptxMagic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptxMartaLoveguard
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Lucknow
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作ys8omjxb
 

Último (20)

Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 Documentation
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhi
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
 
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITM
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
 
Magic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptxMagic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptx
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
 
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
 

Names Bindings Scopes Chapter

  • 1. Chapter 5: Names, Bindings, and Scopes Principles of Programming Languages
  • 2. Contents •Names •Variables •The Concept of Binding •Scope and Lifetime •Referencing Environments •Named Constants
  • 3. Introduction •Imperative languages are abstractions of von Neumann architecture –Memory  variables •Variables characterized by attributes, among of them is type •Scope and lifetime of variables are important issues when designing type
  • 4. Variable Attributes •Name •Address •Value •Type •Lifetime •Scope
  • 5. Names (Identifiers) •Design issues for names: –Are names case sensitive? –Are special words reserved words or keywords?
  • 6. Name Forms •Length –If too short, they cannot be connotative –Language examples: •FORTRAN I: maximum 6 •FORTRAN 95: maximum 31 •C89: unlimited but first 31 is significant •Ada, Java, C#: no limit, and all are significant •C++: no limit, but implementers often impose one
  • 7. Name Forms •Most of languages: a letter followed by s string consisting of letters, digits, or underscore •Special forms: –PHP: variables begin with $ –Perl: special beginning characters denote type $, @, % –Ruby: @ instance variable, @@ class variable
  • 8. Name Forms •Case sensitivity –Many languages, including C-based languages, are case sensitive –Disadvantage: •readability (names that look alike are different) •writability: C++ and Java’s predefined names are mixed case (e.g. IndexOutOfBoundsException)
  • 9. Special Words •Special words –An aid to readability; used to delimit or separate statement clauses –A keyword is a word that is special only in certain contexts, e.g., in Fortran – A reserved word is a special word that cannot be used as a user-defined name –Which one is better? …, but…
  • 10. Variables •A variable is an abstraction of a memory cell •Variables can be characterized as 6 attributes: –Name –Address –Type –Value –Lifetime –Scope
  • 11. Variables Attributes •Name - not all variables have them •Address - the memory address with which it is associated –A variable may have different addresses at different times during execution –l-value –Two variable names can be used to access the same memory: aliases •created via pointers, reference variables, subprogram parameters •harmful to readability
  • 12. Variables Attributes •Type - determines the range of values of variables and the set of operations that are defined for values of that type •Value - the contents of the memory cell(s) associated with the variable –Memory cell here is abstract cell, not physical cell –r-value
  • 13. The Concept of Binding •A binding is an association –an attribute and an entity –an operation and a symbol •Binding time is the time at which a binding takes place.
  • 14. Possible Binding Times •Language design time -- bind operator symbols to operations •Language implementation time -- data type is bound to range of possible values •Compile time -- bind a variable to a data type in Java •Load time -- bind a variable to a memory cell for C static variable •Link time -- call to library subprogram to its code •Runtime -- bind a non-static local variable to a memory cell
  • 15. Example •Consider the C assignment statement count = count + 5; –Type of count –Set of possible values of count –Meaning of + –Internal representation of 5 –Value of count
  • 16. Static and Dynamic Binding •A binding is static if it first occurs before run time and remains unchanged throughout program execution. •A binding is dynamic if it first occurs during execution or can change during execution of the program
  • 17. Binding •Let’s discuss two types of binding: –Type binding: variable to data type –Storage binding: variable to its address
  • 18. Type Binding •How is a type specified? •When does the binding take place? •If static, the type may be specified by either an explicit or an implicit declaration
  • 19. Explicit/Implicit Declaration •An explicit declaration is a program statement used for declaring the types of variables •An implicit declaration is a default mechanism for specifying types of variables –Ex. •FORTRAN: I-N: Integer, others real •Perl: $a: scalar, @a: array –Advantage: writability –Disadvantage: reliability (less trouble with Perl)
  • 20. Dynamic Type Binding •JavaScript and PHP •Specified through an assignment statement e.g., JavaScript list = [2, 4.33, 6, 8]; list = 17.3; –Advantage: flexibility (generic program units) –Disadvantages: •High cost (dynamic type checking and interpretation) •Type error detection by the compiler is difficult
  • 21. Type Inference •Ex. ML fun circumf(r) = 3.14159 * r * r; fun square(x) = x * x; fun square( x : real) = x * x; fun square(x) = (x : real) * x; fun square(x) = x * (x : real); fun square(x) : real = x * x;
  • 22. Storage Binding & Lifetime •Storage binding –Allocation - getting a cell from some pool of available cells –Deallocation - putting a cell back into the pool •The lifetime of a variable is the time during which it is bound to a particular memory cell
  • 23. Categories of Variables by Lifetimes •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)
  • 24. Categories of Variables by Lifetimes •Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. –If scalar, all attributes except storage are statically bound •local variables in C subprograms and Java methods •Advantage: allows recursion; conserves storage •Disadvantages: –Overhead of allocation and deallocation –Subprograms cannot be history sensitive –Inefficient references (indirect addressing)
  • 25. Categories of Variables by Lifetimes •Explicit heap-dynamic -- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution •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
  • 26. Categories of Variables by Lifetimes •Implicit heap-dynamic--Allocation and deallocation caused by assignment statements –all variables in APL; all strings and arrays in Perl and JavaScript •Advantage: flexibility •Disadvantages: –Inefficient, because all attributes are dynamic –Loss of error detection
  • 27. Variable Attributes: Scope •The scope of a variable is the range of statements over which it is visible •The scope rules of a language determine how references to names are associated with variables •Local vs. nonlocal variables
  • 28. Static Scope •Based on program text •To connect a name reference to a variable, you (or the compiler) must find the declaration •Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name •Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
  • 29. Scope (continued) •Variables can be hidden from a unit by having a "closer" variable with the same name •C++ and Ada allow access to these "hidden" variables –In Ada: unit.name –In C++: class_name::name
  • 30. Example -- Ada procedure Big is X : Integer; procedure Sub1 is X : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is begin -- of Sub2 ... X ... end; -- of Sub2 begin -- of Big ... end; -- of Big
  • 31. Blocks •A method of creating static scopes inside program units--from ALGOL 60 •Examples: C or C++ (not Java or C#) void sub() { int count; ... while ( … ) { int count; count++; ... } ... } •Global variables are defined in outermost block
  • 32. var A, B, C: real; //1 procedure Sub1 (A: real); //2 var D: real; procedure Sub2 (C: real); var D: real; begin … C:= C+B; … end; begin … Sub2(B); … end; begin … Sub1(A); … end. Declaration Scope A:real //1 Main B:real //1 Main,Sub1,Sub2 C:real//1 Main,Sub1 A:real //2 Sub1,Sub2 …
  • 33. •Assume MAIN calls A and B A calls C and D B calls A and E MAIN MAIN E A C D B A B C D E Evaluation of Static Scoping
  • 34. Static Scope Example MAIN MAIN A B C D E A C B E D
  • 35. Static Scope (continued) •Suppose the spec is changed so that D must now access some data in B •Solutions: –Put D in B (but then C can no longer call it and D cannot access A's variables) –Move the data from B that D needs to MAIN (but then all procedures can access them) •Same problem for procedure access •Overall: static scoping often encourages many globals
  • 36. Dynamic Scope •Based on calling sequences of program units, not their textual layout •References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point •APL, SNOBOL4, early LISP (Perl, Common LISP)
  • 37. Example procedure Big is X : Integer; procedure Sub1 is X : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is begin -- of Sub2 ... X ... end; -- of Sub2 begin -- of Big ... end; -- of Big Big --> Sub1 --> Sub2 Big --> Sub2
  • 38. Evaluation •Disadvantages: –Local variables are not private anymore, less reliable –Cannot statically type check –Readability –Reading overhead •Advantage: –No need to pass parameters
  • 39. Scope and Lifetime •Scope and lifetime are sometimes closely related, but are different concepts •Examples: –A variable declared in a Java method that contains no method calls –A variable declared in C/C++ function with static specifier –Subprogram calls
  • 40. Referencing Environments •The referencing environment of a statement is the collection of all names that are visible in the statement •In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes
  • 41. procedure Example is A, B : Integer; ... procedure Sub1 is X, Y : Integer; begin -- of Sub1 ... <-------------------------- 1 end; -- of Sub1 procedure Sub2 is X : Integer; ... procedure Sub3 is X: Integer; begin -- of Sub3 ... <----------------------- 2 end; -- of Sub3 begin -- of Sub2 ... <------------------------ 3 end; -- of Sub2 begin -- of Example ... <-------------------------- 4 end. -- of Example
  • 42. Referencing Environment •A subprogram is active if its execution has begun but has not yet terminated •In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms
  • 43. void sub1() { int a, b; ... <---------------- 1 } /* end of sub1 */ void sub2() { int b, c; ... <-------------- 2 sub1; } /* end of sub2 */ void main() { int c, d; ... <---------------- 3 sub2(); } /* end of main */ main --> sub2 --> sub1
  • 44. Named Constants •A named constant is a variable that is bound to a value only when it is bound to storage •Advantages: readability and modifiability •Used to parameterize programs •The binding of values to named constants can be either static (called manifest constants) or dynamic •Languages: –FORTRAN 90: constant-valued expressions –Ada, C++, and Java: expressions of any kind
  • 45. Variable Initialization •The binding of a variable to a value at the time it is bound to storage is called initialization •Initialization is often done on the declaration statement, e.g., in Java int sum = 0;
  • 46. Summary •Case sensitivity and the relationship of names to special words represent design issues of names •Variables are characterized by the sextuples: name, address, value, type, lifetime, scope •Binding is the association of attributes with program entities •Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic
  • 47. Summary (continued) •Static scoping provides a simple, reliable, and efficient method of allowing visibility of nonlocal variables in subprograms •Dynamic scoping provides more flexibility than static scoping but at expense of readability, reliability, and efficiency •Referencing environment of a statement is the collection of all the variables that are visible to that statement