basic semantics associating meaning with language entities

30
Basic Semantics Associating meaning with language entities

Upload: trevor-horn

Post on 13-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic Semantics Associating meaning with language entities

Basic Semantics

Associating meaning with language entities

Page 2: Basic Semantics Associating meaning with language entities

Attributes & Binding

• Attributes associated with variables and other entities– Name– Location– Value

Page 3: Basic Semantics Associating meaning with language entities

Binding Times

• Language definition time

• Language implementation time

• Translation time (compile time)

• Link time (linking external entities)

• Load time (loading the program in memory)

• Execution time (run time)

Page 4: Basic Semantics Associating meaning with language entities

Symbol Table

• The symbol table associates names with attributes– In a compiler the Symbol Table associates

names with the static attributes• The attributes known only at compile time

Page 5: Basic Semantics Associating meaning with language entities

Environment & Memory

• The Environment is a binding of names to storage locations

• The Memory binds values to locations– Sometimes the memory is referred to as the

store or state

Page 6: Basic Semantics Associating meaning with language entities

Blocks

• A Block consists of a sequence of declarations and statements surrounded by syntactic markers such as braces or begin-end pairs

• In many languages blocks are called compound statements

Page 7: Basic Semantics Associating meaning with language entities

Local & Non-local Declarations

• Declarations associated with a specific block are called local declarations

• Declarations in surrounding blocks and global declarations are called non-local declarations

Page 8: Basic Semantics Associating meaning with language entities

Scope

• The scope of a binding is the region of the program over which the binding is maintained

• Lexical scope– Binding limited to the block in which the

declaration appears (and any blocks contained within this block)

Page 9: Basic Semantics Associating meaning with language entities

Declare-Before-Use Rule

• The scope of a declaration begins at the point of the declaration itself

• C and Pascal observe this rule

Page 10: Basic Semantics Associating meaning with language entities

Scope-holes & Visibility

• A scope-hole occurs when a local declaration takes precedence over a non-local declaration. The non-local variable then has a hole in its scope

• Visibility of a declaration includes only those areas of the program where the bindings of the declaration apply

• A declaration may be in scope, but not visible (hence a scope-hole)

• Some languages provide a scope resolution operator

Page 11: Basic Semantics Associating meaning with language entities

The Symbol Table

• A symbol table is like a variable dictionary

• Operations supported– Insertion– Look-up– Deletion

• Names and associated attributes are stored, representing bindings

Page 12: Basic Semantics Associating meaning with language entities

Static Scoping

• The symbol table is maintained by the compiler

• The bindings of all declarations are static

Page 13: Basic Semantics Associating meaning with language entities

Dynamic Scoping

• The Symbol Table is maintained dynamically, during execution

• Declarations are processed as they are encountered along the execution path through the program

Page 14: Basic Semantics Associating meaning with language entities

Name Resolution & Overloading

• To what extent can the same name be used to refer to different things in a program?

• The + operator is overloaded– Used to add integers– Used to add floating point numbers– Used to concatenate Strings

• How does the translator disambiguate these different uses?

Page 15: Basic Semantics Associating meaning with language entities

Overload Resolution

• The lookup feature of the Symbol Table is expanded to perform lookups of functions based not only on the name, but also on the number of parameters and their associated data types

• Ada allows the return type of functions, and even the names of the parameters in the definition also to be used in overload resolution

Page 16: Basic Semantics Associating meaning with language entities

Other overloading

• Some languages allow the same name to be used for different kinds of program entities

• Generally separate name spaces are kept for the different sorts of entities

class A { A A(A A) { A: for(;;) { if (A.A(A) == A) break A; } return A; } }

Page 17: Basic Semantics Associating meaning with language entities

Environment

• The binding of names to memory locations• FORTRAN has a complete static

environment• Lisp has a complete dynamic environment• Block-structured languages usually use a

mix of static & dynamic allocation– Globals are allocated statically– Locals are allocated dynamically

Page 18: Basic Semantics Associating meaning with language entities

Dynamic Environment of Local Variables

• When a block is entered during execution, variables declared in that block are allocated

• Upon exit from a block, the bindings of that block are deallocated, leaving the environment as it was before the block was entered

Page 19: Basic Semantics Associating meaning with language entities

Activation Records

• Each time a procedure or function is called, its local variables will be allocated

• The region of memory allocated for the local variables is called an activation record

• A recursive function or procedure may have several activation records associated with it – one for each recursive call

Page 20: Basic Semantics Associating meaning with language entities

Lifetime

• The lifetime of an entity is the duration of its allocation in the environment

Page 21: Basic Semantics Associating meaning with language entities

Pointers

• A pointer is an variable whose stored value is a reference to another entity– The value stored is actually the address of the

referenced entity

• A language which allows pointers provides a dereferencing operator to allow access to the object to which the pointer points

Page 22: Basic Semantics Associating meaning with language entities

The Heap

• The heap is the area of the environment that is used for arbitrary allocation and deallocation using facilities like new and delete

• Allocating on the heap is often referred to as dynamic allocation

• The heap is not to be confused with the data structure called a heap

Page 23: Basic Semantics Associating meaning with language entities

Allocation

• Automatic allocation– The allocation of local variables on the stack,

accomplished automatically under control of the run-time system

• Dynamic Allocation– Allocation under user control

Page 24: Basic Semantics Associating meaning with language entities

Variables

• A variable is an entity whose value can be changed during execution

• An assignment statement is the primary way to change the value of a variable– l-values– r-values– Assignment by sharing– Assignment by cloning

Page 25: Basic Semantics Associating meaning with language entities

Constants

• A constant is an entity that has a fixed value for the duration of its existence in a program

• A constant is like a variable except that it has no location attribute, but a value only– Compile-time constants

– Load-time constants (static constants)

– Manifest constants – those which are names for literal constants

Page 26: Basic Semantics Associating meaning with language entities

Functions

• Compile-time functions

• Function variables, as in C int (*f) (int, int) = gcd;

• Anonymous functions (function literals) as in ML

fn(x:int) => x * x

Page 27: Basic Semantics Associating meaning with language entities

Aliases

• An alias occurs when two different names are bound to the same location at the same time– Occurs with the use of pointers

– Occurs when a global variable is passed as a parameter to a procedure or function

Page 28: Basic Semantics Associating meaning with language entities

Dangling References

• A dangling reference is a location that has been deallocated from the environment, but which can still be accessed by a program

Page 29: Basic Semantics Associating meaning with language entities

Memory Leaks (Garbage)

• A memory leak occurs when a reference to a location has been removed from the environment, but the memory on the heap has not been deallocated

• This can occur when memory has been allocated in a block, and the block has been exited without deallocating the memory

Page 30: Basic Semantics Associating meaning with language entities

Garbage Collection

• Garbage collection is a means by which garbage is automatically deallocated by the language system

• Java provides automatic garbage collection