Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.

Slides:



Advertisements
Similar presentations
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Advertisements

Programming Languages and Paradigms
Names and Bindings.
Basic Semantics.
Chapter 5 Basic Semantics
ISBN Chapter 10 Implementing Subprograms.
Programming Languages Third Edition
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 4: Names Fall 2009 Marco Valtorta.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
CSC321: Programming Languages Names Chapter 4: Names 4.1 Syntactic Issues 4.2 Variables 4.3 Scope 4.4 Symbol Table 4.5 Resolving References 4.6 Dynamic.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
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 and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
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)
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Names. 2 Variables  binding is an association between an entity (such as a variable) and a property (such as its value). A binding is static if the association.
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.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 5 Names, Bindings, and Scopes.
Programming Languages and Paradigms Imperative Programming.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
SE424 Languages with Context A Block Structured Language.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Dr. Philip Cannata 1 Names, Types, and Functions standard hue names.
Design issues for Object-Oriented Languages
Advanced Programming in C
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
Type Checking, and Scopes
Names, Bindings, and Scopes
Names.
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
CSE 3302 Programming Languages
Scope, Visibility, and Lifetime
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Names and Binding In Text: Chapter 5.
Names, Types, and Functions
CSE 3302 Programming Languages
Names, Bindings, and Scopes
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling things by their right names. Anon. Chinese Proverb

Copyright © 2006 The McGraw-Hill Companies, Inc. 4.1 Syntactic Issues 4.2 Variables 4.3 Scope 4.4 Symbol Table 4.5 Resolving References 4.6 Dynamic Scoping 4.7 Visibility 4.8 Overloading 4.9 Lifetime

Copyright © 2006 The McGraw-Hill Companies, Inc. Binding Binding establishes an association between an entity (such as a variable) and a property (such as its value or type). A binding is static if the association occurs before run-time. A binding is dynamic if the association occurs at run-time.

Copyright © 2006 The McGraw-Hill Companies, Inc. Names and Bindings Names are bound to some attributes when they are declared. Name resolution: the process of associating a name with its declaration. Names refer to variables, constants, functions.

Copyright © 2006 The McGraw-Hill Companies, Inc. Other Characteristics of Names The scope of a name: the lines of code that can reference the name. The lifetime of a variable name: the time interval during which memory is allocated to it. Visibility refers to the possibility that a name may be re- declared within the scope of an existing declaration, thus hiding previous instances of the name. Overloading permits name resolution for functions & operators to be based on number or type of parameters, so a single name can have two meanings within a single scope.

Copyright © 2006 The McGraw-Hill Companies, Inc. Syntactic Issues for Names Lexical rules for names (identifiers). Reserved words or keywords can’t be used as identifiers. Predefined identifiers? have a default meaning, can be redefined by program Case sensitivity ? C-like: yes Early languages, including Fortran, : no PHP: partly yes (variable names), partly no (class and function names (?)). Is this orthogonal?

Copyright © 2006 The McGraw-Hill Companies, Inc. Variables A variable is a binding of a name to a (virtual?) memory address. In addition to names and addresses, variables have other attributes: –Type –Value Variables also have scope and lifetime.

Copyright © 2006 The McGraw-Hill Companies, Inc. Two Meanings of A Variable Name L-value - use of a variable name to denote its address. Ex: x = … R-value - use of a variable name to denote its value. Ex: … = … x … R-value is x dereferenced

Copyright © 2006 The McGraw-Hill Companies, Inc. Some languages only recognize the l-value meaning They require explicit dereferencing of variables. Example - ML x := !y + 1 // !y => value of y Pointer dereferencing in C/C++ –int x,y; –int *p; –x = *p; –*p = y;

Copyright © 2006 The McGraw-Hill Companies, Inc. Scope The scope of a name is the collection of statements which can access the name binding. Static scoping: a name is bound to a collection of statements according to its position in the source program. Most modern languages use static (or lexical) scoping.

Copyright © 2006 The McGraw-Hill Companies, Inc. Static scoping can be handled at compile time by a simple scan of the code. Two different scopes are either nested or disjoint. In disjoint scopes, same name can be bound to different entities without any interference between the two definitions. What constitutes a scope?

Copyright © 2006 The McGraw-Hill Companies, Inc. AlgolCJavaAda Packagen/an/ayesyes Classn/a n/anestedyes Functionnestedyesyesnested Blocknested nestednestednested For Loopnono yes automatic A compilation unit also defines a scope

Copyright © 2006 The McGraw-Hill Companies, Inc. The scope in which a name is defined or declared is called its defining scope. A reference to a name is nonlocal if it occurs in a nested scope of the defining scope; otherwise, it is local. Most languages forbid forward references, the use of a name before its declaration.

Copyright © 2006 The McGraw-Hill Companies, Inc. 1 void sort (float a[ ], int size) { 2 int i, j; 3 for (i = 0; i < size; i++) // i, size local 4 for (j = i + 1; j < size; j++) 5 if (a[j] < a[i]) { // a, i, j are local references 6 float t; 7 t = a[i];// t is a local reference; 8 a[i] = a[j];// a, i: nonlocal references 9 a[j] = t; 10 } 11 } { } delimit a nested scopeFig. 4.1

Copyright © 2006 The McGraw-Hill Companies, Inc. 1 void sort (float a[ ], int size) { 2 int i, j; 3for (i = 0; i < size; i++) 4 for (j = i + 1; j < size; j++) 5if (a[j] < a[i]) { 6float t; 7t = a[i]; 8a[i] = a[j]; 9 a[j] = t; 10 } 11 } { } delimit a nested scope Fig The defining scope of a and size is lines 2 – 10 (between the curly braces) 2.The defining scope of i and j is the same block, but no references to them are permitted until after they are declared. 3.The scope of t is lines 7 – 9, with references only permitted after the declaration.

Copyright © 2006 The McGraw-Hill Companies, Inc. // for-loop scope in C++ & Java for (int i = 0; i < 10; i++) { System.out.println(i);... }... i... // invalid reference to i

Copyright © 2006 The McGraw-Hill Companies, Inc. Symbol Table – Static Scoping A symbol table is a data structure built during semantic analysis that allows the translator to keep track of each declared name and its binding. The data structure can be any implementation of a dictionary or set, where the name is the key & its binding is the value. –In Java, the Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values

Copyright © 2006 The McGraw-Hill Companies, Inc. Symbol Tables Assumption: a name is unique within its local scope (we ignore overloading for simplicity) Assumption: no forward references One symbol table entry for each name: name – binding When a name reference occurs during syntax or semantic processing, use the symbol table to determine the binding.

Copyright © 2006 The McGraw-Hill Companies, Inc. Resolving References Name-binding pairs are entered in the symbol table when they are defined; e.g., when a variable or function is declared When a name occurs in the code, consult the symbol table to determine the binding(s) for that name. Possible bindings: –name-virtual address –name-type –name-parameter list (for a function)

Copyright © 2006 The McGraw-Hill Companies, Inc. Symbol Tables A separate dictionary/symbol table for each local scope. Bindings for local variables are established by querying the dictionary for the current scope. What about non-local references? Use a stack of scopes

Copyright © 2006 The McGraw-Hill Companies, Inc. Creating/managing the symbol table a)Each time a scope is entered, push a new dictionary onto the stack. b)Each time a scope is exited, pop a dictionary off the top of the stack. c)For each name declared, generate an appropriate binding and enter the name-binding pair into the dictionary on the top of the stack. Name resolution: Given a name reference, search the dictionary on top of the stack: a)If found, return the binding. b)Otherwise, repeat the process on the next dictionary down in the stack. c)If the name is not found in any dictionary, report an error.

Copyright © 2006 The McGraw-Hill Companies, Inc. 1 void sort (float a[ ], int size) { 2 int i, j; 3for (i = 0; i < size; i++) 4 for (j = i + 1; j < size; j++) 5if (a[j] < a[i]) { 6float t; 7t = a[i]; 8a[i] = a[j]; 9 a[j] = t; 10 } 11 } { } delimit a nested scope Fig. 4.1 Symbol Table Stacks At line 4 and 11: ST2: ST1: At line 7: ST3: ST2: ST1:

Copyright © 2006 The McGraw-Hill Companies, Inc. Resolving References - Example For static scoping, the referencing environment for a name is its defining scope and all nested sub-scopes. The referencing environment defines the set of statements which can validly reference a name. A valid non-local reference is to a name defined in a nesting (outer) scope.

Copyright © 2006 The McGraw-Hill Companies, Inc. 1 int h, i; 2 void B(int w) { 3 int j, k; 4 i = 2*w; 5 w = w+1; } 8 void A (int x, int y) { 9 float i, j; 10 B(h); 11 i = 3; } 14 void main() { 15 int a, b; 16 h = 5; a = 3; b = 2; 17 A(a, b); 18 B(h); }

Copyright © 2006 The McGraw-Hill Companies, Inc. Symbol Tables for Individual Block Outer scope: 1.Function B: 2.Function A: 3.Function main: As program translation proceeds, different combinations will be on the stack

Copyright © 2006 The McGraw-Hill Companies, Inc. Symbol Table Stack in Function B : FB: Outer: Symbol Table Stack in Function A: FA: Outer: Symbol Table Stack in Function main: Main: Outer:

Copyright © 2006 The McGraw-Hill Companies, Inc. LineReferenceDeclaration 4 (in B) i1 10h1 11 (in A) i9 16h1 18h1

Copyright © 2006 The McGraw-Hill Companies, Inc. Dynamic Scoping In dynamic scoping, a name is bound to its most recent declaration based on the program’s call history. Used by early Lisp, APL, Snobol, Perl (as an option). Symbol table for each scope is built at compile time, but managed at run time. Scope pushed/popped on/off stack when entered/exited during execution.

Copyright © 2006 The McGraw-Hill Companies, Inc. How the compiler uses symbol tables Symbols entered in the tables by lexer or parser. Type bindings and other symtab operations are part of semantic analysis (more about this later). The information is used during type/semantic analysis and possibly code generation; e.g., the symbol table is used for type-checking statements and determining the relative program addresses. Symtabs aren’t present during execution for compiled languages.

Copyright © 2006 The McGraw-Hill Companies, Inc. 1 int h, i; 2 void B(int w) { 3 int j, k; 4 i = 2*w; 5 w = w+1; } 8 void A (int x, int y) { 9 float i, j; 10 B(h); 11 i = 3; } 14 void main() { 15 int a, b; 16 h = 5; a = 3; b = 2; 17 A(a, b); 18 B(h); }

Copyright © 2006 The McGraw-Hill Companies, Inc. Using Figure 4.2 as an example: call history main (17)  A (10)  B FunctionDictionary B A main global B’s reference to i (line 4) resolves to in A when dynamic scoping is used.

Copyright © 2006 The McGraw-Hill Companies, Inc. 1 int h, i; 2 void B(int w) { 3 int j, k; 4 i = 2*w; 5 w = w+1; } 8 void A (int x, int y) { 9 float i, j; 10 B(h); 11 i = 3; } 14 void main() { 15 int a, b; 16 h = 5; a = 3; b = 2; 17 A(a, b); 18 B(h); }

Copyright © 2006 The McGraw-Hill Companies, Inc. Using Figure 4.2 as an example: call history main (17)  B FunctionDictionary B main global This time, B’s reference to i (line 4) resolves to in the global scope. Different call history = different scopes for dynamic scoping.

Copyright © 2006 The McGraw-Hill Companies, Inc. Visibility A name is visible (when a reference is made) if its referencing environment includes the reference and the name has not been re- declared in an inner scope. A name re-declared in an inner scope effectively hides the outer declaration for the extent of that scope For example, the definition of i in function A hides the global definition.

Copyright © 2006 The McGraw-Hill Companies, Inc. Some languages provide a way to reference some “hidden” identifiers; e.g., Java’s “this”

Copyright © 2006 The McGraw-Hill Companies, Inc. 1 public class Student { 2 private String name; 3 public Student (String name,...) { 4this.name = name; } 7 } Some languages provide a mechanism for naming non-local references:

Copyright © 2006 The McGraw-Hill Companies, Inc. Function Nesting Used in Ada, Pascal, a few other languages. See Ada example:

Copyright © 2006 The McGraw-Hill Companies, Inc. procedure Main is x : Integer; procedure p1 is x : Float; procedure p2 is begin... x... end p2; begin... x... end p1; procedure p3 is begin... x... end p3; begin... x... end Main; -- Ada -- x in p2? -- x in p1? -- x in p3? -- x in Main?

Copyright © 2006 The McGraw-Hill Companies, Inc. Overloading Overloading uses the number or type of parameters or operands to distinguish among identical function names or operators. Examples: +, -, *, / can be float or int + can be float or int addition or string concatenation in Java, C++

Copyright © 2006 The McGraw-Hill Companies, Inc. Overloading Examples Java overloads print and println to print many different types (boolean, char, int,...) (see next page) Java also allows an instance variable and a method to have the same name.

Copyright © 2006 The McGraw-Hill Companies, Inc. public class PrintStream extends FilterOutputStream {... public void print(boolean b); public void print(char c); public void print(int i); public void print(long l); public void print(float f); public void print(double d); public void print(char[ ] s); public void print(String s); public void print(Object obj); }

Copyright © 2006 The McGraw-Hill Companies, Inc. Modula Different library functions for each type Read( ) for characters ReadReal( ) for floating point ReadInt( ) for integers ReadString( ) for strings

Copyright © 2006 The McGraw-Hill Companies, Inc. Overloading in programmer-defined types and functions Ada: first language to allow programmer- defined overloaded operators and functions. C++: follows the Ada model (both operators and functions can be overloaded) Java: overloading for methods only

Copyright © 2006 The McGraw-Hill Companies, Inc. Lifetime The lifetime of a variable is the time interval during which the variable has been allocated a block of memory. Earliest languages used static allocation for all memory (no stack or heap). Algol introduced the notion that memory should be allocated/deallocated at scope entry/exit.

Copyright © 2006 The McGraw-Hill Companies, Inc. Lifetime Statically allocated storage has a lifetime equal to the execution time of the program. There is only one copy of a function and its parameters or local variables Function variables retain values between calls (unless re-initialized). Storage allocation needs are determined at compile time and never change

Copyright © 2006 The McGraw-Hill Companies, Inc. Non-static allocation in C/C++ & Java By default, storage for local variables is allocated and deallocated as they go in and out of scope Stack based implementation C/C++: Explicitly declaring a variable static overrides the default Global compilation scope: static – “compilation unit” Java also allows class variables to be declared static

Copyright © 2006 The McGraw-Hill Companies, Inc. More about memory allocation when we discuss functions and function implementation.