You are on page 1of 45

Teknik Kompilasi I

TEKNIK KOMPILASI

Ernastuti & Sulistyo

Ernastuti & Sulistyo 1/45


Teknik Kompilasi I

Ernastuti & Sulistyo 2/45


Teknik Kompilasi I

MATERI

Ernastuti & Sulistyo 3/45


Teknik Kompilasi I

ARTI KATA TEKNIK KOMPILASI

Ernastuti & Sulistyo 4/45


Teknik Kompilasi I

TRANSLATOR:
COMPILER & INTERPRETER

Ernastuti & Sulistyo 5/45


Teknik Kompilasi I

COMPILER & INTERPRETER

Ernastuti & Sulistyo 6/45


Teknik Kompilasi I

COMPILER

Ernastuti & Sulistyo 7/45


Teknik Kompilasi I

KENAPA PERLU TRANSLATOR ?

Ernastuti & Sulistyo 8/45


Teknik Kompilasi I

LATAR BELAKANG

Ernastuti & Sulistyo 9/45


Teknik Kompilasi I

Bagaimana dengan orang yang tidak


mengerti bahasa mesin?

Ernastuti & Sulistyo 10/45


Teknik Kompilasi I

Ernastuti & Sulistyo 11/45


Teknik Kompilasi I

Ernastuti & Sulistyo 12/45


Teknik Kompilasi I

Ernastuti & Sulistyo 13/45


Teknik Kompilasi I

BAHASA TINGKAT TINGGI


Pemrograman bisa menggunakan Bahasa Tingkat Tinggi.

Ernastuti & Sulistyo 14/45


Teknik Kompilasi I

Bahasa Tingkat Tinggi adalah:

Ernastuti & Sulistyo 15/45


Teknik Kompilasi I

Ernastuti & Sulistyo 16/45


Teknik Kompilasi I

Jenis Translator: ASSEMBLER

Ernastuti & Sulistyo 17/45


Teknik Kompilasi I

Jenis Translator: COMPILER

Ernastuti & Sulistyo 18/45


Teknik Kompilasi I

Jenis Translator: Interpreter

Ernastuti & Sulistyo 19/45


Teknik Kompilasi I

Why study compilers?


‰ Most CS students do not go on to write a commercial compiler
someday, but that's not why we study compilers. We study
compiler construction for the following reasons:

‰ Writing a compiler gives experience with large-scale applications


development. Your compiler program may be the largest program
you write as a student. Experience working with really big data
structures and complex interactions between algorithms will help
you out on your next big programming project.

‰ Compiler writing is one of the shining triumphs of CS theory. It


demonstrates the value of theory over the impulse to just "hack
up" a solution.

‰ Compiler writing is a basic element of programming language


research. Many language researchers write compilers for the
languages they design.

‰ Many applications have similar properties to one or more phases


of a compiler, and compiler expertise and tools can help an
application programmer working on other projects besides
compilers.
Ernastuti & Sulistyo 20/45
Teknik Kompilasi I

‰ There is no software development method for writing large


programs that doesn't involve pain: pain is inevitable in
software development (Berry's Theorem).

‰ There is no way to learn the skills necessary for writing big


programs without pain. A good CS course includes pain, and
teaches pain management and minimization.

‰ The questions we should ask, then, are:

(a) should CS majors be required to spend a lot of time


becoming really good programmers?
(b) are we providing students with the assistance and access to
the tools and information they need to accomplish their goals
with the minimal doses of inevitable pain that are required?

Ernastuti & Sulistyo 21/45


Teknik Kompilasi I

What Are They and What Kinds of Compilers are Out There?

• The purpose of a compiler is:


to translate a program in some language (the source
language) into a lower-level language (the target language).

The compiler itself is written in some language, called the


implementation language.

To write a compiler you have to be very good at


programming in the implementation language, and have to
think about and understand the source language and target
language.

Ernastuti & Sulistyo 22/45


Teknik Kompilasi I

Several major kinds of compilers


‰ Native Code Compiler
Translates source code into hardware (assembly or machine code)
instructions. Example: gcc.

‰ Virtual Machine Compiler


Translates source code into an abstract machine code, for execution by a
virtual machine interpreter. Example: javac.

‰ JIT Compiler
Translates virtual machine code to native code. Operates within a virtual
machine. Example: Sun's HotSpot java machine.

‰ Preprocessor
Translates source code into simpler or slightly lower level source code, for
compilation by another compiler. Examples: cpp, m4.

‰ Pure interpreter
Executes source code on the fly, without generating machine code.
Example: Lisp.
Ernastuti & Sulistyo 23/45
Teknik Kompilasi I

Phases of a Compiler
¾ Lexical Analysis:
Converts a sequence of characters into words, or tokens

¾ Syntax Analysis:
Converts a sequence of tokens into a parse tree

¾ Semantic Analysis:
Manipulates parse tree to verify symbol and type information

¾ Intermediate Code Generation:


Converts parse tree into a sequence of intermediate code instructions

¾ Optimization:
Manipulates intermediate code to produce a more efficient program

¾ Final Code Generation:


Translates intermediate code into final (machine/assembly) code

Ernastuti & Sulistyo 24/45


Teknik Kompilasi I

Blok Diagram

Program
Program Program
Program
Sumber
Sumber Sasaran
Sasaran

ANALISA SINTESA
Penganalisa
Penganalisa Penganalisa Semantik Pembentuk Pengoptimal
Leksikal Sintaks Pembangkit kode kode
(scanner) (parser) Kode antara

TABEL
TABEL
SIMBOL
SIMBOL

Bagan pokok proses kompilasi

Ernastuti & Sulistyo 25/45


Teknik Kompilasi I

Ernastuti & Sulistyo 26/45


Teknik Kompilasi I

Ernastuti & Sulistyo 27/45


Teknik Kompilasi I

Ernastuti & Sulistyo 28/45


Teknik Kompilasi I

Contoh Source Program ke dalam Kode Mesin:

Ernastuti & Sulistyo 29/45


Teknik Kompilasi I

Contoh Source Program ke dalam Kode Mesin

Ernastuti & Sulistyo 30/45


Teknik Kompilasi I

Konsep dan Notasi Bahasa

Ernastuti & Sulistyo 31/45


Teknik Kompilasi I

Konsep dan Notasi Bahasa

Ernastuti & Sulistyo 32/45


Teknik Kompilasi I

Konsep dan Notasi Bahasa

Ernastuti & Sulistyo 33/45


Teknik Kompilasi I

Contoh Tata Bahasa Sederhana

Ernastuti & Sulistyo 34/45


Teknik Kompilasi I

Tabel Aturan Produksi

Ernastuti & Sulistyo 35/45


Teknik Kompilasi I

Hirarki CHOMSKY

Ernastuti & Sulistyo 36/45


Teknik Kompilasi I

Ernastuti & Sulistyo 37/45


Teknik Kompilasi I

ATURAN PRODUKSI

Ernastuti & Sulistyo 38/45


Teknik Kompilasi I

Ernastuti & Sulistyo 39/45


Teknik Kompilasi I

DIAGRAM STATE

Ernastuti & Sulistyo 40/45


Teknik Kompilasi I

Notasi BNF (Backus Normal Form)

Ernastuti & Sulistyo 41/45


Teknik Kompilasi I

DIAGRAM SYNTAX

Ernastuti & Sulistyo 42/45


Teknik Kompilasi I

DIAGRAM SYNTAX

Ernastuti & Sulistyo 43/45


Teknik Kompilasi I

Kualitas dari Compiler:

Ernastuti & Sulistyo 44/45


Teknik Kompilasi I

Lanjut ke TEKNIK KOMPILASI II.ppt

Ernastuti & Sulistyo 45/45