Download presentation
Presentation is loading. Please wait.
Published byHarold Goodman Modified over 6 years ago
1
Names and Attributes Names are a key programming language feature
Also known as labels or identifiers Allow a program to declare and refer to an abstraction Names almost always have additional attributes E.g., a type, which may depend on other names Names with data types also have a value attribute Variables also will have a location, but constants might not A function’s signature includes its name and its type I.e., its name and the ordered list of types the function takes Attributes may be bound to labels at different times Lexically scoped languages often bind statically (prior to execution) though some features may be bound dynamically
2
Bindings A binding associates a set of attributes with a name
E.g., int &i = j; // i is a reference to int j Bindings can occur at many different times Language design time: control flow constructs, constructors Language implementation time: 32 vs 64 bit int, etc. Programming time: names given to algorithms, objects, etc. Compile time: templates are instantiated (types are bound), machine code produced for functions and methods Link time: calls between compilation units are linked up Load time: virtual addresses mapped to physical ones Run time: scopes are entered and left, dynamic allocation of objects, updates to variables, pointers, and references, etc.
3
Static, Stack, and Dynamic Allocation
In static allocation, unchanging names are introduced E.g., program code, global or static objects, etc. Manifest constants can be defined at compile time as well In stack based allocation, scoped names are added Recursion requires a stack to maintain frames for calls Elaboration time constants, local variables, parameters, etc. In heap based allocation, names appear any time I.e., whenever the program calls a dynamic allocation operator, constructor, etc. Deallocation may be either explicit or implicit If implicit, garbage collection is often needed
4
Declarations, Blocks, and Scope
A declaration explicitly binds some information to an identifier, and may bind other information implicitly E.g, the statement int x; explicitly binds integer type attribute int with identifier x, implicitly binds a location for x, and may or may not implicitly bind a value (i.e., 0) for x Declarations that bind all attributes are called definitions Declarations that leave some attributes unbound are called simply declarations, or sometimes prototypes A block is a sequence of declarations and definitions Declarations that are specific to a block are locally scoped Program-wide declarations are globally scoped Block-structured languages allow both nesting of blocks and scoped re-declaration of identifiers (via lexical addressing) Classes, structs, namespaces, packages also create scopes
5
Scope Rules A scope identifies where a binding is active
E.g., from the point where it is declared until its block ends In modern languages, often determined statically at compile time (lexically scoped through the process of compilation) Static scoping vs. nested subroutines Scoping layout can be established at compile time but the details must be managed at run-time in many languages I.e., inter-frame pointers, instruction counter, etc. on stack Declaration order may require additional semantics E.g., in C++ can declare and define bindings separately Modules and classes have possibly different uses For functional subdivision vs. encapsulation respectively E.g., namespaces vs. structs and classes in C++
6
Today’s Studio Exercises
We’ll code up ideas from Scott Chapter Looking at the semantics of different C++ constructs Building and managing a simple symbol table Next time we’ll look at additional topics and extend our symbol table implementation (save your code!) Today’s exercises are again in C++ Please take advantage of the on-line reference manual pages that are linked on the course web site As always, please ask us for help as needed When done, your answers to the course account with “Semantics Studio I” in the subject
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.