1 / 12

Names, Scopes and Bindings

Names, Scopes and Bindings. ธนวัฒน์ แซ่เอียบ. The Concept of Binding. Categories of variables by lifetimes Static bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. e.g. all FORTRAN 77 variables, C static variables

lydiaj
Download Presentation

Names, Scopes and Bindings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Names, Scopes and Bindings ธนวัฒน์ แซ่เอียบ

  2. The Concept of Binding • Categories of variables by lifetimes • Static • bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. e.g. all FORTRAN 77 variables, C static variables • Advantages: efficiency (direct addressing), history-sensitive sub program support • Disadvantage: lack of flexibility (no recursion)

  3. The Concept of Binding • Categories of variables by lifetimes • Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. • If scalar, all attributes except address are statically bound • e.g. local variables in C and Java • Advantage: allows recursion; conserves storage • Disadvantages: • Overhead of allocation and deallocation • Subprograms cannot be history sensitive • Inefficient references (indirect addressing)

  4. The Concept of Binding • Categories of variables by lifetimes • Explicit heap-dynamic--Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution • Referenced only through pointers or references e.g. dynamic objects in C++ and all objects in Java • Advantage: provides for dynamic storage management • Disadvantage: inefficient and unreliable

  5. Scope • สิ่งสำคัญในการทำความเข้าใจตัวแปร • Def: The scope of a variable • ช่วงของ statement ที่สามารถมองเห็นตัวแปร • Def: The local variables • ตัวแปรที่มีการประกาศใน subprogram หรือ block • Def: The nonlocal variables • ตัวแปรที่สามารถมองเห็นภายใน subprogram หรือ block แต่ไม่ได้ประกาศที่นั่น

  6. Static Scope • ภาษา ALGOL60 เป็นภาษาแรกที่นำเสนอ scope สำหรับตัวแปรที่เป็นแบบ nonlocal แบบนี้ (ภาษา imperative อื่นได้นำแนวคิดไปใช้ด้วย) • ตัวแปรถูกกำหนด scope ก่อนการปฏิบัติงานอย่างชัดเจน • ตัวอย่าง subprogram ของภาษา Pascal

  7. Static Scope procedure big; var x : integer; procedure sub1; begin … x … end; procedure sub2; var x : integer; begin … end; begin … end; Static parent hidden

  8. Block • ภาษาแรกที่ใช้คือ ALGOL60 • เป็นรูปแบบ static scope ที่ทำให้ส่วนหนึ่งของโค๊ดเป็นเจ้าของตัวแปรได้ • ตัวแปรจะมีพื้นที่ในหน่วยความจำเมื่อเข้าสู่ section และคืนพื้นที่เมื่อออกจาก section (เรียก section นี้ว่า block) if (i<j){ int temp; temp = i; i = j; j = temp }

  9. Block #include <stdio.h> int i = 0; int main() { printf("%d ", i); { int i = 1; printf("%d ", i); { int i = 2; printf("%d ", i); i++; printf("%d ", i); } printf("%d ", i); } printf("%d\n", i); }

  10. Dynamic scoping • ภาษาที่เป็นแบบนี้เช่น APL, SNOBOL4, LISP • Scope เกิดขึ้นจากลำดับการเรียก (calling sequence) ของ subprogram • procedure big; • var x : integer; • procedure sub1; • begin • … x … • end; • procedure sub2; • var x : integer; • begin • … • end; • begin • … • end; สมมุติเป็นแบบ dynamic scoping ถ้า big เรียก sub2 และ sub2 เรียก sub1 ใน sub1 อ้างถึง x ?

  11. Scope and lifetime • Lifetime ของตัวแปรอาจจะพิจารณาจาก scope ของตัวแปรนั้นได้ if (i<j){ int temp; temp = i; i = j; j = temp } void printheader(){ … } void compute(){ int sum; … printheader(); }

  12. ที่มา • Concepts of programming languages : Sebesta, Robert W.

More Related