0% found this document useful (0 votes)
273 views29 pages

Naming, Scope, and Binding Are Important Concepts in High-Level Languages

Names, scopes, and bindings are important concepts in programming languages. A name refers to an identifier or symbol. A binding associates a name with the thing it refers to. The scope of a binding is the region of code where the binding is active. There are different times when bindings can occur, from design time to run time. Static bindings occur earlier and allow for more efficiency, while dynamic bindings occur later and provide more flexibility. Scope rules determine how scopes are defined based on program structure.

Uploaded by

Pravin Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
273 views29 pages

Naming, Scope, and Binding Are Important Concepts in High-Level Languages

Names, scopes, and bindings are important concepts in programming languages. A name refers to an identifier or symbol. A binding associates a name with the thing it refers to. The scope of a binding is the region of code where the binding is active. There are different times when bindings can occur, from design time to run time. Static bindings occur earlier and allow for more efficiency, while dynamic bindings occur later and provide more flexibility. Scope rules determine how scopes are defined based on program structure.

Uploaded by

Pravin Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Names, Scopes, and Bindings, Scope Rules, Storage Management.

Type Systems
naming, scope, and binding are important concepts in high-level languages

• A name is exactly what you think it is


– Most names are identifiers
– symbols (like '+') can also be names
• A binding is an association between two things, such as a name and the thing it
names • The scope of a binding is the part of the program (textually)in which the
binding is active.
 Binding

 • Binding Time is the point at which a binding is created or, more


generally, the point at which any implementation decision is made
– language design time
• program structure, possible type
– language implementation time
• I/O, arithmetic overflow, type equality (if unspecified in manual)
Implementation decisions (continued ):
 –program writing time •algorithms, names
 –compile time •plan for data layout
 –link time •layout of whole program in memory
 –load time •choice of physical addresses
 •Implementation decisions (continued):
 –run time •value/variable bindings, sizes of strings
 •subsumes –program start-up time
–module entry time
–elaboration time (point a which a declaration is first "seen")
–procedure entry time
–block entry time
–statement execution time.
 The terms STATIC and DYNAMIC are generally used to refer to things bound
before Compile time and at run time, respectively
 • It is difficult to overstate the importance of Binding Times in programming
languages.
 In general, early binding times are associated with greater efficiency
 • Later binding times are associated with greater flexibility
 • Compiled languages tend to have early binding times
 • Interpreted languages tend to have later binding times
 • Today we talk about the binding of identifiers to the variables they name. Scope
Rules - control bindings
– Fundamental to all programming languages is the ability to name data, i.e., to refer
to data using symbolic identifiers rather than addresses
Lifetime and Storage Management
Key events
– creation of objects
– creation of bindings
– references to variables (which use bindings)
– reactivation of bindings
– destruction of bindings
– destruction of objects
 • The period of time from creation to destruction is called the LIFETIME of a binding
– If object outlives binding it's garbage
– If binding outlives object it's a dangling reference
• The textual region of the program in which the binding is active is its scope
• In addition to talking about the scope of a binding, we sometimes use the word scope as a noun all by itself,
without an indirect object.
• Storage Allocation mechanisms
–Static
–Stack
–Heap
• Static allocation for
–code
–globals
–static or own variables
–explicit constants (including strings, sets, etc)
 •Central stack for
–parameters
–local variables
–temporaries
•Why a stack?
–allocate space for recursive routines (not necessary in FORTRAN
– no recursion)
–reuse space (in all programming languages)
Contents of a stack frame (cf., Figure 3.1)
– arguments and returns
– local variables
– temporaries
– bookkeeping (saved registers, line number static link, etc.)
• Local variables and arguments are assigned fixed OFFSETS from the stack pointer or frame pointer at compile
time
 Maintenance of stack is responsibility of calling sequence and subroutine prologue and epilogue
–space is saved by putting as much in the prologue and epilogue as possible
–time may be saved by putting more in the body of the subroutine
•putting stuff in the caller instead or
•combining what's known in both places (interprocedural optimization)
 Scope Rules
• A scope is a program section of maximal size in which no bindings change, or at least in
which no re-declarations are permitted
• In most languages with subroutines, we OPEN a new scope on subroutine entry: – create
bindings for new local variables, – deactivate bindings for global variables that are redeclared
(these variable are said to have a "hole" in
• On subroutine exit: – destroy bindings for local variables – reactivate bindings for global
variables that were deactivated • Algol 68: – ELABORATION = process of creating bindings
when entering a scope • Ada (re-popularized the term elaboration): – storage may be allocated,
tasks started, even exceptions propagated as a result of the elaboration of declarations their
scope) – make references to variables.
 • With STATIC (LEXICAL) SCOPE RULES, a scope is defined in terms of the physical (lexical)
structure of the program – The determination of scopes can be made by the compiler – All bindings
for identifiers can be resolved by examining the program – Typically, we choose the most recent,
active binding made at compile time – Most compiled languages, C and Pascal included, employ
static scope rules
 We will see classes - a relative of modules
 later on, when discussing abstraction and object oriented languages
– These have even more sophisticated (static) scope rules
-nested scopes in which all scopes are enclosed within other scope.
– rules were designed to avoid ALIASES, which complicate optimization and correctness arguments
 • Note that the bindings created in a subroutine are destroyed at subroutine exit
– The modules of Modula, Ada, etc., give you closed scopes without the limited
lifetime
– Bindings to variables declared in a module are inactive outside the module, not
destroyed
– The same sort of effect can be achieved in many languages with own (Algol term)
or static (C term) variables
Access to non-local variables STATIC LINKS
– Each frame points to the frame of the (correct instance of) the routine inside which
it was declared
– In the absence of formal subroutines, correct means closest to the top of the stack
– You access a variable in a scope k levels out by following k static links and then
using the known offset within the frame thus found
 The key idea in static scope rules is that bindings are defined by the physical
(lexical) structure of the program.
• With dynamic scope rules, bindings depend on the current state of program
execution
– They cannot always be resolved by examining the program because they are
dependent on calling sequences
– To resolve a reference, we use the most recent, active binding made at run time
Dynamic scope rules are usually encountered in interpreted languages
• Such languages do not normally have type checking at compile time because type
determination isn't always possible when dynamic scope rules are in effect
C programming Memory Structure
 Binding of Referencing Environments
• SCOPE RULES determine that collection and its order
• BINDING RULES determine which instance of a scope should be used to resolve
references when calling a procedure that was passed as a parameter – they govern the
binding of referencing environments to formal procedures

You might also like