Computers/Programming Language

ch5. Names, bindings, type checking, and scope

emzei 2012. 3. 16. 14:14


▷ Intro

Imperative programming languages are abstraction of the underlying von Neumann computer architecture.

- memory for storing instruction and data

- process for operations

* Variable in Von Neumann architecture


▷ Names ( := identifier )

   : associated with variable, labels, subprograms, keywords, etc.

♠ Design Issue

- max length of a name?

- connector characters can be used?

- case-sensitive?

- special words like reserved word or keyword?


♠ Name forms

▶ Name : sting of characters

- 초기 PL : single-character name

- FORTRAN 77 : 6 character

- FORTRAN 90 and C : up to 31 character, case-sensitive(C,C++,java)


▶ Connector character('_':under-score)

Pascal, Modula-2, and FORTRAN77 do not allow but most other contemporary language do 


▶ Case Sensitive?

- C, C++,and Java : recognize difference

- a serious detriment to readability

( ※ same name should have the same meaning)

- readability vs. writability


♠ Special words

▶ Keyword : word of PL that is special only in certain contexts

▶ Reserved word : special word of PL that can not be used as a name



 Variables 

: nothing more than an abstraction of a computer memory cell or collection of cells.

<6가지 속성>

♠ name

♠ address : memory address with which it is associated

▶ same as : L-value (주소값이 주로 할당문의 왼쪽에 나타나니까)

▶ Aliases : 하나 이상의 변수이름이 단일 메모리 주소를 접근할 때.

♠ Type

▶ determines the range of values / the set of operation

♠ Value : contents of the memory cell or cells associated with the variable 

▶ physical cell vs. abstract cell

ex. 물리적으로 4바이트 차지하지만, 우리는 추상적인 하나의 메모리 칸을 채운다고 생각하지!

▶ same as : R-value (할당문에서 오른쪽 편에 존재하니까)


♠ Lifetime

♠ Scope


▷ The concept of binding

♠  ★ binding : an association, such as between an attribute and an entity or between an operation and a symbol

♠ ★ binding time : the time at which a binding take places

♠ take place at design time, language implementation time, compile time, link time, load time, or run time.


♠ Binding of Attributes to Variables (속성 결정)

▶ ★ static binding : occur before run time, remains unchanged

▶ ★ dynamic binding : occur during run time, can change during execution time


 Type Binding : binding between variable name and type

   ( ☆ how the type is specified / when the binding takes places )


!!!두 가지 구분!!!

▶ ★  Static type binding

  - explicit declaration : type을 명시

: most of the language after mid-1960 require

  - implicit declaration : 선언부가 따로 없이 기본 관습대로 디폴트가 정해짐. 이름이 처음 등장할 때가 선언부가 됨.

: in FORTRAN, the identifier beginning with I,J,K,L,M,and N is implicitly declared to be integer

: readability를 떨어뜨릴 수 있음


▶  Dynamic type binding : 값을 할당받을 때 type이 결정됨

: variable is bound to a type when it is assigned to a value in an assignment statement

: APL, SNOBOL4, JavaScript,PHP ~ dynamic type binding

: flexibility가 높아짐.


◇ Static type binding or dynamic type binding

◇ Type inferences in functional languages


♠ Storage Bindings and Lifetime

    => Binding between a variable name and memory cell.

 

♠ Lifetime : the time during which the variable is bound to a specific memory location.


♠ Run time Storage *** activation record


<4 categories of variables)

▣ static :

those that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminate.

예) global variables, or static in C

장점) efficiency

단점) flexibility ~ cannot support recursion 


 static-dynamic :

those whose storage bindings are created when their declaration statements are elaborated, but whose type are statically bound.

예) local variable in a subprogram, or a block

※ elaboration of such a declaration: the storage allocation and binding process indicated by the declaration, which takes place when execution reaches the code to which the declaration is attached.

⊙ the storage for the variables in the declaration section is allocated at elaboration time and deallocated when the procedure control to its caller.

⊙ These variables are allocated from the run-time stack.


▣ explicit-heap dynamic :

nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions written by the programmer.

예) new in C++, malloc() in C

⊙ these variables, which are allocated from and deallocated to the heap, can be referenced through pointer variables.


▣ implicit-heap dynamic :

bound to help storage only when they are assigned values.

예) string and array in Perl and Javascript

장점) flexibility가 높다

단점) run-time overhead of maintaining all the dynamic attributes

        the loss of some error detection by compiler


(9판에는 type checking/strong typing/type equivalence 없음)

▷ Type checking : the activity of ensuring that the operands of an operator are of compatible types.

♠ compatible type : one that either is legal for the operator or is allowed under language rules to be implicitly converted by compiler-generated code ( or the interpreter) to a legal type. ~ this automatic conversion :coercion.

♠ type error ; the application of an operator to an operand of an inappropriate type.

▷ Strong Typing : one of the new ideas in language design that became prominent in the so-called structured-programming revolution of the 1970s is strong typing.

▷ Type Equivalence : two variables have equivalent types if their types have identical structure.


▷ Scope : the range of statements in which the variable is visible.

♠ non-local variable (비지역변수) :

  - those that are visible within the program unit or block but are not declared there.


♠ static scoping and dynamic scoping

   ▣ Static scope :

  - the scope of a variable can be statically determined, that is, prior to execution.

  - When a reference to a variable is found, the attribute of the variable are determined by finding its declaration.


◆ static scoping rule

- when a variable x is referenced,

> try to find the declaration of x in the subprogram including the reference of x.

> If not found, the search continue in the declaration of static parent.

> If not found there, the search continue to the next larger enclosing unit up to main.


♥ Evaluation of static scoping

   -> problems

- too much data access is a closely related problem

- difficult to modify the scope if required


   ▣ Dynamic scope : 

based on the calling sequence of subprograms, not on their spatial relationship to each other.

- the scope can be determined only at run time


◆ dynamic scoping rule

- when a reference to x is made, the correct meaning of  x  can be determined as follow,

> try to find it at the local declaration

> If failed, the declaration of the dynamic parent, or calling procedure, are searched

> If failed either, the search continue in that procedure's dynamic parent up to main.

* calling sequence


 

♥ Evaluation of dynamic scoping

   -> advantage

- subprogram inherit the context of their callers

   -> problems

- there is no way to protect local variables from unconditional accessibility

- the inability to statically type check reference to nonlocals

 


¿ static chain은 함수가 호출될 때 호출하는 쪽에서 생성. 프로그래머가 고급언어로 작성하여 생성됨.

¿ dynamic chain은 프로그램이 실행되면서 만듬.

¿ C는 함수가 activate 될 때, 블록 내의 변수까지 한꺼번에 생성


◎ Hidden scope 

 ※ hidden variable ?

◎ Block : a section of code. (..ALGOL계열)

  - (Declaration, statements)

  - unit of name scoping

  - unit of storage allocation

  - different from the compound statement


▷ Scope and Lifetime

- scope

- lifetime

- extern : scope를 바꿈

- static : scope는 바꾸지 않음. lifetime 바뀜.


▷ Referencing Environment

- the collection of all names that are visible in the statement.

> the variables declared in its local scope plus the collection of all variables of its ancestor scopes that are visible.

* forward reference.

▷ Named Constants

- C에서는 constant로 선언하는 기능이 없음. const를 이용하여 변수에다가 상수적 속성을 부여해줄 뿐. enum은 정수형 상수로 만드는 기능이 있음.

♠ Variable Initialization

     - Initialization keyword

DATA  ( FORTRAN )

VALUE ( COBOL ) 

initial ( PL/1 ) 

=  ( C ) 


 * 전역 ~ before runtime / 지역 ~ during runtime


'Computers > Programming Language' 카테고리의 다른 글

ch6-2. Data Type  (0) 2012.03.19
ch6-1. Data Type  (0) 2012.03.17
ch4. Lexical and syntax analysis  (0) 2012.03.15
ch3. describe syntax and semantics  (0) 2012.03.14
ch1. preliminaries  (0) 2012.03.13