programming languages concepts and...

24
PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATION 경인교대 컴퓨터교육과 이수정 교수 참조: Computer Science – an overview, Ed 7, J. Glenn Brookshear, Addison Wesley

Upload: others

Post on 31-Dec-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

PROGRAMMING LANGUAGES –

CONCEPTS AND TRANSLATION

경인교대 컴퓨터교육과

이수정 교수

참조: Computer Science – an overview, Ed 7, J. Glenn Brookshear,

Addison Wesley

Page 2: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

2/24

CONTENTS

Traditional Programming Concepts

Language Implementation - Program Translation

2/40

Page 3: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

3/24

문장의 종류

1) 선언문 (declarative statement): 변수 이름 정의.

int WeightLimit;

char strcharA, strcharB;

2) 명령문 (Imperative statement): 문장 서술.

Feranheit = Centigrade * 5 / 9 + 32;

if (Centigrade > 30) printf(“Hot weather”);

3) 주석(Comment): 설명문.

A program normally begins with declarative statements, followed by imperative statements. 3/40

program

The first part consists of declaration statements describing the data that is manipulated by the program.

The second part consists of imperative statements describing the action to be performed.

Page 4: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

4/24

VARIABLES AND DATA TYPES

변수 (variable, identifier):

메모리의 특정지역에 부과한 이름 (변수 이름).

Declarative 문장(선언문)으로 정의.

데이터 타입과 연관되어 있음.

Data type:

data의 encoding 방식과 허용된 operation 을 의미

integer, float, character, boolean.....

예) int WeightLimit;

int Height, Width;

int WeightLimit = 100; 4/40

Page 5: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

5/24

VARIABLES AND DATA TYPES

5/40

Language Example

Pascal var

Length, Width: real;

Price, Tax, Total: integer;

Symbol: char;

C, C++, C#, Java float Length, Width;

int Price, Tax, Total;

char Symbol;

FORTRAN REAL Length, Width;

INTEGER Price, Tax, Total;

CHARACTER Symbol;

Variable declarations

Page 6: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

6/37

상수와 리터럴(LITERAL)

상수(constant)

변경되지 않는 특정 값에 부여된 이름 예) const int AirportAlt = 645; EffectiveAlt Altimeter + 645 EffectiveAlt Altimeter + AirportAlt

리터럴:

숫자 또는 문자열 값 (텍스트 리터럴) 자체 4040 (숫자), “Lee”(문자열값) 비교예: LastName “Smith” LastName Smith

“4040”과 4040의 차이는? int a = 4040+238 int a = “4040”+238 (wrong!!)

Page 7: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

7/24

DATA STRUCTURE

Homogeneous array

a block of values of the same type

one-dimensional, two-dimensional , …

ex) int Scores[2][9]; (C 언어)

INTEGER Scores(2, 9) (FORTRAN)

Scores: array [3..5, 12..20] of integer; (Pascal)

7/40

Scores(2, 4) in

FORTRAN; indices

start at one.

Scores[1][3] in C;

indices start at zero.

Page 8: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

8/24

DATA STRUCTURE

Heterogeneous array

a block of data where different elements can have different

types

8/40

Name Age SkillRating

< Employee >

• The array declaration in Pascal

var

Employee: record

Name: packed array[1..8] of char;

Age: integer;

SkillRating: real

end

• The array declaration in C

struct{

char Name[8];

int Age;

float SkillRating;

} Employee;

Page 9: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

9/24

ASSIGNMENT STATEMENTS (배정문/할당문)

변수(특정 메모리 영역)에 값을 배정

변수, 배정 연산 기호, 배정될 값/수식의 순으로 작성

예) Z = X + Y; (C, C++, C#, Java)

Z := X + Y; (Ada)

Z X + Y (APL)

연산자 우선순위 (operator precedence): ( ), 곱셈/나눗셈, 덧/뺄셈

중복 정의 (overloading):

하나의 연산자가 여러 연산 의미

Java의 예: 3+5 (덧셈), “abc”+”def” (concatenation)

9/40

Page 10: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

10/24

CONTROL STATEMENTS (제어문)

실행순서를 변경하는 명령문

goto, 기계어의 jump, for, while, if-else

구조적 프로그래밍(structured programming): 제어문의 사용을 적절히

하여 조직적 설계

goto 40

20 Apply procedure X

goto 70

40 if (A < B) then goto 60

goto 20

60 Apply procedure Y

70 …

if (A < B) then apply procedure Y

else (apply procedure X)

10/40

Page 11: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

11/24

CONTROL STATEMENTS (제어문)

실행순서를 변경하는 명령문

11/40

<출처: Computer Science – an overview, Ed 7, J. Glenn Brookshear, Addison Wesley>

Page 12: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

12/24

CONTROL STATEMENTS

for-structure

similar to while-statement

All the initialization,

modification, and

termination are

incorporated into a single

statement.

12/40

<출처: Computer Science – an overview, Ed 7, J. Glenn Brookshear, Addison Wesley>

Page 13: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

13/24

COMMENTS

프로그램에 대한 설명문

컴파일러는 주석을 무시함

두 가지 삽입 방법:

시작과 끝부분에 특별한 기호 (여러 줄에 걸친 설명문 작성 시)

시작 부분에만 특별한 기호 (한 줄의 설명문)

예) C++, C#, C, Java에서,

/* This is a

comment. */

// This is a comment

13/40

Page 14: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

LANGUAGE IMPLEMENTATION

- PROGRAM TRANSLATION

Page 15: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

15/24

COMPILER

A computer program to convert the program from our form to a form the computer can read and execute (machine code).

Machine code is a string of 1's and 0's.

The original C program ≡ "source code",

The resulting compiled code ≡ "object file".

15/40

Page 16: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

16/24

LINKER

Produces the final complete file that can be executed by the

computer (“.exe” file).

Combines one or more object files and predefined libraries.

Library: a collection of pre-compiled "object code" that provides

frequently-used operations.

ex) “sqrt”, “sort”, “printf”….

A good compiler provides not only a compiler, but an editor, a

debugger, a library, a linker, online documentation, help files,

and a tutorial. 16/40

Page 17: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

17/24

TRANSLATION PROCESS (COMPILE)

Lexical analysis (어휘 분석기):

소스(source) 프로그램에서 토큰(token)이라는 기호열 분류

토큰: 숫자, 단어, 연산자 등

주석은 무시함

17/40

Source

program

Lexical

analyzer

Parser

Code

generator

Object

program

Page 18: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

18/24

TRANSLATION PROCESS

Parser (Syntax Analyzer, 구문 분석기 ):

토큰들을 묶어 문장으로 그룹화.

구문 규칙(grammar)에 의거하여 문법의 적합성 판단

구문 다이어그램(Syntax Diagram)

구문 규칙을 그림으로 표현

비종결자(nonterminal)와 종결자(terminal)를 포함하는 다이어그램 18/40

Source

program

Lexical

analyzer

Parser

Code

generator

Object

program

Page 19: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

19/24

TRANSLATION PROCESS

syntax diagram:

terminals: keywords

nonterminals: usually require further description

19/40

if then else Boolean

expression statement statement

Page 20: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

20/24

TRANSLATION PROCESS

20/40

<출처: Computer Science – an overview, Ed 7, J. Glenn Brookshear, Addison Wesley>

Page 21: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

21/24

TRANSLATION PROCESS

parse tree a pictorial form that

represents the manner in

which a particular string

conforms to a set of syntax

diagrams.

x + y X z 의 parse tree

21/40

<출처: Computer Science – an overview, Ed 7, J. Glenn Brookshear, Addison Wesley>

Page 22: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

22/24

TRANSLATION PROCESS

parsing a program = constructing a parse tree for the source program

The syntax rules must not allow ambiguities:

two distinct parse trees for one string.

if B1 then if B2 then S1 else S2

<출처: Computer Science – an overview, Ed 7, J. Glenn Brookshear, Addison Wesley>

Page 23: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

23/24

TRANSLATION PROCESS

기호 테이블 (Symbol Table)

구문 분석기 (Parser, Syntax Analyzer)가 선언문 분석 시에 생성

변수 이름, 타입, 구조 등의 정보 기록

구문 분석기 (Parser, Syntax Analyzer)가 명령문 분석 시에 이용

예) a b + c;

b와 c의 타입(실수, 정수, 문자열 등)에 따라 적절한 기계어를 생성하도록

코드 생성기에 요청. 23/40

Source

program

Lexical

analyzer

Parser

Code

generator

Object

program

Page 24: PROGRAMMING LANGUAGES CONCEPTS AND TRANSLATIONcontents.kocw.net/KOCW/document/2016/ginue/leesoojung/3.pdf · 2017. 1. 23. · PROGRAMMING LANGUAGES – CONCEPTS AND TRANSLATION 경인교대

24/24

TRANSLATION PROCESS

코드 생성기 (Code Generator)

구문 분석기가 인식한 문장을 구현하기 위한 기계어 생성

코드 최적화(code optimization)가 이슈

코드 최적화의 필요성 및 예:

ex1) temp a + b + c;

temp2 (a + b + c) * 3;

ex2) x y + z;

w x + z; // x and z need not be loaded into the

// registers again.

24/40