전공/프로그래밍언어론

Name, Bindings, and Scope(1) - Name, Bindings

nongdamgom 2023. 12. 28. 12:42

Chapter 5. Name, Bindings, and Scope

Introduction

  • 이 장은 PL에서 사용하는 변수에 관한 기본적인 의미적 논점에 대해 이야기함

Imperative language are abstractions of von Neumann architecture

  • 폰노이만 구조의 두 가지 주요 요소
  • Memory (instruction & data 저장)
  • Processor(memory 내용을 변경하기 위한 연산을 제공)

Variables are characterized by attributes

  • 변수들은 여러 속성들의 모임으로 정의 됨
  • scope, lifetime, type checking, initialization, type compatibility

Variables - an abstraction of a memory cell

  • 6가지 속성으로 특정지어질 수 있다.
  • name, address, value, type, lifetime, scope

Name

  • 변수의 가장 기본적인 속성
  • identifier(식별자)라는 용어와 혼용돼서 사용되기도 함
  • 모든 변수가 이름을 갖는 것은 아님(없을 수도 있다)

Design issues for names:

  • Are names case sensitive?

--> upper/lower 구분이 되는지 ex) NAME? name? NAme?

  • Are special words reserved words or keywords?

--> ex) int, char, print...

--> PL에서 이미 지정해놓고 쓰는 word들에 대한 것

Name forms

Length

  • 너무 짧으면 정보를 담을 수 없다. ex)a, b, ...
  • C99 : 길이 제한 x, but 내부 이름은 63개, 외부 이름은 31개까지만 의미가 있음
  • C#, java : 길이 제한 x, 전부 의미를 가짐
  • C++ : 길이 제한 x, but 종종 구현자들이 자체적으로 길이를 제한함

Special characters(특수 문자)

  • PHP : 모든 변수는 $ 로 시작되어야 함
  • Perl : 모든 변수는 그 변수의 타입을 특정할 수 있는 특수문자로 시작해야 함
  • Ruby : instance 변수는 @로, class 변수는 @@로 시작해야 함

Case sensitivity

  • 단점 : readability : 특정 대소문자 사용을 다 기억해야함
  • C 기반 언어들이 보통 대소문자가 구분이 됨

Special words

  • 목적 : readability
  • special word -> reserved word(재정의 불가) or keyword(재정의 가능)
  • reserved word가 많으면 이름 구성하는데 힘들다
  • COBOL -> 예약어가 300개나 됨

Address

  • the memory address with which the variable is associated
  • 만약 한 변수가 서로 다른 실행시간 or 다른 장소에서 호출 되면, 각각 다 다른 주소를 갖게 될
  • 동일 메모리 주소에 여러개의 변수 사용 가능 => alias

Type

  • 변수가 저장할 수 있는 값들의 범위 + 그 타입 값들에 대해 정의되는 연산의 집합

Value

  • 그 변수에 연관된 memory cell or cell의 내용
  • l - value : 그 변수의 주소값
  • r - value : 그 변수 실제 할당된 값

** Abstract memory cell

  • the physical cell or collection of cells associated with a variable

(cell 하나가 너무 작을 때, 여기에 다 담지 못하고 더 큰 cell에 담아야함.

그러니까 이 변수에 관련된 모든 cell을 abstract memory cell로 명명하겠다는 뜻)

Binding

  • association between an entity and an attribute
  • 변수와 그 변수 타입 혹은 값 사이의 관계
  • 연산과 기호사이의 관계
  • Binding time - binding이 일어나는 시기

Possible Binding Time

not computational

  • * 라는 기호의 의미 -> 언어 설계할 때 '곱셈'이라는 의미 binding
  • int, float .. -> 언어 구현할 때 가능한 값들의 범위 binding
  1. Language design time
  2. Language implementation time

-----------

computaional

  1. Compile time - bind a variable to a type in C or Java
  2. Load time - bind a C or C++ static variable to a memory cell
  3. Runtime - bind a nonstatic local variable to a memory cell

Static binding (global)

  • 처음 일어나는게 run time 전이고
  • 실행 도중 변하지 않는 binding

Dynamic Binding (local)

  • 실행 중에 생기거나, 혹은 실행중에 변하는 binding

Type Binding

  • How is a type specified?
  • When does the binding take place?
  • type은 explicit or implicit으로 statically 하게 지정 될 수 있다.

Explicit / Implicit Declaration

  • explicit : int Age = 20;
  • implicit : 위에처럼 int 뭐 = 뭐 이렇게 안하고 그냥 각 언어의 default conventions에 따라서 변수에 타입을 연관시킴
  • explicit , implicit 둘 다 type에 대한 static binding
  • Basic, Perl, Ruby, JavaScript, PHP -> implicit
  • implicit 장점 : writability(규칙이 적어서 쓰기 쉽다)
  • 단점 : 아무래도 읽기가 어렵다
  • type inference - implicit declaration에서 문맥적으로 type을 추론
  • ex) In C# : var Age = 20; (int) , var name = "p2"(stinrg)
  • Visual Basic 9.0+, ML, Haskell, F# 도 type inference 사용

Dynamic Type Binding

  • JavaScript, Python, Ruby, PHP, C#(limited)
  • 장점 : flexibility
  • 단점 : high cost, complier가 type error를 잡기가 어려움

Storage Bindings & Lifetime

allocation - 어떤 pool에서부터 memory cell을 가져옴

deallocation - 가져온걸 다시 pool로 반환

lifetime - memory cell을 가져와서 binding 한 후 부터 다시 반환될 때까지의 시간

Categories of Variables by lifetime

** scalar 변수 == type 관계 없이 그냥 단일 값을 갖는 변수

  • Static variable
  • Stack-dynamic variable
  • explicit heap-dynamic variable
  • implicit heap-dynamic variable

Static variable

  • bound to memory cells before execution begins and remains bound to the same memory cell throughout execution
  • ex) C, C++ static 이라는 specifier 존재
  • 장점 : efficiency, fast (direct addressing), history-sensitive(동일 기억장소 유지)
  • 단점 : lack of flexibility(no recursion)

Stack - dynamic variable

** elaboration : 어떤 statement에 의해서 일어나는 메모리 할당과 binding 과정, 실행시간 중에 일어남

  • statemete가 elaborate 될 때 binding이 생성 but type binding은 static인 변수
  • c++이나 java에서는 변수의 선언은 아무데서나 해도 되고, 그 elaboration은 어떤 함수나 메소드가 호출 됐을 때 일어난다.
  • 어떤 함수에서 지역변수를 선언하고 사용하면 그 함수 내에 있는 지역변수들은 그 함수가 호출될 때 같이 기억공간에 binding 된다는 의미 인듯
  • 그 함수가 실행 종료시 같이 lifetime이 끝난다
  • scalar 변수의 경우, address를 제외한 모든 속성은 static binding(실제 주소값은 runtime에 결정된다)
  • 장점 : allows recursion(subprogram들이 메모리 공간을 공유함)
  • 단점 : 할당/해제에 따른 overhead, not be history sensitive, inefficient reference(indirect addressing)

Explicit heap - dynamic variable

  • 할당/해제가 명시적임 - 프로그래머가 new / delete 한 순간에 사용/반환
  • 오직 pointer or reference를 이용해서 참조
  • pointer 변수가 stack에 참조할 주소값을 가지고, 그 주소의 heap 영역을 참조
  • 장점 : 동적 장소 관리가 제공됨
  • 단점 : inefficient and unreliable

Implicit heap - dynamic variable

  • 할당/해제가 statement에 의해 전부 일어남
  • 변수의 모든 속성들이 문장에 의해서 바로 binding (explicit 처럼 new/delete 하는게 x)
  • 장점 : flexibility
  • 단점 : 모든 속성이 다 dynamic이라 느리고, 에러 감지가 어렵다