Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Identifiers PowerPoint Presentation
Download Presentation
Identifiers

Identifiers

130 Views Download Presentation
Download Presentation

Identifiers

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Identifiers Identifiers name many things in programs, including… Identifier Properties

  2. Symbol Tables Static properties are determined at compile time. Thesymbol tableis the compiler’s dictionary. Examples (Java) int myInt = 3; public double total(double a, double b, double c) …

  3. Binding Bindingoccurs when a symbol (usually an identifier) is associated with a memory location a type (for a future lecture) a set of possible values a particular value a meaning (as for a subprogram) a set of possible meanings Such bindings can happen… ____________ binding means that a binding is fixed throughout execution. ____________ binding allows bindings to change in the midst of execution. malloc polymorphism

  4. Lifetime Three kinds of data lifetime - allocated from beginning to end of program execution. implemented via fixed memory - allocated when a block is entered and deallocated at block exit. implemented via run-time stack The textbook calls this type of data stack dynamic - allocated under program control implemented via storage heap Block-structured languages define regions for automatic data. Lifetime and scope are related, but different, concepts.

  5. Modula-2 Example MODULE Example1; VAR I : INTEGER; C : CARDINAL; GlobalBoo : BOOLEAN; PROCEDURE foo(B : BOOLEAN); VAR R : REAL; C : CHAR; Boo : BOOLEAN; BEGIN   foo(Boo);   END; (* foo *) BEGIN (* Example2 *)   foo(GlobalBoo);   END Example1.

  6. Java Example public class myClass { public int myInt; public SomeClass myClassVar; public final int size = 100; public static myStaticInt; public void myFunction( int j ) { int w = 4; for (int m = 0; m<20; m++) { System.out.println( m*w + j ); } myClass = new SomeClass(); myClass = new SomeClass(); myStaticInt = 7; } }

  7. Scope in Java… private - access restricted to containing class. public - unrestricted access. protected - access restricted to containing class and descendant classes. <instance variable with unspecified scope> - access restricted to package of classes <local variable> - access restricted to method/for statement block {…} in Eiffel… feature {NONE} - access restricted to containing object and subclasses. feature {ANY} - unrestricted access. feature {MyClass, YourClass} - access restricted to objects belonging to MyClass and YourClass type objects <local variable> - access restricted to block {…} Eiffel features are read only. Eiffel extends scope by objects. Java extends scope by classes. in Perl… dynamic scope

  8. program Example1(input, output); var j, k : integer; globalBoo : boolean; procedure foo1(b : boolean); var r, k : real; boo : boolean; function foo2(z : real) : real; begin foo2 := z + r + k + j; end begin {foo1}   foo1(boo); k := foo2(r);   end {foo1} begin {Example1}   foo1(globalBoo);   end. Pascal Example

  9. public class Outer { public int outPub; private int outPriv; private InnerPub inPubZ = new InnerPub(); private InnerPriv inPrivZ = new InnerPriv(); public class InnerPub { public int inPubPub; private int inPubPriv; public InnerPub() { } public void testAccess() { outPub = 1; outPriv = 1; inPrivZ.inPrivPub = 1; inPrivZ.inPrivPriv = 1; } } private class InnerPriv { public int inPrivPub; private int inPrivPriv; public InnerPriv() { } public void testAccess() { outPub = 1; outPriv = 1; inPubZinPubPub = 1; inPubZ.inPubPriv = 1; } } public Outer() { outPub = 1; outPriv = 1; inPubZ.inPubPub = 1; inPubZ.inPubPriv = 1; inPrivZ.inPrivPub = 1; inPrivZ.inPrivPriv = 1; inPubZ.testAccess(); inPrivZ.testAccess(); } } Java - inner classes

  10. Java public class Driver { public Classy c1, c2; public NotSoClassy nc1, nc2; public void Driver() { c1 = new Classy(); c2 = new Classy(); c1.z = 1; c1.s = “c1”; c2.z = 2; c2.s = “c2”; System.out.println(c1.z + “…” + c1.s); System.out.println(c2.z + “…” + c2.s); nc1 = new NotSoClassy(); nc2 = new NotSoClassy(); nc1.z = 1; nc1.s = “c1”; nc2.z = 2; nc2.s = “c2”; System.out.println(nc1.z + “…” + nc1.s); System.out.println(nc2.z + “…” + nc2.s); } } public class Classy { public int z; public String s; public Classy() { … } } public class NotSoClassy { public static int z; public static String s; public static final String alpha = “abc”; public NotSoClassy() { … } }

  11. Java public class OrderedPair { public int x; private int y; public OrderedPair(int a, int b) { x = a; y = b; } public boolean isBiggerThan(OrderedPair p) { returns (x > p.x && y > p.y); } } public class MyClass { public OrderedPair point1, point2; public MyClass() { point1 = new OrderedPair(1, 2); point2 = new OrderedPair(3, 2); if (point1.isBiggerThan(point2)) System.out.println(“Hi Mom”); point1.x = point.x * 2; point1.y = point.y* 2; // this instruction violates scope. } }

  12. Eiffel - part 1 class OrderedPair creation create; feature {ANY} x : INTEGER; create(a, b : INTEGER) is do x := a; y := b; end; isBiggerThan( p : OrderedPair ) : BOOLEAN is do result := (x > p.x and y > p.y); // p.y is a scope violation end; feature {NONE} y : INTEGER; end -- class OrderedPair

  13. Eiffel - part 2 class myClass feature {ANY} point1, point2 : OrderedPair; someMethod is do !!point1.create(1, 2); !!point2.create(3, 2); if point1.isBiggerThan(point2) then io.putstring(“Hi Mom.”); end; point1.x = point.x * 2; //write access is not permitted. point1.y = point.y* 2; // this violates both scope & write access end; end -- class myClass