Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019

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
Basic Semantics.
Chapter 5 Basic Semantics
Programming Languages Third Edition
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,
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 4: Names Fall 2009 Marco Valtorta.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
The Concept of Variables
Chapter 9: Subprogram Control
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.
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.
CSI 3125, Names etc., page 1 Names, binding, scope, type checking Names, variables Binding Scope Constants. Variable initialization Type checking Plan.
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.
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.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
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.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Advanced Programming in C
Object Lifetime and Pointers
Name Spaces: ALL versus OOL
Functions.
CS 326 Programming Languages, Concepts and Implementation
Chapter 4 Variables & Binding
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Names.
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Scope of Variables.
Scope, Visibility, and Lifetime
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSC 533: Programming Languages Spring 2015
Scope.
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Names, Types, and Functions
CSE 3302 Programming Languages
Names, Bindings, and Scopes
Names, binding, scope, type checking
CSC 533: Organization of Programming Languages Spring 2007
CSC 533: Programming Languages Spring 2019
Types and Related Issues
Presentation transcript:

Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019 CS485, Lecture 6, Name

Name Binding Recall that the term 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 occurs before run-time. A binding is dynamic if the association occurs at run-time. 5/27/2019 CS485, Lecture 6, Name

Name Binding Name bindings play a fundamental role. The lifetime of a variable name refers to the time interval during which memory is allocated. 5/27/2019 CS485, Lecture 6, Name

Syntactic Issues Lexical rules for names. Collection of reserved words or keywords. Case sensitivity C-like: yes Early languages: no PHP: partly yes, partly no 5/27/2019 CS485, Lecture 6, Name

Reserved Words Cannot be used as Identifiers Usually identify major constructs: if while switch Predefined identifiers: e.g., library routines 5/27/2019 CS485, Lecture 6, Name

Variables Basic bindings for a variable Name Address Type Value 5/27/2019 CS485, Lecture 6, Name

Variables 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 … Some languages support/require explicit dereferencing. Ex: x := !y + 1 5/27/2019 CS485, Lecture 6, Name

Variables // Pointer example: int x,y; int *p; x = y; // assign to the l-value of x the r-value of y x = *p; // assign to the l-value of x the r-value of the r-value of p *p = y; 5/27/2019 CS485, Lecture 6, Name

Scope The scope of a name is the collection of statements which can access the name binding. In 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. 5/27/2019 CS485, Lecture 6, Name

Scopes Two different scopes are either nested or disjoint. In disjoint scopes, same name can be bound to different entities without interference. What constitutes a scope? 5/27/2019 CS485, Lecture 6, Name

Class n/a n/a nested yes Function nested yes yes nested Algol C Java Ada Package n/a n/a yes yes Class n/a n/a nested yes Function nested yes yes nested Block nested nested nested nested For Loop no no yes automatic 5/27/2019 CS485, Lecture 6, Name

Scopes 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. 5/27/2019 CS485, Lecture 6, Name

for (int i = 0; i < 10; i++) { System.out.println(i); ... } ... i ... // invalid reference to i 5/27/2019 CS485, Lecture 6, Name

Symbol Table A symbol table is a data structure kept by a translator that allows it to keep track of each declared name and its binding. Assume for now that each name is unique within its local scope. The data structure can be any implementation of a dictionary, where the name is the key. 5/27/2019 CS485, Lecture 6, Name

Each time a scope is entered, push a new dictionary onto the stack. Each time a scope is exited, pop a dictionary off the top of the stack. For each name declared, generate an appropriate binding and enter the name-binding pair into the dictionary on the top of the stack. Given a name reference, search the dictionary on top of the stack: If found, return the binding. Otherwise, repeat the process on the next dictionary down in the stack. If the name is not found in any dictionary, report an error. 5/27/2019 CS485, Lecture 6, Name

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 local 6 float t; 7 t = a[i]; // t local; a, i nonlocal 8 a[i] = a[j]; 9 a[j] = t; 10 } 11 } 5/27/2019 CS485, Lecture 6, Name

at line 7: At line 4 and 11: <t, 6> <j, 2> <i, 2> <size,1> <a, 1> <sort, 1> At line 4 and 11: Is a reference to i at 7 local or non-local? 5/27/2019 CS485, Lecture 6, Name

Resolving References For static scoping, the referencing environment for a name is its defining scope and all nested subscopes. The referencing environment defines the set of statements which can validly reference a name. 5/27/2019 CS485, Lecture 6, Name

1 int h, i; 2 void B(int w) { 3 int j, k; 4 i = 2*w; 5 w = w+1; 6 ... 6 ... 7 } 8 void A (int x, int y) { 9 float i, j; 10 B(h); 11 i = 3; 12 ... 13 } 14 void main() { 15 int a, b; 16 h = 5; a = 3; b = 2; 17 A(a, b); 18 B(h); 19 ... 20 } 5/27/2019 CS485, Lecture 6, Name

Function B: <w, 2> <j, 3> <k, 4> Outer scope: <h, 1> <i, 1> <B, 2> <A, 8> <main, 14> Function B: <w, 2> <j, 3> <k, 4> Function A: <x, 8> <y, 8> <i, 9> <j, 9> Function main: <a, 15> <b, 15> 5/27/2019 CS485, Lecture 6, Name

Symbol Table Stack for Function B: <w, 2> <j, 3> <k, 4> <h, 1> <i, 1> <B, 2> <A, 8> <main, 14> Symbol Table Stack for Function A: <x, 8> <y, 8> <i, 9> <j, 9> Symbol Table Stack for Function main: <a, 15> <b, 15> 5/27/2019 CS485, Lecture 6, Name

Line Reference Declaration 4 i 1 10 h 1 11 i 9 16 h 1 18 h 1 5/27/2019 CS485, Lecture 6, Name

Dynamic Scoping In dynamic scoping, a name is bound to its most recent declaration based on the program’s call history. Used be early Lisp, APL, Snobol, Perl. Symbol table for each scope built at compile time, but managed at run time. Scope pushed/popped on stack when entered/exited. 5/27/2019 CS485, Lecture 6, Name

1 int h, i; 2 void B(int w) { 3 int j, k; 14 void main() { 4 i = 2*w; 5 w = w+1; 6 ... 7 } 8 void A (int x, int y) { 9 float i, j; 10 B(h); 11 i = 3; 12 ... 13 } 14 void main() { 15 int a, b; 16 h = 5; a = 3; b = 2; 17 A(a, b); 18 B(h); 19 ... 20 } 5/27/2019 CS485, Lecture 6, Name

B <w, 2> <j, 3> <k, 3> call history main (17)  A (10)  B Function Dictionary B <w, 2> <j, 3> <k, 3> A <x, 8> <y, 8> <i, 9> <j, 9> main <a, 15> <b, 15> <h, 1> <i, 1> <B, 2> <A, 8> <main, 14> Reference to i (4) resolves to <i, 9> in A. 5/27/2019 CS485, Lecture 6, Name

1 int h, i; 2 void B(int w) { 3 int j, k; 4 i = 2*w; 5 w = w+1; 6 ... 6 ... 7 } 8 void A (int x, int y) { 9 float i, j; 10 B(h); 11 i = 3; 12 ... 13 } 14 void main() { 15 int a, b; 16 h = 5; a = 3; b = 2; 17 A(a, b); 18 B(h); 19 ... 20 } 5/27/2019 CS485, Lecture 6, Name

B <w, 2> <j, 3> <k, 3> call history main (17)  B Function Dictionary B <w, 2> <j, 3> <k, 3> main <a, 15> <b, 15> <h, 1> <i, 1> <B, 2> <A, 8> <main, 14> Reference to i (4) resolves to <i, 1> in global scope. 5/27/2019 CS485, Lecture 6, Name

Visibility A name is visible if its referencing environment includes the reference and the name is not redclared in an inner scope. A name redeclared in an inner scope effectively hides the outer declaration. Some languages provide a mechanism for referencing a hidden name; e.g.: this.x in C++/Java. 5/27/2019 CS485, Lecture 6, Name

3 public Student (String name, ...) { 4 this.name = name; 5 ... 6 } 1 public class Student { 2 private String name; 3 public Student (String name, ...) { 4 this.name = name; 5 ... 6 } 7 } 5/27/2019 CS485, Lecture 6, Name

1. procedure Main is 2. x : Integer; 3. procedure p1 is 4. x : Float; 6. begin 7. ... x ... 8. end p2; 9. begin 10. ... x ... 11. end p1; 12. procedure p3 is 13. begin 14. ... x ... 15. end p3; 16. begin 17. ... x ... 18. end Main; -- Ada -- x in p2? -- x in p1? Main.x? -- x in p3? p1.x? -- x in Main? 5/27/2019 CS485, Lecture 6, Name

Overloading Overloading uses the number or type of parameters 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 System.out.print(x) in Java 5/27/2019 CS485, Lecture 6, Name

Modula: library functions Read( ) for characters ReadReal( ) for floating point ReadInt( ) for integers ReadString( ) for strings 5/27/2019 CS485, Lecture 6, Name

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); } 5/27/2019 CS485, Lecture 6, Name

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. What is the problem of the static allocation? 5/27/2019 CS485, Lecture 6, Name

Lifetime Algol introduced the notion that memory should be allocated/deallocated at scope entry/exit. So scope = lifetime In some cases, a variable should not forget its value. We consider mechanisms which break scope equals lifetime rule. 5/27/2019 CS485, Lecture 6, Name

C: Global compilation scope: static Explicitly declaring a variable static Remark: Java also allows a variable to be declared static 5/27/2019 CS485, Lecture 6, Name