Categorias: Todos - variables - scope - attributes - dynamic

por Levi George 3 anos atrás

292

Chapter_5__Names_Bindings_and_Scopes_2

Variables serve as representations of machine memory, characterized by properties such as scope and lifetime. The concept of binding involves associating attributes with entities, such as variables with their types or values.

Chapter_5__Names_Bindings_and_Scopes_2

Chapter 5: Names, Bindings, and Scopes

5.5: Scope

Eval of Dynamic
Although, dynamic scoping allows bypassing of parameterizing for functions. Static - reliable, easy to read, fast dynamic - slow, easy to access, unreliable at times
Lastly: dynamic scoping requires long times to access appropriate nonlocals.
Issue 2: hard to type check references to non-local variables, harder to read and understand code.
Issue 1: low reliability, local vars of subprograms are visible to other running subprograms.
can't read and determine scope
Dynamic Scope
searches local declarations, then parents, then ancestors, if no declaration is found, run-time error.
Dynamic - scope can only be determined at runtime
Eval of Static Scoping
Due to variable nature of development, causes issues when restructuring programs. -- Solution? Encapsulation constructs
allows access to variables and subprograms, moreso than necessary
Global Scope
Some variables simply need to be at top of file, others need keyword.

You can use scope operator (::) to access global "hidden" by local

extern in C

declaration vs. definition
Global variables reach entire program (very bad)
Declaration Order
Different languages have different requirements

JS, allows declaration at bottom of block or function

C++ allow declarations in for loops

Variables typically must be declared before use
Blocks
minimized scope with dedicated local variables

variables are stack dynamic, allocated when code is entered, deallocated when code is exited.

Originated ALGOL 60
Static Scope
static parent - the declarator of a subprogram static ancestor - the declarator of a static parent
nested scopes vs. non nested scopes
prior to execution (static) scope is set, tell type by reading code

Named Constants

Initialization
If the storage binding is dynamic, initialization is also dynamic and the initial values can be any expression.
If the variable is statically bound to storage, binding and initialization occur before run time.
The binding of a variable to a value at the time it is bound to storage.
Parameterize a program.
Useful as aids to readability and program reliability.
A variable that is bound to a value only once.

5.7 Referencing Environments

Dynamically scoped languages
An active subprogram is one whose execution has begun, but not yet terminated.
In a dynamically scoped language, the referencing environment is the locally declared variables plus the variables of all other active subprograms.
Statically scoped languages
In a statically scoped language, the referencing environment is the variables declared in its local scope plus the collection of all variables of its ancestor scopes that are visible.
The referencing environment of a statement is the collection of all variables that are visible in the statement.

5.2 Names

Names can be of different lengths, and some languages set a specific length that a name can be. Other languages have an infinite length, but is only recognized by the first X characters. Finally, some languages allow infinite length and are recognized by that infinite length.
Design Issues
Are special words of the language reserved words or keywords?

Keywords are special words of a programming language that can be reassigned by a programmer.

A reserved word is a special word of a programming language that cannot be used as a name. A programmer cannot reassign a reserved word.

Should they be case sensitive?

Case sensitivity results in less readable code!

Name and identifier are interchangeable terms that are strings of characters used to identify an entity in a program. This could be a variable, a subprogram, formal parameter, etc.

5.1: Introduction

Defined by numerous properties
used based on scope and lifetime
stored based on type
variables are noted by name
Variables are representations of machine memory

5.4: The Concept of Binding

Storage Bindings and Lifetime
Implicit Heap-Dynamic Variables

Run-time overhead of maintaining all the dynamic attributes.

Have the highest degree of flexibility.

Bound to heap storage only when they are assigned values.

Stack-Dynamic Variables

The run-time overhead of allocation and deallocation.

Dynamic local storage so that each active copy of the recursive subprogram has its own version of the local variables.

Allocated from the runtime stack.

whose storage bindings are created when their declaration statements are elaborated, but whose types are statically bound.

Explicit Heap-Dynamic Variables

Difficult to use pointer and reference variables correctly.

Used to construct dynamic structures.

Nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions written by the programmer.

Static Variables

Storage cannot be shared among variables.

Cannot support recursive subprograms.

All addressing of static variables can be direct (faster).

Bound to a memory cell before program execution begins and remains bound to that same memory cell until program execution terminates.

The lifetime of a variable

The time during which the variable is bound to a specific memory location.

Deallocation

The process of placing a memory cell that has been unbound from a variable back into the pool of available memory.

Allocation

The memory cell to which a variable is bound somehow must be taken from a pool of available memory.

Binding of Attributes to Variables
Type Bindings

Types can be specified statically through some form of explicit or implicit declaration.

Before a variable can be referenced in a program, it must be bound to a data type.

When the binding takes place.

How the type is specified.

Dynamic

Implemented using pure interpreters rather than compilers.

Incorrect types of right sides of assignments are not detected as errors; rather, the type of the left side is simply changed to the incorrect type.

Causes programs to be less reliable.

Provides more programming flexibility.

Name can be thought of as being only temporarily bound to a type.

The variable is bound to a type when it is assigned a value in an assignment statement.

The type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name.

If the binding first occurs during run time or can change in the course of program execution.

Static

explicit declaration

Some of the problems with implicit declarations can be avoided by requiring names for specific types to begin with particular special characters.

Prevent the compilation process from detecting some typographical and programmer errors.

Implicit variable type binding is done by the language processor, either a compiler or an interpreter.

a means of associating variables with types through default conventions, rather than declaration statements.

a statement in a program that lists variable names and specifies that they are a particular type.

If it first occurs before run time begins and remains unchanged throughout program execution.

Binding
can take place at language design time, language implementation time, compile time, load time, link time, or run time.
An association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol.

Binding Time

The time at which a binding takes place.

5.6: Scope and Lifetime of Variables

Both of these concepts become fuzzy with subprogram calls and in a number of other situations outside of function calls from main.
C++'s 'static' keyword extends lifetime beyond the life of the function, but doesn't alter scope.
Scope is where the function can be reached, Lifetime is temporal and is the time that the memory for that variable exists and is allocated
Often restricted to the function range
Scope is the "reach" that a variable has, where it may be accessed.

5.3: Variables int varName = varValue

Type
The type of information stored, this in part determines the amount of memory and how that information is stored. A long int will take up more space than a short or regular int. (The type in this case is int, it precedes the variable name. In some cases it is not needed)
Address
Addresses are the computer memory cells that store information in them regarding the variable. This is not seen directly by programmers.
Name
Names are the way that we identify variables. Immediately after type (varName in this case)

Aliases

One address can be referenced by two names, not good for reading code

Value
That which is stored within a variables memory. placed on the RHS of a declaration or assignment.