1 / 31

Names, Bindings, Type Checking, and Scope in High-Level Programming Languages

Explore the concepts of names, bindings, type checking, and scope in high-level programming languages. Learn about variable names, their design issues, and the different categories of variables based on their lifetimes. Understand the importance of type checking and the scope of variables in programming.

georgebaker
Télécharger la présentation

Names, Bindings, Type Checking, and Scope in High-Level Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Names Bindings Type Checking Scope

  2. High-Level Programming Languages • Two main goals: • Machine independence • Ease of programming • High-level programming language are independent of any particular instruction set • But compilation to assembly code requires a target instruction set • There is a trade-off between machine independence and efficiency • E.g. Java vs. C

  3. Programming Language Ease of Programming • The driving problem in programming language design • Names • Control Flow • Types • Subroutines • Object Orientation • Concurrency • Declarative Programming

  4. Naming • Naming is the process by which the programmer associates a name with a potentially complicated program fragment • The goal is to hide complexity • Programming languages use name to designate variables, types, classes, methods, operators,… • Naming provides abstraction • E.g. Mathematics is all about the formal notation (i.e. naming) that lets us explore more and more abstract concepts

  5. Variable Names Design issues - What should the maximum length be? - Are connector characters allowed? - Are names case sensitive? - Are special words reserved words or keywords? Length - FORTRAN I: maximum 6 - COBOL: maximum 30 - FORTRAN 90 and ANSI C: maximum 31 - Ada: no limit, and all are significant - C++: no limit, but implementers often impose one

  6. Variable Names • Case sensitivity • Disadvantage: readability (names that look alike are different) • C, C++, Java, and Modula-2 names are case sensitive • The names in other languages are not • Special words • A keywordis a word that is special only in certain contexts • Disadvantage: poor readability • A reserved word is a special word that cannot be used as a user-defined name

  7. Variable Names • A variableis an abstraction of a memory cell • Variables can be characterized as a collection of attributes: • name, address, value, type, lifetime, and scope • Name - not all variables have them • Address - the memory address with which it is associated • A variable may have different addresses at different times during execution • A variable may have different addresses at different places in a program • If two variable names can be used to access the same memory location, they are called aliases

  8. Categories of variables by lifetimes Static--bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. e.g. C static variables Advantage: efficiency (direct addressing) Disadvantage: lack of flexibility (no recursion) Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. Advantage: allows recursion; conserves storage Disadvantages: - Overhead of allocation and de-allocation - Subprograms cannot be history sensitive - Inefficient references (indirect addressing)

  9. Categories of variables by lifetimes • Explicit heap-dynamic--Allocated and de-allocated by explicit directives, specified by the programmer, which take effect during execution • Referenced only through pointers or references e.g. dynamic objects in C++ (via new and delete) all objects in Java • Advantage: provides for dynamic storage management • Disadvantage: inefficient and unreliable Implicit heap-dynamic--Allocation and de-allocation caused by assignment statements Advantage: flexibility Disadvantages: - Inefficient, because all attributes are dynamic - Loss of error detection

  10. Type Checking • Type checking is the activity of ensuring that the operands of an operator are of compatible types • A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type. This automatic conversion is called a coercion. • Atype error is the application of an operator to an operand of an inappropriate type • If all type bindings are static, nearly all type checking can be static • If type bindings are dynamic, type checking must be dynamic • A programming language is strongly typed if type errors are always detected

  11. Strong Typing Allows the detection of the misuses of variables that result in type errors. C and C++ don’t have strong typing: parameter type checking can be avoided; unions are not type checked (Java is similar)

  12. Type Compatibility Type compatibilityby name means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name - Easy to implement but highly restrictive - Sub-ranges of integer types are not compatible with integer types - Formal parameters must be the same type as their corresponding actual parameters (Pascal) Type compatibility by structure means that two variables have compatible types if their types have identical structures - More flexible, but harder to implement

  13. Scope The scope of a variable is the range of statements over which it is visible The non-local variables of a program unit are those that are visible but not declared there The scope rules of a language determine how references to names are associated with variables Static scope Based on program text. To connect a name reference to a variable, the compiler must find the declaration. Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent

  14. Scope • Variables can be hidden from a unit by having a "closer" variable with the same name • C++ allows access to these "hidden" variables • Blocks- a method of creating static scopes inside program units • Examples: • C and C++: • for (...) { • int index; • ... • }

  15. Static Scoping • Java example Time t = new Time(“Feb 1 2002”); System.out.println(t); t = new Time(“Jan 30 2002”); System.out.println(t); t = new Time(“Jan 28 2002”); Time.mysteriousMethod(t) System.out.println(t);

  16. Nested SubroutinesClosest Nested Subroutine Rule

  17. Referencing Environments The referencing environment of a statement is the collection of all names that are visible in the statement In a static scoped language, that is the local variables plus all of the visible variables in all of the enclosing scopes A subprogram is active if its execution has begun but has not yet terminated In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms

  18. Referencing Environments A named constant is a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability The binding of values to named constants can be either static (called manifest constants) or dynamic Variable Initialization The binding of a variable to a value at the time it is bound to storage is called initialization Initialization is often done on the declaration statement

  19. Control and Data Abstraction • Control abstraction allows the programmer to hide an arbitrarily complicated code behind a simple interface • Subroutines • Classes • Data Abstraction allows the programmer to hide data representation details behind abstract operations • ADTs • Classes

  20. Binding Time • A binding is an association between two things • E.g. Name of an object and the object • Binding time is the time at which a binding is created • Language design time • Language implementation • Program writing time • Compile Time • Link Time • Load Time • Run Time

  21. Static vs. Dynamic Binding A binding is static if it occurs before run time and remains unchanged throughout program execution. A binding is dynamicif it occurs during execution or can change during execution of the program. • Type Bindings • 1. How is a type specified? • 2. When does the binding take place? • An explicit declaration is a program statement used for declaring the types of variables • An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program)

  22. Dynamic Type Binding Advantage:flexibility (generic program units) Disadvantages: 1. High cost (dynamic type checking and interpretation) 2. Type error detection by the compiler is difficult Type Inferencing (ML, Miranda, and Haskell) Rather than by assignment statement, types are determined from the context of the reference Storage Bindings Allocation- getting a cell from some pool of available cells Deallocation - putting a cell back into the pool The lifetime of a variable is the time during which it is bound to a particular memory cell

  23. Binding Time Impact • Binding times have a crucial impact in programming languages • They are a fundamental design decision • In general, early binding is associated with greater efficiency • E.g. C string vs. Java’s StringBuffer • In general, late binding is associated with greater flexibility • E.g. Class method vs. Subroutine • Compiled languages tend to bind names earlier than interpreted languages

  24. Object Lifetime Object Lifetime • Events in the life of a binding • Creation • Destruction • A binding to an object that no longer exists is called a dangling reference • Deactivation and Reactivation • Events in the life of an object • Creation • Destruction

  25. Storage Allocation Mechanisms • In static allocation, objects are given an absolute address that is retained throughout the program’s execution • E.g. Global variables, Non-recursive Subroutine Parameters

  26. Storage Allocation Mechanisms • In static allocation, (static) objects are given an absolute address that is retained throughout the program’s execution • E.g. Global variables, Non-recursive Subroutine Parameters • In stack-based allocation, (stack) objects are allocated in last-in, first-out data structure, a stack. • E.g. Recursive subroutine parameters

  27. Size of Integer Static Allocation Example • Calculator language example Memory read A read B sum := A + B write sum A B sum

  28. Storage Allocation Mechanisms • In static allocation, (static) objects are given an absolute address that is retained throughout the program’s execution • E.g. Global variables , Non-recursive Subroutine Parameters • In stack-based allocation, (stack) objects are allocated in last-in, first-out data structure, a stack. • E.g. Subroutine parameters • In heap-based allocation, (heap) objects may be allocated and deallocated at arbitrary times • E.g. objects created with C++ new and delete

  29. Heap-based Allocation • The heap is a region of storage in which sub-block can be allocated and deallocated • This not the heap data structure

  30. Heap Space Management • In general, the heap is allocated sequentially • This creates space fragmentation problems • Internal fragmentation • If size of object to allocated is larger than the size of the available heap • External fragmentation • If size of object to allocated is not larger than the size of the available heap, but the available space in the heap is scaterred through the head in such a way that no contiguous allocation is possible • Automatic de-allocation after an object has no bindings is called garbage collection • E.g. Java

  31. End of Lecture • Read Chapter 5

More Related