22
Compiler Construction 1장

Compiler Construction

Embed Size (px)

DESCRIPTION

Compiler Construction. 1 장. 간략한 소개. Aho, Sethi and Ullman Kenneth C. Louden Thomas W. Parsons 관련 과목 Theories of programming languages Automata theory System programming (including O.S) Assembly language Data structures and File structures Programming techniques. - PowerPoint PPT Presentation

Citation preview

Page 1: Compiler Construction

Compiler Construction

1 장

Page 2: Compiler Construction

2

간략한 소개

Aho, Sethi and Ullman Kenneth C. Louden Thomas W. Parsons 관련 과목

– Theories of programming languages– Automata theory– System programming (including O.S)– Assembly language– Data structures and File structures– Programming techniques

Page 3: Compiler Construction

3

Compilers and Interpreters

Compilers source program compiler target program |__ error messages

Interpreterssource program interpreter result data __|

Page 4: Compiler Construction

4

역사

컴파일러 역사– John Backus : FORTRAN compiler– Backus-Naur Form and Algol– N. Wirth : Pascal– C : System programming language– Ada : Language Environment– Java, C#, ….

Page 5: Compiler Construction

5

인터프리터

Basic …. Bill Gates APL … an intolerable complexity Smalltalk … Object oriented languages LISP … lambda calculus …. Incremental

programming Java … for ubiquitous computing, Internet Embedded systems

Page 6: Compiler Construction

6

New paradigm

For Internet, Home computing, Ubiquitous computing, embedded systems and so on

Languages and Compilers that are Small, Smart and Safe Hybrid approach with concepts of compilers and interpreters

– Java, C# Virtual machine

– Free University, Tannenbaum– 개념 설명

Page 7: Compiler Construction

7

Java

Simple, Robust, Object-oriented, Platform-independent, multithreaded, dynamic, general-purpose programming environment

Write-once, Run Anywhere Java virtual machine

– Bytes codes : portability and platform independent

Java Runtime Environment

Page 8: Compiler Construction

8

Software tools related to compilers

Structure editors Pretty printers Static checkers Interpreters Text formatters … tex, nroff, troff Silicon compilers … System on chip Query interpreter Interpreters for HTML, XML, SGML

Page 9: Compiler Construction

9

Skeletal source program

Source program

Target assembly program

Relocatable machine code

library, relocatable object files

Absolute machine code

preprocessor

compiler

assembler

Loader/link-editor

Fig. 1.3. A language-processing system

Page 10: Compiler Construction

10

lexicalanalyzer

syntaxanalyzer

semanticanalyzer

intermediate codegenerator

codeoptimizer

codegenerator

errorhandler

symbol-tablemanager

source program

target program

Fig. 1.9. Phases of a compiler

Page 11: Compiler Construction

11

lexical analyzer

syntax analyzer

id1 : = id2 + id3 * 60

position : = initial + rate * 60

: =

id3

id2

id1

60

*

+

semantic analyzer

: =

id3

id2

id1

inttoreal

*

+

60

intermediate code generator

code optimizer

temp1 : = inttoreal(60)temp2 : = id3 * temp1temp3 : = id2 + temp2id1 : = temp3

temp1 : = id3 * 60.0id1 : = id2 + temp1

code generator

MOVF id3, R2MOVF #60.0, R2MOVF id2, R1MOVF R2, R1MOVF R1, id1

position

initial

rate

SYMBOL TABLE

. . .

. . .

. . .

1

2

3

4

Fig. 1.10. Translation of a statement

Page 12: Compiler Construction

12

Lexical Analysis

Finite state machine Regular expression Regular grammar Kleen closure O(n) Lex Editor, pattern search Perl : $x =~ thing

Page 13: Compiler Construction

13

Lexical Analysis

In a compiler, linear analysis is called lexical analysis or scannning. For example ,in lexical analysis the characters in the assignment statementposition := initial + rate * 60

Would be grouped into the following tokens:1. The identifier position.2. The assignment symbol := 3. The identifier initial.4. The plus sign.5. The identifier rate.6. The multiplication sign.7. The number 60.

The blanks separating the characters of these tokens would normally be eliminated during lexical analysis.

Page 14: Compiler Construction

14

Syntax Analysis(Parsing)

Push-down automata Context-free grammar BNF(Backus-Naur Form) O(n3) LL, LR parsing PLs, XML, HTML … Interpretation framework YACC … syntax-directed translation engine

Page 15: Compiler Construction

15

Syntax Analysis

Fig. 1.4 Parse tree for position := initial + rate + 60

position

initial

rate 60

assignmentstatement

expression

expression

expression

identifier

expression

expressionidentifier

identifier number

*

+

: =

Page 16: Compiler Construction

16

Semantic Analysis

Context-sensitive grammar bounded Turing machine

Unrestricted grammar Turing machine Unsolvable problem …. Heuristic rules Type checking, resolving overload, scope rule,

binding, …. Syntax-directed translation engine

Page 17: Compiler Construction

17

Fig. 1.5. Semantic analysis inserts a conversion from integer to real.

: =

*

+

60

position

initial

rate

: =

*

+

60

position

initial

rate inttoreal

Page 18: Compiler Construction

18

Code optimization and generation

Speed, space Data-flow analysis data flow engines Two pass optimization

– Pseudo code level : P-code, Bytes code– Target code level : register allocation, select

efficient operations, efficient pipelining … RISC machine 의 실패 (????)

– Alpha chip Free University’s approach, down loading Automatic code generators

Page 19: Compiler Construction

19

: =

id3

id2

id1

60

*

+id 1

id 2

id 3 num 60

: =

+

*

(a) (b)

Fig. 1.11. The data structure in (b) is for the tree in (a).

Page 20: Compiler Construction

20

temp1 : = inttoreal(60)temp2 : = id3 * temp1temp3 : = id2 + temp2id1 : = temp3

(1.3)

temp1 : = id3 * 60.0id1 : = id2 + temp1 (1.4)

(1.5)

MOVF id3, R2MOVF #60.0, R2MOVF id2, R1MOVF R2, R1MOVF R1, id1

Page 21: Compiler Construction

21

Others

Portability – A 라는 컴파일러가 M 에서 수행될 때 , N 이라는 기계에

이식하는 방법 : 먼저 M 에서 intermediate code 에서 N 기계의 machine code 로 바꾸는 루틴을 작성하고 , A 로 A 컴파일러를 작성한 후 , 이를 컴파일하고 , intermediate code로 만든 후 N 기계의 기계어를 생성한다 . 그리고 이를 down load 하여 사용…

– 컴파일러 개발에서 사용하는 방법 Multi-passes or single pass

Page 22: Compiler Construction

22

Term project

Compiler 작성– Java, C, XML, … 선택 가능– 최대한 도와줄 것임 … 조교가 직접 자신이 짠 프로그램을

설명하고 , code generation part 는 강의에 포함하여 짤 수 있게 함

– Bytes code 는 설명할 것임– Java 를 추천