งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Names, Scopes and Bindings

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Names, Scopes and Bindings"— ใบสำเนางานนำเสนอ:

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

2 Introduction Abstraction ของภาษา imperative อยู่ภายใต้สถาปัตยกรรมของเครื่องคอมพิวเตอร์ที่สร้างขึ้นโดย von Neumann ส่วนประกอบสำคัญมี 2 ส่วนคือ Memory Processor มี operation เปลี่ยนแปลง contents ของ memory Memory cells ของเครื่อง สำหรับภาษาคือ ?

3 Variable Variable ประกอบด้วยกลุ่มของ properties หรือ attributes เช่น Name, Type, Scope, Lifetime, Type checking, Initialization Name not all variables have them is a string of characters used to identify some entity in program Design issues for names: Maximum length? Are connector characters allowed? Are names case sensitive? Are special words reserved words or keywords?

4 Names Length ภาษายุคแรกใช้ 1 ตัวอักษร ซึ่งเพียงพอสำหรับคำนวณสมการทางคณิตศาสตร์ Language examples: FORTRAN I: maximum 6 COBOL: maximum 30 FORTRAN 90 and ANSI C: maximum 31 Ada and Java: no limit C++: no limit

5 Names Case sensitivity ชื่ออักษรตัวเล็กและตัวใหญ่แตกต่างกัน
C, C++, and Java names are case sensitive ใน C มีตัวแปร เช่น Rose, rose, ROSE Readability & Writability ใน Java มี method สำหรับเปลี่ยนจาก String เป็น integer เช่น parseInt

6 Names Special words เป็นการนำคำที่สอดคล้องกับการปฏิบัติงานมาใช้ในโปรแกรมเพื่อให้อ่านโปรแกรมได้ง่ายขึ้น Keyword คือคำที่ขึ้นอยู่กับ context เช่น ใน Fortran REAL APPLE REAL INTEGER REAL = INTEGER REAL Reserved word คือคำที่สงวนไว้ ไม่สามารถนำไปใช้สร้างเป็นชื่อได้ Predefined มีความหมายระหว่าง reserves word และ user-defined name printf ของภาษา C ?

7 Address คือ memory address ของตัวแปรนั้น (บางครั้งเรียก l-value)
ตัวแปร sum ของ sub1 อยู่ที่ address หมายเลข FF00 sub1(); sub1(); สมมุติว่าทั้ง sub1 และ sub2 มีตัวแปรชื่อเดียวกันคือ sum sub1() [FF00] sub2() [?] Alias หมายถึงตัวแปรตั้งแต่สองตัวมี address เหมือนกัน Aliases are harmful to readability (program readers must remember all of them)

8 Type ใช้สำหรับพิจารณาช่วงค่าที่ตัวแปรสามารถเป็นไปได้ และ operator ต่างๆที่สามารถทำงานได้ type size range operator byte integer double String

9 Value คือ content ของ memory cell ของตัวแปรนั้น byte b1 byte b2
integer i double d บางครั้งเรียก r-value 1 2 3 4 5 6 7 8 9

10 The Concept of Binding Binding คือความสัมพันธ์ เช่น
Entity กับ attribute หรือ Symbol กับ operation เวลาที่เกิด binding เรียกว่า binding time เช่น design time, compile time, link time, load time, run time พิจารณา statement ของภาษา C ต่อไปนี้ int count; count = count + 5; 1. int ถูกกำหนดช่วงของค่าที่เป็นไปได้ตอน ? 2. count ถูกกำหนด type ตอน ? 3. ค่าของ count ถูกกำหนดตอน ? 4. ความหมายของ + ใน statement ถูกกำหนดตอน ?

11 The Concept of Binding Possible binding times:
Language design time--e.g., bind operator symbols to operations Language implementation time--e.g., bind floating point type to a representation Compile time--e.g., bind a variable to a type in C or Java Load time--e.g., bind a FORTRAN 77 variable to a memory cell (or a C static variable) Runtime--e.g., bind a nonstatic local variable to a memory cell

12 The Concept of Binding Def: A binding is static if it first occurs before run time and remains unchanged throughout program execution. Def: A binding is dynamic if it first occurs during execution or can change during execution of the program.

13 The Concept of Binding Type Bindings มีวิธีการระบุ type อย่างไร ?
จะทำเมื่อไร ? ถ้าเป็นแบบ static การระบุควรจะเลือกแบบ explicit หรือ implicit declaration ?

14 The Concept of Binding Variable declaration
explicit declaration เป็นรูปแบบที่ใช้ statement กำหนด type ของตัวแปรอย่างชัดเจน implicit declaration ใช้ข้อกำหนดของการตั้งชื่อเพื่อระบุ type เช่น ในภาษา Fortran ถ้าชื่อขึ้นต้นด้วย I, J, K, L, M จะหมายถึง integer type FORTRAN, PL/I, BASIC, and Perl provide implicit declarations Advantage: writability Disadvantage: reliability (less trouble with Perl)

15 The Concept of Binding Dynamic Type Binding (JavaScript and PHP)
Specified through an assignment statement e.g., JavaScript list = [2, 4.33, 6, 8]; list = 17.3; Advantage: flexibility (generic program units) Disadvantages: High cost (dynamic type checking and interpretation) Type error detection by the compiler is difficult

16 The Concept of Binding Type Inference
นำ type ของ expression มาพิจารณา (โปรแกรมไม่จำเป็นต้องระบุ type ก็ได้) ML เป็นภาษาที่เป็นทั้ง functional และ imperative fun circumf(r) = * r * r; fun times10(x) = 10 * x; Fun square(x) = x * x;

17 The Concept of Binding Storage Bindings & Lifetime
Allocation - getting a cell from some pool of available cells Deallocation - putting a cell back into the pool sub1(){ int b; } sub2(){ int c; Main(){ int a; sub1(); sub2();

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


ดาวน์โหลด ppt Names, Scopes and Bindings

งานนำเสนอที่คล้ายกัน


Ads by Google