Kiến trúc máy tính và hợp ngữ - Chapter 4: Names The first step toward wisdom is calling things by their right names. Anon. Chinese Proverb

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. Name bindings play a fundamental role. The lifetime of a variable name refers to the time interval during which memory is allocated.

ppt35 trang | Chia sẻ: huyhoang44 | Lượt xem: 615 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Kiến trúc máy tính và hợp ngữ - Chapter 4: Names The first step toward wisdom is calling things by their right names. Anon. Chinese Proverb, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Programming Languages 2nd edition Tucker and NoonanChapter 4NamesThe first step toward wisdom is calling things by their right names. Anon. Chinese Proverb 4.1 Syntactic Issues4.2 Variables4.3 Scope4.4 Symbol Table4.5 Resolving References4.6 Dynamic Scoping4.7 Visibility4.8 Overloading4.9 Lifetime 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.Name bindings play a fundamental role.The lifetime of a variable name refers to the time interval during which memory is allocated.Syntactic IssuesLexical rules for names.Collection of reserved words or keywords.Case sensitivity C-like: yes Early languages: no PHP: partly yes, partly noReserved WordsCannot be used as Identifiers Usually identify major constructs: if while switch Predefined identifiers: e.g., library routinesVariablesBasic bindings NameAddressTypeValueLifetime 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 // Pointer example:int x,y;int *p;x = *p;*p = y;ScopeThe 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. 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? Algol C Java AdaPackage n/a n/a yes yesClass n/a n/a nested yesFunction nested yes yes nestedBlock nested nested nested nestedFor Loop no no yes automatic The scope in which a name is defined or delared 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. 1 void sort (float a[ ], int size) {2 int i, j;3 for (i = 0; i At line 4 and 11: Resolving ReferencesFor 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. 1 int h, i; 2 void B(int w) {3 int j, k;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 } Outer scope: Function B: Function A: Function main: Symbol Table Stack for Function B: Symbol Table Stack for Function A: Symbol Table Stack for Function main: Line Reference Declaration 4 i 1 10 h 1 11 i 9 16 h 1 18 h 1Dynamic ScopingIn 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. 1 int h, i; 2 void B(int w) {3 int j, k;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 } Using Figure 4.2 as an example: call history main (17)  A (10)  B Function Dictionary B A main Reference to i (4) resolves to in A. 1 int h, i; 2 void B(int w) {3 int j, k;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 } Using Figure 4.2 as an example: call history main (17)  B Function Dictionary B main Reference to i (4) resolves to in global scope.VisibilityA name is visible if its referencing environment includes the reference and the name is not reclared 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. 1 public class Student {2 private String name;3 public Student (String name, ...) {4 this.name = name;5 ...6 }7 } 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? Main.x?-- x in p3? p1.x?-- x in Main?OverloadingOverloading 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 JavaSystem.out.print(x) in Java Modula: library functionsRead( ) for charactersReadReal( ) for floating pointReadInt( ) for integersReadString( ) for strings 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);}LifetimeThe lifetime of a variable is the time interval during which the variable has been allocated a block of memory.Earliest languages used static allocation.Algol introduced the notion that memory should be allocated/deallocated at scope entry/exit.Remainder of section considers mechanisms which break scope equals lifetime rule. C:Global compilation scope: staticExplicitly declaring a variable staticRemark: Java also allows a variable to be declared static

Các file đính kèm theo tài liệu này:

  • pptch04_4149.ppt