Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 16 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
16
Dung lượng
101,71 KB
Nội dung
C o e on nZ Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn 09, 2013 hV ie HCMC University of Technology, Viet Nam in m Name, Binding and Scope https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e on Name - character string used to represent something else nZ identifiers, operators (+, &, *) Use symbol instead of address to refer an entity hV ie Abstraction in m Name https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e on Definition Binding - the operation of associating two things Binding time - the moment when the binding is performed nZ Some issues Early binding vs Late binding ie Static binding vs Dynamic binding Polymorphism - A name is bound to more than one entity hV Alias - Many names are bound to one entity in m Binding https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e on Language design time Language implementation time Programming time Linking time ie Load time nZ Compilation time hV Runtime in m Binding Time https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e Object - any entity in the program Binding lifetime ie nZ Dangling reference p = new int; q = p; delete p; *q; on Object lifetime - the period between the object creation and destruction p q hV Leak memory - Garbage p = new int; p = null; in m Object Lifetime https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e Stack Dynamic on Static nZ Explicit Heap Dynamic Static Stack hV ie Implicit Heap Dynamic in m Object Allocation Heap https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e on Definition A block is a textual region, which can contain declarations to that region nZ Example, hV ie procedure f o o ( ) var x : i n t e g e r ; begin x := 1; end ; in m Blocks { int x ; x = 1; } https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o Static vs Dynamic on e Definition Scope of a binding is the textual region of the program in which the binding is effective nZ Static scope, or lexical scope, is determined during compilation ie Current binding - in the block most closely surround Global scope Local static scope hV Dynamic scope is determined at runtime Current binding - the most recently execution but not destroyed in m Scope https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e on A reference to an identifier is always bound to its most local declaration nZ A declaration is invisible outside the block in which it appears Declarations in enclosing blocks are visible in inner blocks, unless they have been re-declared hV ie Blocks may be named and its name declaration is considered as a local declaration of outer block in m Staic Scope Rules for Blocks https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e nZ on var A, B, C: real; //1 procedure Sub1 (A: real); //2 var D: real; procedure Sub2 (C: real); var D: real; begin C:= C+B; end; begin Sub2(B); end; begin Sub1(A); end Variable A:real //1 B:real //1 ie C:real //1 A:real //2 hV in m Example on Static scope https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope Scope Main Main, Sub1, Sub2 Main, Sub1 Sub1, Sub2 C o e on X in Sub2 ? Calling chain: Big → Sub1 → Sub2 X ⇒ X:Integer in Sub1 Calling chain: Big → Sub2 X ⇒ X:Real in Big hV ie nZ procedure Big is X : Real; procedure Sub1 is X : Integer; begin – of Sub1 end; – of Sub1 procedure Sub2 is begin – of Sub2 X end; – of Sub2 begin – of Big end; – of Big in m Example on Dynamic Scope https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e on The referencing environment of a statement is the collection of all names that are visible to the statement nZ In a static-scoped language, it is the local names plus all of the visible names in all of the enclosing scopes hV ie In a dynamic-scoped language, the referencing environment is the local bindings plus all visible bindings in all active subprograms in m Referencing Environment https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e nZ on var A, B, C: real; //1 procedure Sub1 (A: real); //2 var D: real; procedure Sub2 (C: real); var D: real; begin C:= C+B; end; begin Sub2(B); end; begin Sub1(A); end Function Main Sub1 ie Sub2 hV in m Example on Static-scoped Language https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope Referencing Environment A, B, C, Sub1 A, B, C, D, Sub1, Sub2 A, B, C, D, Sub1, Sub2 o3 o4 C o b c on o1 o2 hV ie nZ c void sub1() { int a, b; d } /* end of sub1 */ void sub2() { int b, c; sub1; } /* end of sub2 */ void main() { int c, d; sub2(); } /* end of main */ → sub2 → sub2 b c o5 o6 e main in m Example on Dynamic-scoped Language Frame main sub2 sub2 sub1 a b o7 o8 Referencing Environment c → o1, d → o2 b → o3, c → o4, d → o2 b → o5, c → o6, d → o2 a → o7, b → o8, c → o6, d → o2 https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn → sub1 Name, Binding and Scope C o e on Name nZ Binding Scope hV ie Referencing Environment in m Summary [1] https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e on hV ie nZ , Maurizio Gabbrielli and Simone Martini, Programming Languages: Principles and Paradigms, Chapter 4, Springer, 2010 in m References I https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope ... sub1 Name, Binding and Scope C o e on Name nZ Binding Scope hV ie Referencing Environment in m Summary [1] https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and. .. nZ Compilation time hV Runtime in m Binding Time https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o e Object - any entity in the program Binding. .. int x ; x = 1; } https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Name, Binding and Scope C o Static vs Dynamic on e Definition Scope of a binding is the textual region