형식 언어

Download Report

Transcript 형식 언어


Compiler
“A compiler is a computer program which translates programs written in
a particular high-level programming language into executable code for
a specific target computer.”
Source
Programs
Compiler
Object
Programs
(Assembly Language,
Machine Language)
ex) C compiler on SPARC

C program을 입력으로 받아 SPARC에서 수행 가능한 코드를
출력한다.

Compiler Structure
Source
Programs
Front-End
IC
Back-End


Front-End : language dependent part
Back-End : machine dependent part
Object
Programs
Cross-Compiler
“A cross-compiler is a program which is to run on machine A
and produce target code for another machine B.”
Compiler
on A machine
Source
Programs


Target Code
for machine B
Execution : down-loading or interpretation
Bootstrapping
Interpreter
“An interpreter transforms a program directly into a sequence
of machine actions and produces the results of the program.”
data
Interpreter
Source
Programs
C: \. . .
Result


Compiler : Operational System
Interpreter : Developing System or Educational System

Preprocessor

for the language extension
Source
Programs



Preprocessor
Macro substitution
Conditional compilation
Inclusion of files
Extended
Source
Programs
Translator
Target
Programs
Source
Programs
Lexical Analyzer
Token
Syntax Analyzer
Tree
Intermediate Code
Generator
Intermediate
Code
Code Optimizer
Optimized
Code
Target Code
Generator
Object
Programs
1. Lexical Analyzer(Scanner)

컴파일러 내부에서 효율적이며 다루기 쉬운 정수로 바꾸어 줌.
A sequence
of tokens
Lexical Analyzer
Source
Programs
ex) if ( a > 10 ) ...
Token
: if
Token Number : 32
(
a
>
10
7
4
25
5
) ...
8
2. Syntax Analyzer(Parser)

기능: Syntax checking, Tree generation.
Tree
A sequence
of tokens

Syntax Analyzer
Error message
or syntactic
structure
출력: incorrect - error message 출력
correct - program structure (=> tree 형태) 출력
ex) if (a > 10) a = 1;
if
>
a
=
10 a
1
3. Intermediate Code Generator

Semantic checking

Intermediate Code Generation
ex) if (a > 10) a = 1.0; ☞ a가 정수일 때 semantic error !
ex) a = b + 1;
Tree :
=
a
+
b
Ucode:
1
lod 1 2
ldc 1
add
str 1 1
- variable reference: (base, offset)
4. Code Optimizer



Optional phase
비효율적인 code를 구분해 내서 더 효율적인 code로 바꾸어 준다.
Meaning of optimization


ex)

major part : improve running time
minor part : reduce code size
LDC R1, 1
LDC R1, 1
(x)
Criteria for optimization



preserve the program meanings
speed up on average
be worth the effort
5. Target Code Generator

중간 코드로부터 machine instruction을 생성한다.
Intermediate
Code

Target
Code Generator
Code generator tasks
1. instruction selection & generation
2. register management
3. storage allocation
4. code optimization (Machine-dependent optimization)
Target
Code