Name Spaces: ALL versus OOL

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
CSC 4181 Compiler Construction Scope and Symbol Table.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
Tutorial 6 & 7 Symbol Table
The Procedure Abstraction Part IV: Run-time Structures for OOLs Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Run time vs. Compile time
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
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.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
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.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
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
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
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 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
COMP3190: Principle of Programming Languages
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Advanced Programming in C
Implementing Subprograms
Programming Language Concepts
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
The Procedure Abstraction Part III: Symbol Tables, Storage
CS 326 Programming Languages, Concepts and Implementation
Object-Oriented Programming
Names.
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
Packages, Interfaces & Exception Handling
Nested class.
Implementing Subprograms
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Names, Binding, and Scope
CSE 3302 Programming Languages
Scope of Variables.
Scope, Visibility, and Lifetime
Packages and Interfaces
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Procedure Abstraction Part V: Run-time Structures for OOLs
The Procedure Abstraction Part I: Basics
PZ09A - Activation records
Chapter 7: User-Defined Functions II
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 3302 Programming Languages
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
The Three Attributes of an Identifier
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
C++ Object Oriented 1.
Implementing Subprograms
Presentation transcript:

Name Spaces: ALL versus OOL Maryam Gholamalitabar Spring 2017

The Procedure as a Name Space Each Scope (procedure or block) creates its own name space Any name (almost) can be declared locally Local names obscure identical non-local names Local names cannot be seen outside the procedure We call this set of rules & conventions “lexical scoping” Examples Algol and Pascal are the classic examples Nested Procedures, often with deep nesting C has global, static, local, and block scopes Blocks can be nested, procedures cannot

The Procedure as a Name Space Why introduce lexical scoping? Simplifies rules for naming & resolves conflicts Lets the programmer introduce “local” names with impunity Provide a compile-time mechanism for binding “free” variables The Questions At point p, which declaration of x is active? At run-time, where is x found? The Answer The compiler needs structures that model both the creation and the destruction of scopes Compilers use lexically-scoped symbol tables for this purpose

Lexically-Scoped Symbol Tables High-level idea Create a new table for each scope Chain them together for lookup The symbol table interface enter() - enter a new scope level insert(name) – creates entry for name in current scope lookup(name) – lookup a name, return an entry exit() – leave scope, remove all names declared there

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Name Resolution in an OOL What names can an executing method access? Names defined by the method Instance variables Class variables and method Any object defined in the global name space An OOL resembles an ALL, with a different name space Scoping is relative to hierarchy in the code of an ALL Scoping is relative to hierarchy in both the code & the data of an OOL

Concrete Example: The Java Name Space Code within a method M for object O of class C can see: Local variables declared within M All instance variables of O & class variables of C All public and protected variables of any superclass of C Classes defined in the same package as C or in any explicitly imported package Public class variables and public class instance variables of imported classes package class and instance variables in the package containing C Class declarations can be nested! Member declarations hide outer class declarations of the same name Accessibility options: public, private, protected, package

Java Symbol Tables To compiler method M of object O in class C, the compiler needs: Lexically scoped symbol table for the current block and its surrounding scopes Just like ALL– inner declarations hide outer declarations Chain of symbol tables for inheritance Class C and all of its superclasses Need to find methods and instance variables in any superclass Symbol tables for all global classes (package scope) Entries for all members with visibility Need to construct symbol tables for imported packages and link them into the structure in appropriate places Three sets of tables for name resolution In an ALL, we could combine 1 & 3 for a single unified set of tables.

The Java Name Space Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { // inherits x, y, & draw() from Point Color c; // local data public void draw() {…} // override (hide) Point’s draw public void test() { y = x; draw(); } // local code Class C { // independent of Point & ColorPoint int x, y; // local data public void m() // local code { Point p = new ColorPoint(); // uses ColorPoint, and, by inheritance y = p.x; // the definitions from Point p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Example Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { Color c; public void draw() {…} public void test() { y = x; draw(); } Class C { int x, y; public void m() { Point p = new ColorPoint(); y = p.x; p.draw();

Java Symbol Tables To find the address for a reference to x in method M for an object O of class C, the compiler must: For an unqualified use (i.e., x): Search the symbol table for the method’s lexical hierarchy Search the symbol tables for the receiver’s class hierarchy Search global symbol table (current package and imported) In each case check visibility attribute of x

Java Symbol Tables To find the address for a reference to x in method M for an object O of class C, the compiler must: For a qualified use (i.e., Q.x): Find Q by the method above Search from Q for x Must be a class or instance variable of Q or some class it extends Check visibility attribute of x

Reference Cooper, Keith, and Linda Torczon. Engineering a compiler. Elsevier, 2011.