Names and Bindings.

Slides:



Advertisements
Similar presentations
Names, Bindings, Type Checking, and Scopes
Advertisements

Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Chapter 5 Names, Bindings, and Scopes
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Lecture 05 Variable Attributes.
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
Names, Bindings, and Scopes
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
ISBN Chapter 5 Names, Bindings, and Scopes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Names, Bindings, Type Checking, and Scopes
Object Lifetime and Pointers
Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
Presentation transcript:

Names and Bindings

Names A name is a string of characters used to identify some entity in a program Several design choices are involved even in specifying allowable names Case sensitivity Minimum or maximum length Connectors Treatment of special words

Length If too short, names cannot be connotative If too long, extra space required for the symbol table Some examples: FORTRAN I: maximum 6 COBOL: maximum 30 FORTRAN 90 and ANSI C: maximum 31 Ada and Java: no limit, and all are significant C++: technically no limit, but implementers often impose one

Names continued Most PLs have the same form for names: a letter followed by a string of letter, digits and underscores Underscores very popular in 1970s and 1980s to represent spaces Replaced in C-based languages by camel notation Prior to Fortran90, names could contain spaces C-based languages are case sensitive Detriment to readability? Writability questionable?

Special Words Aid in readability Used to delimit or separate statement clauses Keyword is special only in certain contexts Fortran is the only remaining widely used language whose special words are keywords Reserved words cannot be used as names Cannot be redefined Potentially less confusing, but too many means user has trouble making up new ones Predefined names are between special words and user- defined names Can be redefined

Variables Abstraction of a computer memory cell or collection of cells Made up of six attributes: Name Address Value Type Lifetime Scope

Address of a Variable The machine memory address with which a variable is associated Can change during the course of execution Different addresses at different times during execution Different addresses at different locations in a program Sometimes referred to as l-value left-hand side of an assignment Multiple variables can have the same address, in which case the variables are called aliases Hindrance to readability Can be created in several ways

Variable Type Determines range of values the variable can store Determines the set of operations that are defined for values of that type E.g. int of Java specifies: Range of -2147483648 to 2147483647 Arithmetic operations +, -,*, /,%

Variable Value The contents of the memory cell or cells associated with the variable Abstract memory cell has the size required by the variable with which it is associated The value of each simple, non-structured type is considered to occupy a single abstract memory cell E.g. Though a floating point number may occupy 4 physical bytes, the value is thought of as occupying a single abstract memory cell` R-value, since this is what is required when used on the RHS of an assignment statement

Binding A binding is an association Attribute and an entity Operation and a symbol The time when the association takes place is the binding time Both binding and binding time are important for semantics of programming languages

Possible Binding Times Language design time -- bind operator symbols to operations Language implementation time -- bind floating point type to a representation Compile time -- bind a variable to a type in C or Java Load time -- bind a FORTRAN 77 variable to a memory cell (or a C static variable) Runtime -- bind a nonstatic local variable to a memory cell

Java assignment example Count = count + 5; Type of count bound at compile time Set of possible values bound at compiler design time Meaning of + bound at compile time (when types of operands have been determined) Internal representation of literal 5 bound at compiler design time Value of count is bound at execution time with this statement

Static vs. Dynamic Bindings Static binding First occurs before run time AND Remains unchanged throughout program execution Dynamic binding First occurs at run time OR Can change in the course of program execution

Type Binding Before a variable can be referenced in a program, it must be bound to a data type Two questions: When does type binding take place? How is type specified?

Static Type Binding Explicit declaration lists variable names and specifies that they are a particular type Implicit declaration associates variables with types though default conventions First appearance constitutes its implicit declaration Both versions in use today Advantage of implicit: writability Disadvantage: reliability (though less so in Perl)

Dynamic Type Binding Type of variable is determined when it is assigned a value Advantage Programming flexibility Disadvantages Reduced reliability Cost Usually implemented in languages with interpreters rather than compilers

Type Inference Not by assignment, but by context E.g ML function declaration: Fun circumf(r) = 3.14159 * r * r Floating point (real) type inferred from the constant Fun square(x) = x * x Arithmetic operator * indicates numeric type, so return type and arguments are default numeric type int

Allocation and Deallocation Process by which an available memory cell is taken from a pool of available memory and bound to a variable Deallocation Process of placing a memory cell that has been unbound from a variable back to the pool of available memory

Lifetime The time during which the variable is bound to a specific memory location Scalar variables fall into four categories based on their lifetimes: Static Stack-dynamic Explicit heap-dynamic Implict heap-dynamic

Static Variables Bound to memory locations before program execution begins Remain bound throughout the program execution Advantages: Efficiency through direct accessing History sensitive subprogram support Disadvantages Reduced flexibility (no recursion, no shared storage)

Stack-Dynamic Variables Storage bindings are created when their declaration statements are elaborated, but type is dynamically bound Elaboration refers to the storage allocations and binding process indicated by the declaration Occurs during run-time If scalar, all attributes except address are statically bound Advantages Allows recursion, conserves storage Disadvantages Overhead of allocation and deallocation Subprograms cannot be history sensitive Inefficient references (indirect addressing)

Explicit Heap-Dynamic Variables Allocated and deallocated 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 Variables Bound to heap storage only when they are assigned values (i.e. assignment statements) E.g. all strings and arrays in Perl and JavaScript Advantage Extremely flexible Disadvantage Inefficient, since all attributes are dynamic Loss of error detection