Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Semantics Associating meaning with language entities.

Similar presentations


Presentation on theme: "Basic Semantics Associating meaning with language entities."— Presentation transcript:

1 Basic Semantics Associating meaning with language entities

2 Attributes & Binding Attributes associated with variables and other entities –Name –Location –Value

3 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)

4 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

5 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

6 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

7 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

8 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)

9 Declare-Before-Use Rule The scope of a declaration begins at the point of the declaration itself C and Pascal observe this rule

10 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

11 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

12 Static Scoping The symbol table is maintained by the compiler The bindings of all declarations are static

13 Dynamic Scoping The Symbol Table is maintained dynamically, during execution Declarations are processed as they are encountered along the execution path through the program

14 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?

15 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

16 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; } }

17 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

18 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

19 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

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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

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

29 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

30 Garbage Collection Garbage collection is a means by which garbage is automatically deallocated by the language system Java provides automatic garbage collection


Download ppt "Basic Semantics Associating meaning with language entities."

Similar presentations


Ads by Google