60
제 4 제 제제제제제 제제제 제제제 제제 제제 4.1 언언 언언 4.2 언언언언언 언언 언언 언언

제 4 장 프로그래밍 언어의 구문과 구현 기법

Embed Size (px)

DESCRIPTION

제 4 장 프로그래밍 언어의 구문과 구현 기법. 4.1 언어 구문. 4.2 프로그래밍 언어 구현 기법. 가상의. 가상의. Ada. 운영. 체제. C++. 컴퓨터. 명령어. 컴퓨터. Ada. 번역기. C++. 번역기. 가상의. 번역기. Assembly. 가상의. 운영 체제. 언어. Cobol. 컴퓨터. 어셈블러. 컴퓨터. Cobol. computer. 번역기. hardware. Lisp. 인터프리터. 가상의. Power. Lisp. 컴퓨터. Builder. - PowerPoint PPT Presentation

Citation preview

Page 1: 제  4 장 프로그래밍 언어의 구문과 구현 기법

제 4 장 프로그래밍 언어의 구문과 구현 기법

4.1 언어 구문

4.2 프로그래밍 언어 구현 기법

Page 2: 제  4 장 프로그래밍 언어의 구문과 구현 기법

가상 컴퓨터컴퓨터를 가상의 고급 언어 컴퓨터로 간주

computerhardware

운영 체제

PowerBuilder

인터프리터

Cobol번역기

운영 체제명령어번역기 C++

번역기

.......

Ada번역기

어셈블러

Lisp인터프리터

가상의Cobol컴퓨터

가상의 C++컴퓨터

가상의Ada컴퓨터

가상의Assembly언어컴퓨터

가상의PowerBuilder컴퓨터

가상의Lisp 컴퓨터

Page 3: 제  4 장 프로그래밍 언어의 구문과 구현 기법

프로그래밍 언어의 기본 문자 집합

4.1 언어 구문

구문 정의

알파벳 (A Z) 26∼ 개아라비아 (0 9) 10∼ 개특수 문자 (+, -, ,, …)

문자 코드 체계EBCDIC (Extended Binary Coded Decimal Interchange Code)

ASCII (American Standard Code for Information Interchange)

4.1.1 프로그래밍 언어의 어휘 구조

유니코드

Page 4: 제  4 장 프로그래밍 언어의 구문과 구현 기법

문자 또는 문자열에 대한 일반적 순서비트 조합 순서에 영향 받음

정합순서 (collecting sequence)

예 ) 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 A < B < C < ... < X < Y < Z

문자 -> 단어 -> 언어

예약어 (reserved word, key word)

프로그램 판독성 증가기호 테이블 빠른 탐색많은 예약어 기억하기 어려움중복여지가 프로그램 혼돈 우려

구문 정의

Page 5: 제  4 장 프로그래밍 언어의 구문과 구현 기법

G = ( VN, VT, P, S )

– VN : non-terminal Symbol 의 유한집합– VT : terminal Symbol 의 유한집합

• VN VT = V(Vocabulary) = VN VT

– P : 생성규칙의 유한집합• ( 유도 ) V+ V*

– S : start Symbol VN

형식언어의 문법 (Formal Grammar)

Page 6: 제  4 장 프로그래밍 언어의 구문과 구현 기법

예제 VN = {<sentence>,<subject>,<verb>,<object>,<noun>,<article>} VT = {The, Boy, Girl, Loves, .} S = <sentence> P : 1. <sentence> <subject><verb><object>. 2. <subject> <article><noun> 3. <object> <article><noun> 4. <verb> Loves 5. <article> The 6. <noun> Boy | Girl

derivation(replacement: 유도 )

<sentence> => <subject><verb><object>. => <article><noun><verb><object>. => The <noun><verb><object>. => The Boy <verb><object>. => The Boy Loves <object>. => The Boy Loves <article><noun>. => The Boy Loves The <noun>. => The Boy Loves The Girl.

Sentential Form

Sentence

Left most derivationTop-down 방식

Right most derivationBottom-up 방식

Page 7: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Chomsky 4 type Grammar– 생성 규칙의 형태에 따라 구분

0) Type 0(Unrestricted Grammar) • 생성 규칙에 어떤 제한도 두지 않는다 . ( 위축형 문법 )

예제 ) bc b

1) Type 1(Context-sensitive Grammar :csg)• 오른쪽이 왼쪽보다 같거나 긴 생성규칙

• || ≤ ||

예제 ) P : A abc A aABc

Bb BC CB cc bc c (X)

형식 언어의 문법의 분류

Page 8: 제  4 장 프로그래밍 언어의 구문과 구현 기법

2) Type 2 (Context-free Grammar:cfg)– 왼쪽은 하나의 nonterminal symbol 만 가능– : nonterminal V*

예제 ) L(G2) = {anban | n 1}

G2 = ({S, C}, {a, b}, P, S), P : S aCa C aCa C b

3) Type 3(Regular Grammar : rg : 정규문법 )– 왼쪽에 하나의 nonterminal symbol 이 오고 , 오른쪽도 최대한 하나의 nonteminal symbol 이거나 terminal symbol 로 구성

– A tB A t t Vt A,B VN (right linear regular grammar)

– A Bt A t t Vt A,B VN (left linear regular grammar)

예제 ) L(G3) = {anbam | n, m 1}

G3 =({S,B,C}, {a,b}, P, S), P : S aS S aB B bC C aC C a

*

*

Page 9: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Grammar Language Recongnizer

Type 0(unresticted) Recusively enumerableset

Turing machine

Type 1(context-sensitive) Context-sentivelanguage

Linear boundedautomata

Type 2(context-free) Context-free language Pushdown automata

Type 3(regular) Regular language Finite automata

언어의 포함관계

문법형태에 따른 언어와 인식기

Unrestricted 언어

Context-sensitive 언어

Context-free 언어

Regular 언어

Page 10: 제  4 장 프로그래밍 언어의 구문과 구현 기법

::= 정의하다< > nonterminal : 다시 정의될 대상| 택일

BNF (Backus-Naur Form) : context free grammar

4.1.2 문맥 자유 문법과 BNF

구문 정의

구문 (Syntax) 형식 정의 보편적 방법언어의 생성 규칙 정의메타 기호

Page 11: 제  4 장 프로그래밍 언어의 구문과 구현 기법

식별자 정의의 BNF 표기

<identifier> ::= <letter> |

<identifier><letter> |

<identifier><digit>

<letter> ::= A | B | C | … | X | Y | Z

<digit> ::= 0 | 1 | 2 | … | 8 | 9

구문 정의

Page 12: 제  4 장 프로그래밍 언어의 구문과 구현 기법

예 ) AB1C

<identifier> ::= <identifier><letter> ::= <identifier><digit><letter> ::=

<identifier><letter><digit><letter> ::= <letter><letter><digit><letter> ::= A <letter><digit><letter> ::= AB <digit><letter> ::= AB1 <letter> ::= AB1C

구문 정의

Page 13: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

EBNF (Extended Backus-Naur Form) 표기법

BNF 확장하여 읽기 쉽고 간단한 표기법

BNF 에 추가된 메타 기호

{} : 0 번 이상 반복 ( { }07 )

[] : 0 번 또는 1 번 선택

() : 택일 연산자

‘ ‘ : terminal 표시

Page 14: 제  4 장 프로그래밍 언어의 구문과 구현 기법

<id-name> ::= <alphabet> { <alphanumeric> }07

<alphanumeric> ::= <alphabet> | <digit> <alphabet> ::= A | B | C | … | Z <digit> ::= 0 | 1 | 2 | … | 9

구문 정의

EBNF 의 예

식별자

If - then - else

<if-statement> ::= if <condition> then<statement> [else<statement>]

Page 15: 제  4 장 프로그래밍 언어의 구문과 구현 기법

<expression> ::= <expression> + <expression> |

<expression> - <expression> | <expression> * <expression> | <expression> / <expression>

구문 정의

사칙 연산

BNF

<expression> ::= <expression>(+|-|*|/)<expression>

EBNF

Page 16: 제  4 장 프로그래밍 언어의 구문과 구현 기법

<BNF-rule> ::= <left-part> ‘::=‘ <right-part> <right-part> ::= <right-part-element>

{ ‘|’ <right-part-element> }

구문 정의

메타 기호가 terminal 기호 일 때

Page 17: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Subpascal 시작부에 대한 EBNF

<subpascal> ::=program <ident> ; <block> .<block> ::=[<const_dcl>][<var_dcl>]{<proc_dcl>} <compound-st><const_dcl> ::=const <ident> = <number>

{ ; <ident> = <number> } ;<var_dcl> ::=var <ident_list> : <type>

{ ; <ident_list> : <type> } ;<ident_list> ::=<ident> {,<ident>} <proc_dcl> ::=procedure <ident>['('<formal_param>')'] ; <block> ;<compound-st> ::=begin <statement> { ; <statement>} end

구문 정의

Page 18: 제  4 장 프로그래밍 언어의 구문과 구현 기법

4.1.3 구문 도표

구문 정의

구문 도표 (syntax diagram)

구문 도표는 그 형태가 순서도와 유사구문 구문 도표는 EBNF 와 일대일 대응

다시 정의될 대상은 네모칸으로 terminal 기호는 원이나 타원형으로 표시이들 사이는 지시선으로 연결

Page 19: 제  4 장 프로그래밍 언어의 구문과 구현 기법

① terminal x

② nonterminal B

X

B

구문 정의

구문 도표 그리는 방법

Page 20: 제  4 장 프로그래밍 언어의 구문과 구현 기법

④ A ::= α1 | α2 | ... | αn

③ A ::= X1X2 ...Xn

A X1 X2 Xn...

ⓐ Xi 가 nonterminal 인 경우

ⓑ Xi 가 terminal 인 경우

A ...x1 x2 xn

α2

αn

α1

... A

구문 정의

Page 21: 제  4 장 프로그래밍 언어의 구문과 구현 기법

α

α

A

A

α2

α1

A β

⑤ EBNF A ::= {α}

⑥ EBNF A ::= [α]

⑦ EBNF A ::= (α1 | α2)β

구문 정의

Page 22: 제  4 장 프로그래밍 언어의 구문과 구현 기법

– 예제

EBNF 구문도표A ::= x |’ (‘ B’ )’

B ::= AC

C ::= {+A}

( 조건 )

VN = { A, B, C }

VT = { +, x, (, ) }

x

B )(

A

A CB

A

C

+

x

)(

A

A +

A

구문 정의

Page 23: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

subpascalprogram ident ; block ·

const ident = number

;

var

procedure

begin

ident

ident

statement

type:

;

end

;

( formal-param )

;

; block

block

Subpascal 구문 도표

Page 24: 제  4 장 프로그래밍 언어의 구문과 구현 기법

4.1.4 파스 트리

구문 정의

파스 트리

원시 프로그램의 문법 검사 과정 중 생성 트리

구문 검사하는 검증 트리

한 표현이 BNF 에 의해 작성될 수 있는지 여부

Page 25: 제  4 장 프로그래밍 언어의 구문과 구현 기법

예 ) TEST1

구문 정의

<identifier> ::=<letter>| <identifier><letter> | <identifier><digit><letter> ::=A | B | C | ... | X | Y | Z<digit> ::=0 | 1 | 2 | ... | 8 | 9

<identifier>

<identifier> <digit>

<identifier> <letter>

<identifier> <letter>

<identifier> <letter>

<letter>

T E S T 1

Page 26: 제  4 장 프로그래밍 언어의 구문과 구현 기법

<exp> ::= <exp> - <exp> | <exp> * <exp> | (<exp>) | <number>

<number> ::= <number> <digit> | <digit><digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

<exp> ::= <exp> - <exp> | <exp> * <exp> | (<exp>) | <number>

<number> ::= <number> <digit> | <digit><digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

<exp>

<exp> <exp>

<exp><exp>

-<number>

<number>

<number><digit>

*

3

2 5

<digit>

<digit>

Page 27: 제  4 장 프로그래밍 언어의 구문과 구현 기법

<exp>

<exp> <exp>

<exp><exp>

-<number>

<number>

<number><digit>*

3

2 5

<digit>

<digit>

이 파스트리에서 불필요하게 표현되어 있는 비단말 기호들을 없애면 추상구문 (AST) 트리 혹은 구문트리(syntax tree) 가 된다 .

-

* 3

2 5

Page 28: 제  4 장 프로그래밍 언어의 구문과 구현 기법

4.1.5 모호성 , 결합성 및 우선순위

예 ) B33 생성 유도 과정

<identifier> <identifier><digit> <identifier> 3<identifier> <digit> 3 <identifier> 3 3 <letter>

3 3 B 3 3

<identifier> <identifier><digit> <identifier><digit><digit>

<letter> <digit> <digit> B <digit> <digit> B 3 <digit> B 3 3

Page 29: 제  4 장 프로그래밍 언어의 구문과 구현 기법

예 ) B33 의 파스트리

<identifier>

<identifier> <digit>

<identifier>

<letter>

<digit>

B

3

3

Page 30: 제  4 장 프로그래밍 언어의 구문과 구현 기법

<exp> <exp> - <exp> <exp> - <exp> * <exp> ( 두번째 <exp> 이 <exp> * <exp> 으로 대치 ) <number> - <exp> * <exp> ….

3 – 2 * 5

<exp> <exp> * <exp> <exp> - <exp> * <exp> ( 첫번째 <exp> 이 <exp> - <exp> 으로 대치 ) <number> - <exp> * <exp> ….

3 – 2 * 5

<exp> <exp> - <exp> <exp> - <exp> * <exp> ( 두번째 <exp> 이 <exp> * <exp> 으로 대치 ) <number> - <exp> * <exp> ….

3 – 2 * 5

<exp> <exp> * <exp> <exp> - <exp> * <exp> ( 첫번째 <exp> 이 <exp> - <exp> 으로 대치 ) <number> - <exp> * <exp> ….

3 – 2 * 5

예 ) 3 – 2 * 5 수식의 생성 유도 과정

Page 31: 제  4 장 프로그래밍 언어의 구문과 구현 기법

<exp>

<exp> <exp>*

3

2

5

<exp>

<exp> <exp> 3

2 5

예 ) 두가지 서로 다른 파스트리

-

- *

같은 문법에서 같은 스트림에 대하여 두가지 이상의 서로 다른 파스트리가 발생하면 이러한 문법은 모호(ambiguous) 하다고 한다 .

Page 32: 제  4 장 프로그래밍 언어의 구문과 구현 기법

예 ) Unambiguous 문법 ( 분명한 문법 )

수식 우선 순위의 문제 해결

<exp> ::= <exp> - <term> | <term>

<term> ::= <term> * <factor> | <factor>

<factor> ::= (<exp>) | <number>

<number> ::= <number> <digit> | <digit>

<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

<exp> ::= <exp> - <term> | <term>

<term> ::= <term> * <factor> | <factor>

<factor> ::= (<exp>) | <number>

<number> ::= <number> <digit> | <digit>

<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 33: 제  4 장 프로그래밍 언어의 구문과 구현 기법

4.1.6 구문과 프로그램 신뢰성

구문 정의

구문

프로그램의 신뢰성 증대 시키는데 큰 영향

Page 34: 제  4 장 프로그래밍 언어의 구문과 구현 기법

예 )

FORTRAN

DO 10 I = 2.6 A(I) = B + C(I)10 CONTINUE

DO 10 I = 2.6 A(I) = B + C(I)10 CONTINUE

2.6 의 오류 (. 대신 , 사용해야함 )DO10I 에 2.6 배정으로 인식

PL/I

A = B = C A = B = C다중배정문의 의미 (A 와 B 에 C 값 저장 )(B = C) 의 결과를 A 에 저장하는 문장으로 인식

구문 정의

Page 35: 제  4 장 프로그래밍 언어의 구문과 구현 기법

If cond1 then (if cond2 then s1 else s2)

If cond1 then (if cond2 then s1) else s2

Dangling ( 현수 ) else 문제중첩된 if 문에서 else 는 어떤 if 의 else 인가 ?

if <cond> then <s> if <cond> then <s> else <s>

if <cond> then <s> if <cond> then <s> else <s>

구문 정의

If cond1 then if cond2 then s1 else s2

Page 36: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

<s> ::= if <cond> then <s> | if <cond> then <s> else <s> | s1 | s2

<cond> ::= cond1 | cond2

<s> ::= if <cond> then <s> | if <cond> then <s> else <s> | s1 | s2

<cond> ::= cond1 | cond2

<s>

if <s><cond>then

If <cond> then <s> else <s>

cond2 s1 s2

cond1

<s>

If <cond> then <s> else <s>

if <s><cond>thencond1

cond2 s1 s2

① ②

Page 37: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

언어별 현수 else 문제의 해결 방안

Algol 60 :- ① 의 경우

if cond1 then begin if cond2 then s1 else s2 end- ② 의 경우

if cond1 then begin if cond2 s1 end else s2Algol 68 :

- ① 의 경우 if cond1 then if cond2 then s1 else s2 fi fi

- ② 의 경우 if cond1 then if cond2 s1 fi else s2 fi

PL/I :- ② 의 경우

IF cond1 THEN IF cond2 THEN s1; ELSE s2;또는IF cond1 THEN IF cond2 THEN s1; ELSE; ELSE s2;

Pascal :- ② 의 경우

if cond1 then begin if cond2 then s1 then else s2또는if cond1 then if cond2 then s1 else else s2

※ PL/I 과 파스칼에서 일반적으로 사용된 경우 ①의 경우로 해석됨 .

Algol 60 :- ① 의 경우

if cond1 then begin if cond2 then s1 else s2 end- ② 의 경우

if cond1 then begin if cond2 s1 end else s2Algol 68 :

- ① 의 경우 if cond1 then if cond2 then s1 else s2 fi fi

- ② 의 경우 if cond1 then if cond2 s1 fi else s2 fi

PL/I :- ② 의 경우

IF cond1 THEN IF cond2 THEN s1; ELSE s2;또는IF cond1 THEN IF cond2 THEN s1; ELSE; ELSE s2;

Pascal :- ② 의 경우

if cond1 then begin if cond2 then s1 then else s2또는if cond1 then if cond2 then s1 else else s2

※ PL/I 과 파스칼에서 일반적으로 사용된 경우 ①의 경우로 해석됨 .

Page 38: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

Program Compare(input, Output);Var

x, y, z : integer;Begin

z := 7;writeln(‘Type the each variables x, y, z ‘);write (‘ x= ‘);readln (x);write (‘ y= ‘);readln (y);if x > 0 then

if y > 5 thenz := x + y

elsez := x + y;

writeln(‘x = ‘, x, ‘ y = ‘, y, ‘ z= ‘, z)end.

Sample List (1-1) *PASCAL*

Page 39: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

Sample List (1-2) *C*#include <stdio.h>void main(){

int x, y, z = 7;printf(“ Type the each variables x, y, z\n”);printf(“x = “);scanf(“%d”, &x);printf(“y =“);scanf(“%d”, &y);

if (x > 0)if (y > 5)

z = x + y;else

z = 0;printf(“\n x = %d, y = %d, z = %d\n”, x, y, z);

}

Page 40: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Sample List (1-3) *JAVA*import java.io.*;

public class A { public static void main(String[] args) throws IOException { String strx; String stry; int x,y,z=7; BufferedReader in = new BufferedReader(

new InputStreamReader(System.in)); System.out.println("Type the each variables x, y"); System.out.print("x = "); strx = in.readLine(); x = Integer.parseInt(strx);j // x = new Integer(strx); // Java 5.0 이상 System.out.print("y = "); stry = in.readLine(); y = Integer.parseInt(stry); if (x > 0)

if (y > 5) z = x + y;else

z = 0; System.out.println("x = "+ x + ", y = " + y+", z = "+z); }}

Page 41: 제  4 장 프로그래밍 언어의 구문과 구현 기법

import java.io.*;

public class B { public static void main(String[] args) throws IOException { int x,y,z=7;

System.out.println("Type the each variables x, y"); x = System.in.read() - '0'; y = System.in.read() - '0'; if (x > 0)

if (y > 5) z = x + y;else z = 0;

System.out.println("x = "+ x + ", y = " + y+", z = "+z); }}

Sample List (1-4) *JAVA*

Page 42: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

Program Compare(input, Output);Var

x, y, z : integer;Begin

writeln(‘Type the each variables x, y, z ‘);write (‘ x= ‘);readln (x);write (‘ y= ‘);readln (y);if x > 0 then

if y > 5 thenz := x + y

else else z := 0;

writeln(‘x = ‘, x, ‘ y = ‘, y, ‘ z= ‘, z)end.

Sample List (2-1) *PASCAL*

Page 43: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

Sample List (2-2) *C*#include <stdio.h>void main(){

int x, y, z;z = 7;printf(“ Type the each variables x, y, z\n”);printf(“x = “);scanf(“%d”, &x);printf(“y =“);scanf(“%d”, &y);if (x > 0) {

if (y > 5)z = x + y;

elsez = 0; }

printf(“\n x = %d, y = %d, z = %d\n”, x, y, z);}

Page 44: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Sample List (2-3) *JAVA*import java.io.*;

public class A { public static void main(String[] args) throws IOException { String strx; String stry; int x,y,z=7; BufferedReader in = new BufferedReader(

new InputStreamReader(System.in)); System.out.println("Type the each variables x, y"); System.out.print("x = "); strx = in.readLine(); x = Integer.parseInt(strx); // x = new Integer(strx); // Java 5.0 이상 System.out.print("y = "); stry = in.readLine(); y = Integer.parseInt(stry); if (x > 0) {

if (y > 5) z = x + y;else

z = 0; } System.out.println("x = "+ x + ", y = " + y+", z = "+z); }}

Page 45: 제  4 장 프로그래밍 언어의 구문과 구현 기법

import java.io.*;

public class B { public static void main(String[] args) throws IOException { int x,y,z=7;

System.out.println("Type the each variables x, y"); x = System.in.read() - '0'; y = System.in.read() - '0'; if (x > 0) {

if (y > 5) z = x + y;else z = 0;

} System.out.println("x = "+ x + ", y = " + y+", z = "+z); }}

Sample List (2-4) *JAVA*

Page 46: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

Program Compare(input, Output);Var

x, y, z : integer;Begin

z := 7;writeln(‘Type the each variables x, y, z ‘);write (‘ x= ‘);readln (x);write (‘ y= ‘);readln (y);if x > 0 then begin

if y > 5 thenz := x + y

end else

z := 0;writeln(‘x = ‘, x, ‘ y = ‘, y, ‘ z= ‘, z)

end.

Sample List (3-1) *PASCAL*

Page 47: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구문 정의

Sample List (3-2) *C*#include <stdio.h>

void main(){

int x, y, z;z = 7;printf(“ Type the each variables x, y, z\n”);printf(“x = “);scanf(“%d”, &x);printf(“y =“);scanf(“%d”, &y);if (x > 0) {

if (y > 5)z = x + y;

}else z = 0; printf(“\n x = %d, y = %d, z = %d\n”, x, y, z);

}

Page 48: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Sample List (3-3) *JAVA*import java.io.*;

public class A { public static void main(String[] args) throws IOException { String strx; String stry; int x,y,z=7; BufferedReader in = new BufferedReader(

new InputStreamReader(System.in)); System.out.println("Type the each variables x, y"); System.out.print("x = "); strx = in.readLine(); x = Integer.parseInt(strx); // x = new Integer(strx); // Java 5.0 이상 System.out.print("y = "); stry = in.readLine(); y = Integer.parseInt(stry); if (x > 0) {

if (y > 5) z = x + y;

} else z = 0; System.out.println("x = "+ x + ", y = " + y+", z = "+z); }}

Page 49: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Sample List (3-3-1) *JAVA*import java.util.*;

public class ScanJava {public static void main(String[] args){

int x,y;int z = 0;Scanner scan = new Scanner(System.in);System.out.println("Type the each variables x,y");System.out.print("x= " );x = scan.nextInt();

System.out.print("y = " );y = scan.nextInt();if(x > 0)

if(y>5)z = x + y;

elsez = 0;

System.out.println("x = " + x + ", y = " + y + ", z = " + z);

}}

Page 50: 제  4 장 프로그래밍 언어의 구문과 구현 기법

import java.io.*;

public class B { public static void main(String[] args) throws IOException { int x,y,z=7;

System.out.println("Type the each variables x, y"); x = System.in.read() - '0'; y = System.in.read() - '0'; if (x > 0) {

if (y > 5) z = x + y;

} else z = 0; System.out.println("x = "+ x + ", y = " + y+", z = "+z); }}

Sample List (3-4) *JAVA*

Page 51: 제  4 장 프로그래밍 언어의 구문과 구현 기법

구현 방법

4.2 프로그래밍 언어 구현 기법

프로그래밍 언어 설계

번역 기법 (Translation)

인터프리터 기법 (Interpretation)

어떤 원시 (Source) 언어를 입력으로 목적 언어로 된 기능이 동등한 프로그램을 출력해 주는 언어 처리기

고급 언어로 된 프로그램을 읽어 각 문장을 디코딩하고 실행시키는 언어 처리기

Page 52: 제  4 장 프로그래밍 언어의 구문과 구현 기법

번역 기법 (Translation)

로 더 기계어LINKER

고급언어

컴파일러 목적모듈

컴파일러

컴파일러

어셈블리 언어

어셈블러

. . .. . .

. . .

원 시프로그램

목 적프로그램

입력 자료

실행 결과

로드모듈

고급언어

고급언어

목적모듈

목적모듈

목적모듈

translatortarget

( 목적 언어 ) source ( 원시 언어 )

프로그래밍 언어 설계

Page 53: 제  4 장 프로그래밍 언어의 구문과 구현 기법

번역기의 종류Preprocessor

원시 언어 : 고급 언어목적 언어 : 고급 언어

Compiler원시 언어 : 고급 언어목적 언어 : 저급 언어 ( 어셈블리 , 준 기계어 )

Assembler원시 언어 : 어셈블리 언어목적 언어 : 준기계어 언어

프로그래밍 언어 설계

Page 54: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Linkage Editor

원시 언어 : 여러 개의 프로그램목적 언어 : 로드 모듈 생성

( 실행 가능한 하나의 기계어 )

Loader

원시 언어 : 로드 모듈목적 언어 : 실행 가능한 기계어

(excutable code)

프로그래밍 언어 설계

Page 55: 제  4 장 프로그래밍 언어의 구문과 구현 기법

인터프리터 기법 (1)

고급 언어로 작성된 원시 프로그램

인터프리터

입력자료

실행 결과

프로그래밍 언어 설계

Page 56: 제  4 장 프로그래밍 언어의 구문과 구현 기법

인터프리터 기법 (2)

원시 프로그램

중간 형태 코드 입력자료

실행 결과

적당한 번역과정

인터프리터

프로그래밍 언어 설계

Page 57: 제  4 장 프로그래밍 언어의 구문과 구현 기법

Java

프로그램

출력

결과

Java

바이트

코드

바이트코드

컴파일러 (javac)

Java 가상기계의

바이트코드 인터프리터

Java 가상 기계가 작동하고 있는 실제 기계

< Java 바이트 코드 컴파일 >

Page 58: 제  4 장 프로그래밍 언어의 구문과 구현 기법

번역기 종류와 인터프리터

고급 언어 프로그램 컴파일러

어셈블러 프로그램 어셈블러

링 커(linker, linkage editor) 로드 모듈

로드 모듈 로 더(relocating loader) 실행 가능 기계어

고급 언어 프로그램 Preprocessor

원시 언어 (입력) 목적 언어 ( 출력 )소프트웨어 프로그램

인터프리터 실행된프로그램이

결과

원시 언어 ( 입력 ) 출 력

목적모듈 ( 준기계어 )

고급 언어 프로그램

소프트웨어 프로그램 ( 하드웨어 기능 )

기계어 , 준기계어 ( 목적 모듈 )

기계어 , 준기계어 ( 목적 모듈 )

고급 언어 프로그램

프로그래밍 언어 설계

Page 59: 제  4 장 프로그래밍 언어의 구문과 구현 기법

번역 기법과 인터프리터 기법 비교

컴파일러 언어

입력 프로그램과 동일한 의미의 목적 언어 프로그램 생성

FORTRAN, ALGOL, PL/I, PASCAL, COBOLC, Ada

번역기

장점 : 실행 시간 효율성 (efficiency)단점 : 큰 기억 장치 요구

프로그래밍 언어 설계

Page 60: 제  4 장 프로그래밍 언어의 구문과 구현 기법

인터프리터 언어

중간 언어를 생성 후 중간 언어로 작성된 프로그램을 번역 , 실행

LISP, SNOBOL, APL, PROLOG

인터프리터

장점 : 사용자의 적응성 (flexibility)

단점 : 실행 시 많은 디코딩 시간 요구

프로그래밍 언어 설계