Download presentation
Presentation is loading. Please wait.
Published byMarjory Joseph Modified over 8 years ago
1
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY
2
ABSTRACT Language Translator: compiler, interpreter Fundamental Principles of Compilation Compilation Processes Compiler Structure Good Compiler Portability, Retargetability, Bootstrapping Short History Automation Tools Compiler Theory: Grammar & Automata (2014-1) Compiler 2
3
1. LANGUAGE TRANSLATOR Source code/program Target/object code/program Language translator Compiler Interpreter Preprocessor Assembler Cross compiler … Language Translator Source Target (2014-1) Compiler 3
4
1.1 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. (2014-1) Compiler 4
5
1.2 INTERPRETER Input: executable specification Output: result of executing the specification (2014-1) Compiler 5 Source Programs Interpreter Results
6
1.3 HYBRID METHOD Compile + interpretation Java (2014-1) Compiler 6 Compiler source code bytecode JVM JIT compiler results native code MACHINE
7
2. FUNDAMENTAL PRINCIPLES OF COMPILATION The compiler must preserve the meaning of the program being compiled The compiler must improve the input program in some discernible way (2014-1) Compiler 7
8
3. COMPILATION PROCESS (1) Compiler components (2014-1) Compiler 8
9
3. COMPILATION PROCESS (2) Front-end Lexical analysis: token Syntactic analysis(parsing): parse tree Semantic analysis: intermediate code Back-end Code optimization Target code generation (2014-1) Compiler 9 Source language Target language
10
4. COMPILER STRUCTURE (2014-1) Compiler 10
11
어휘 분석 고급 언어 프로그램 기계어 또는 어셈블리어 프로그램 구문 분석 의미 분석 중간 코드 생성 코드 최적화 목적 코드 생성 심벌 테이블 오류 처리 (2014-1) Compiler 11
12
(2014-1) Compiler 12
13
(2014-1) Compiler 13
14
4.1 LEXICAL ANALYSIS Lexical analyzer(scanner) (2014-1) Compiler 14 ex) if ( a > 10 )... Token : if ( a > 10 )... Token Number : 32 7 4 25 5 8
15
4.2 SYNTAX ANALYSIS (1) Syntax analysis, parsing Syntax analyzer, parser Function Syntax checking Tree generation (2014-1) Compiler 15
16
4.2 SYNTAX ANALYSIS (2) (2014-1) Compiler 16 ex) if (a > 10) a = 1; if > = a 10 a 1
17
4.3 SEMANTIC ANALYSIS Intermediate code generation Semantic checking if (a>10) a = 1.0; when a is integer, semantic error (2014-1) Compiler 17
18
4.4 CODE OPTIMIZATION (1) Optional phase Local optimization Constant folding Eliminating redundant load, store instructions Algebraic simplification Strength reduction (2014-1) Compiler 18
19
4.4 CODE OPTIMIZATION (2) Global optimization Flow analysis technique Common sub-expression Moving loop invariants Removing unreachable codes (2014-1) Compiler 19
20
4.5 TARGET CODE GENERATION Instruction selection & generation Instruction scheduling Register management/allocation Storage allocation (2014-1) Compiler 20
21
5. GOOD COMPILER Correct code generation Ability of handling of programs of arbitrary size Compilation speed Compiler size User-friendliness: quality of error reporting (2014-1) Compiler 21
22
6. OTHER ISSUES (1) Portability The ease with which the compiler itself can be made to run on another machine Retargetability The ease with which it can be made to generate code for another machine Replace/create back-end (2014-1) Compiler 22
23
6.1 BOOTSTRAPPING (1) What language is used to write compiler itself? Implementation (host) language = machine language How the first compilers were written? Simple case (2014-1) Compiler 23 MACHINE M1 Existing Compiler for language B Compiler for language A Written in language B Running compiler for language A
24
6.1 BOOTSTRAPPING (2) More complicated case T-diagram (2014-1) Compiler 24 MACHINE M1MACHINE M2 Cross compiler Existing Compiler for language B Compiler for language A Written in language B Running compiler for language A ST H
25
6.1 BOOTSTRAPPING (3) Combination 1 (2014-1) Compiler 25 ABACBC HHH MACHINE H
26
6.1 BOOTSTRAPPING (4) Combination 2 (2014-1) Compiler 26 AABB HH M KK MACHINE H MACHINE M MACHINE K
27
6.1 BOOTSTRAPPING (5) Scenario 1 (2014-1) Compiler 27 MACHINE H Existing Compiler for language B Compiler for language A Written in language B Running compiler for language A AAHH BB H HH
28
6.1 BOOTSTRAPPING (6) Scenario 2: cross compiler (2014-1) Compiler 28 MACHINE KMACHINE H Existing Compiler for language B Compiler for language A Written in language B Running compiler for language A A A H H BB K K K
29
6.1 BOOTSTRAPPING (7) Bootstrapping process “quick and dirty” compiler in assembly language Inefficient and correct code generation Compile “good” compiler by using the “quick and dirty” compiler (2014-1) Compiler 29 AAHH AA H HH Compiler written in its own language A “quick and dirty” compiler written in machine language Running but inefficient compiler
30
6.1 BOOTSTRAPPING (8) Recompile final version (2014-1) Compiler 30 AAHH AA H HH Compiler written in its own language A Running but inefficient compiler Final version of the compiler
31
6.1 BOOTSTRAPPING (9) Advantages Improvement to the source code of the compiler working compiler Easy porting Porting a compiler written in its own source language (step 1) (2014-1) Compiler 31 AK AA H H AK H Compiler source code retargeted to K Original compiler Cross compiler
32
6.1 BOOTSTRAPPING (10) Porting a compiler written in its own source language (step 2) (2014-1) Compiler 32 AAKK AA H KK Compiler source code retargeted to K Cross compiler Retargeted compiler
33
7. SHORT HISTORY 1945-1960: code generation 1960-1975: parsing 1975-present: code generation and code optimization Reliability, efficiency, pleasant user interface How to compiler what to compile (2014-1) Compiler 33
34
8. AUTOMATION TOOLS Compiler compiler: compiler generator (2014-1) Compiler 34
35
8.1 LEXICAL ANALYZER GENERATOR Ex: LEX (2014-1) Compiler 35
36
8.2 PARSER GENERATOR Ex: YACC (2014-1) Compiler 36
37
(2014-1) Compiler 37
38
8.3 CODE GENERATION AUTOMATION (2014-1) Compiler 38
39
9. COMPILER THEORY: GRAMMAR & AUTOMATA (1) Grammar Noam Chomsky classification Type 3: Regular Grammar (RG) Type 2: Context-Free Grammar (CFG) Type 1: Context-Sensitive Grammar (CSG) Type 0: Unrestricted Grammar (UG) (2014-1) Compiler 39
40
9. COMPILER THEORY: GRAMMAR & AUTOMATA (2) RG ⊂ CFG ⊂ CSG ⊂ UG 무제한 문법 문맥 자유 문법 정규 문법 문맥 의존 문법 (2014-1) Compiler 40
41
9. COMPILER THEORY: GRAMMAR & AUTOMATA (3) Regular language Context-free language Context-sensitive language Recursively Enumerable Set (2014-1) Compiler 41
42
9. COMPILER THEORY: GRAMMAR & AUTOMATA (4) Finite Automata: NFA, DFA Pushdown Automata Linear Bounded Automata Turing Machine (2014-1) Compiler 42
43
10. OTHERS Symbol table Literal table Error message (2014-1) Compiler 43
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.