Presentation is loading. Please wait.

Presentation is loading. Please wait.

Name Spaces: ALL versus OOL

Similar presentations


Presentation on theme: "Name Spaces: ALL versus OOL"— Presentation transcript:

1 Name Spaces: ALL versus OOL
Maryam Gholamalitabar Spring 2017

2 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

3 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

4 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

5 Example

6 Example

7 Example

8 Example

9 Example

10 Example

11 Example

12 Example

13 Example

14 Example

15 Example

16 Example

17 Example

18 Example

19 Example

20 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

21 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

22 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.

23 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();

24 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();

25 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();

26 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();

27 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();

28 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();

29 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();

30 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();

31 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();

32 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();

33 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();

34 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();

35 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();

36 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();

37 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();

38 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();

39 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();

40 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();

41 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

42 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

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


Download ppt "Name Spaces: ALL versus OOL"

Similar presentations


Ads by Google