While n n n n n x Var

  • Slides: 56
Download presentation

While 언어 n 정수 n n 변수 n n x Var 식 n n

While 언어 n 정수 n n 변수 n n x Var 식 n n n Int e Exp 문장 S → x : = e | skip | S 1 ; S 2 | if e then S 1 else S 2 | while b do S

상태 전이 n 기본 아이디어 n n 계산 과정을 (변수 값들의) 상태 전이 과정으로

상태 전이 n 기본 아이디어 n n 계산 과정을 (변수 값들의) 상태 전이 과정으로 기술한다. 상태 전이(State transition) n (S, s) s’ where n S is a statement n s is the initial state n s’ is the final state

상태 전이 시스템(State Transition System) n ( , T, ) where n Configuration =

상태 전이 시스템(State Transition System) n ( , T, ) where n Configuration = {(S, s) | S While, s State} State n State T n Transition relation {(S, s) | S While, s State} State

전이 관계(Transition Relation) (x : = a, s) s[x -> [[a]]s] n Assignment skip

전이 관계(Transition Relation) (x : = a, s) s[x -> [[a]]s] n Assignment skip sequence n if-then-else (S 1 , s) s’ if [[e]]s = true (if e then S 1 else S 2, s) s’ n n (skip, s) s (S 1 , s) s’, (S 2 , s’) s” (S 1; S 2, s) s” (S 2 , s) s’ (if e then S 1 else S 2, s) s’ if [[e]]s = false

전이 관계 n while (S, s) s’, (while e do S, s’) s” if

전이 관계 n while (S, s) s’, (while e do S, s’) s” if [[e]]s = true (while e do S, s) s” (while e do S, s) s if [[e]]s = false

Denotational Semantics y : = 1; while (x != 1) do (y : =

Denotational Semantics y : = 1; while (x != 1) do (y : = x*y; x : = x-1) n The program computes a function from states to states: n The final state will be equal to the initial state except that n n the value of x will be 1 and the value of y will be equal to the factorial of the value of x in the initail state.

Axiomatic Semantics y : = 1; while (x != 1) do (y : =

Axiomatic Semantics y : = 1; while (x != 1) do (y : = x*y; x : = x-1) n If x = n holds before the program is executed then y = n! will hold when the execution terminates. n Properties of programs are specified as assertions { P } S { Q } where n n n S is a statement P is the precondition and Q is the postcondition

Pre- and post conditions n 예제 { x=n } y: =1; { x=n and

Pre- and post conditions n 예제 { x=n } y: =1; { x=n and y=1} while (x !=1) do (y: =x *y; x : = x-1) { y=n! }

바인딩 시간 세분화 n 언어 정의 시간(Language definition time) n n boolean, true, false,

바인딩 시간 세분화 n 언어 정의 시간(Language definition time) n n boolean, true, false, char, integer, maxint 언어 구현 시간(Language implementation time) n integer, maxint n 컴파일 시간(Compile- time) n 링크/로드 시간(Link/load time) n n external definition/the location of a global variable 실행 시간(Runtime)

5. 3 선언, 블록 및 유효 범위 (Declarations, Blocks, and Scope)

5. 3 선언, 블록 및 유효 범위 (Declarations, Blocks, and Scope)

블록(Blocks) n Pascal n n C, C++, Java n n {. . . }

블록(Blocks) n Pascal n n C, C++, Java n n {. . . } Ada n n procedure or function declare. . . begin …. End ML n let … in … end

블록 구조 언어 (Block Structured Languages) n 블록의 중첩을 허용하는 언어 A B D

블록 구조 언어 (Block Structured Languages) n 블록의 중첩을 허용하는 언어 A B D C n Algol, Pascal, Modula, Ada, C, . . .

Ada 블록 declare x: integer; y: boolean; begin x : = 2; y :

Ada 블록 declare x: integer; y: boolean; begin x : = 2; y : = true; x : = x+1; … end

Pascal의 블록 program ex; (* main 프로그램 *) var x: integer; (* 전역 선언

Pascal의 블록 program ex; (* main 프로그램 *) var x: integer; (* 전역 선언 *) procedure p; (* 전역 선언 *) var y: boolean; (* p 내의 선언 *) begin if x = 2 then begin … end; begin (* main *) (* 선언할 수 없음 *) … end. (* 프로그램 *)

클래스 내의 선언 public class Int. With. Gcd { private int value; } /*

클래스 내의 선언 public class Int. With. Gcd { private int value; } /* value 필트 지역 선언 */ public int. Value() { return value; } /* int. Value 메쏘드 지역 선언 */ public int gcd(int v) {… } /* gcd 메쏘드 지역 선언 */

예제 void p( ) { char y; … } p y int x; x

예제 void p( ) { char y; … } p y int x; x void q( ) { double z; … } q z main() { int w[10]; … } main w

예: Ada 언어의 유효범위 규칙 n 핵심 아이디어 n n 선언의 유효범위는 선언된 블록

예: Ada 언어의 유효범위 규칙 n 핵심 아이디어 n n 선언의 유효범위는 선언된 블록 내 예제 B 1: declare x: integer; y: boolean; begin x : = 2; y : = false; B 2: declare a, b: integer; begin if y then a : = x; else b : = x; end if end B 2; … end B 1;

심볼 테이블 구현 n n Structure is a stack. Operations: n n n S_table

심볼 테이블 구현 n n Structure is a stack. Operations: n n n S_table S_empty(void) | Make a new S_table S_enter(S_table t, S_symbol sym, void *value) Push sym and its info. on the symbol table t. S_look(S_table t, S_symbol sym) Search the symbol table from the top, looking for sym. Return first sym found; NULL if none. S_remove(S_table t) | Pop the stack Searching the stack from the top n n n guarantees the most recently added (i. e. , most closely nested) definition is found. Pushing hides old definitions; popping exposes old definitions.

함수(메쏘드) 중복정의 n 함수 중복 정의 int max(int x, int y) { return x

함수(메쏘드) 중복정의 n 함수 중복 정의 int max(int x, int y) { return x > y ? x : y; } // max #1 double max(double x, double y) // max #2 { return x > y ? x : y; } int max(int x, int y, int z) // max #3 { return x > y ? (x > z ? x : z) : (y > z ? y : z); } n 함수 호출 max(2, 3); max(2. 1, 3. 0); max(2. 1, 3); max(2, 4, 3);

연산자 중복정의 예 #include <iostream> using namespace std; typedef struct { int i; double

연산자 중복정의 예 #include <iostream> using namespace std; typedef struct { int i; double d; } Int. Double; bool operator < (Int. Double x, Int. Double y) { return x. i < y. i && x. d < y. d; } Int. Double operator + (Int. Double x, Int. Double y) { Int. Double z; z. i = x. i + y. i; z. d = x. d = y. d; return z; } int main() { Int. Double x = {1, 2. 1}, y = {5, 3. 4}; if (x <y) x = x + y; else y = x + y; cout << x. i << “ “ << x. d << endl; return 0; }

메모리 할당 예 n FORTRAN n n LISP n n all locations are bound

메모리 할당 예 n FORTRAN n n LISP n n all locations are bound statically all locations are bound dynamically Pascal, C, Modula-2, Java n some allocation statically, others dynamically

메모리 할당 n 전역 변수(global variables) n n 지역 변수(local variables) n n static

메모리 할당 n 전역 변수(global variables) n n 지역 변수(local variables) n n static allocation at compile-time dynamic allocation on runtime stack when execution reaches the block 동적 메모리 할당(dynamic memory allocation) n n malloc() in C, new() in Pascal, Java dynamic allocation on heap when executing the function

메모리 배치 static(global) area runtime stack (unallocated) heap

메모리 배치 static(global) area runtime stack (unallocated) heap