SlideShare ist ein Scribd-Unternehmen logo
1 von 47
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
Vasavi College of Engg
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
Manoj Patil
 
Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilers
Vasim Pathan
 

Was ist angesagt? (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
 

Andere mochten auch

Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta Language
Kelly Bauer
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variables
Teerawat Issariyakul
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
baran19901990
 

Andere mochten auch (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
 

Ähnlich wie Subprogram

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
SanjeevSaharan5
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
Adeel Hamid
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
MeghaKulkarni27
 

Ähnlich wie Subprogram (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
 

Mehr von 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
 

Kürzlich hochgeladen

在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
ydyuyu
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Monica Sydney
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Monica Sydney
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
ydyuyu
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Monica Sydney
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
ayvbos
 

Kürzlich hochgeladen (20)

Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency Dallas
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
 

Subprogram

  • 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