Teknik Kompiler 0. oleh: antonius rachmat c, s.kom

Teks penuh

(1)

Teknik Kompiler 0

oleh: antonius rachmat c,

(2)

Teknik Kompiler (3 sks)

z

Tujuan :

z Mempelajari teori-teori kompilasi, struktur mesin

kompiler, dan pada akhirnya mahasiswa mampu menerapkan teori tersebut untuk membuat

aplikasi suatu kompiler sederhana

z

Hari dan Waktu :

z Setiap hari 8.00-10.00 (Blok 2) z TTS: 14 juli dan TAS: 24 juli

(3)

Pengajar

z

Dosen:

Antonius Rachmat C, S.Kom, M.Kom

z

Email:

anton@ukdw.ac.id

z

Website:

http://lecturer.ukdw.ac.id/anton

z

Blog:

http://antonie.wordpress.com

(4)

Silabus

z Silabus & Pengantar Kompiler secara umum z Teori Kompilasi

z Kategori Bahasa Pemrograman, Translator, Model

Kompilator, dan Mutu Kompilator

z Struktur dan Fase Kompiler

z Perancangan Bahasa Pemrograman z Regular Expression 1

z Sintaks Regex

z Regular Expression 2

z Sintaks Regex (lanjutan)

z Regex dalam penggunaan (PHP&.NET) dan Contoh

(5)

Silabus (2)

z Notasi Bahasa & Analisis Leksikal z Grammar

z Hirarki Chomsky

z Automata - Finite State Automata

z DFA dan NFA

z Token, dan Lexem z Diagram Transisi

z Analisis Sintaks z Sintaks

z Tata Bahasa Bebas Konteks z NFA ke TBBK

z Parsing : Top-down dan Bottom-Up

(6)

Silabus (3)

z

Transformasi TBBK:

z Penghilangan TBBK useless, produksi unit, dan

produksi epsilon

z Chomsky Normal Form (CNF) dan Algortima

Chocke, Youger, Kasami (CYK)

z

Analisis Semantik

z LL(1) dan Push Down Automata z Recursive Descent Parsing

z Translasi Berdasarkan Sintaks z Tabel Simbol & Hashing

(7)

Silabus (4)

z Pengecekan Tipe & Intermediate Code

z Type Checking z Tupple

z Translasi Intermediate Code z Linking & Loading

z Memory Allocations & Runtime Environments

z Storage

z Runtime Environment z Activation Record

(8)

Silabus (5)

z

Code Optimization

z Optimasi Lokal dan Global

z

Code Generation

z Result

(9)

Daftar Pustaka

z

Alfred V.Aho cs, Compilers Principles,

Techniques, and Tools, 2003, Prentice-Hall

z

Firrar Utadirartatmo, Teknik Kompilasi, 2001,

Yogyakarta : J&J Learnings

z

Firrar Utadirartatmo, Teori Bahasa dan

Otomata, 2001, Yogyakarta : J & J Learnings

z

Steven Haryanto, Regex Kumpulan Resep

(10)

Daftar Pustaka (2)

z Thomas Pittman & James Peters, The Art of

Compiler Design Theory and Practice, 1992, New Jersey : Prentice-Hall International Editions

z Jeffrey E.F. Friedl, Mastering Regular Expressions

owerful Techniques for Perl and Other Tools, 1997, USA : O'REILLY(TM)

z Donald A. Lewine Data General Corporation, POSIX

Programmer's Guide Writing Portable UNIX

Programs with the POSIX.1 Standard, 1991, USA : O'REILLY(TM)

(11)
(12)

Penilaian

z Penilaian : z 85.0 - 100 A 4.0 z 80.0 - 84.9 A- 3.7 z 75.0 - 79.9 B+ 3.3 z 70.0 – 74.9 B 3.0 z 65.0 – 69.9 B- 2.7 z 60.0 – 64.9 C+ 2.3 z 55.0 – 59.9 C 2.0 z 45.0 – 54.9 D 1.0 z 0 – 44.9 E 0.0

(13)

Distribusi Nilai

z

Tes Tengah Semester

: 25

z

Tes Akhir Semester

: 30

z

Intiasari Jurnal 2 org

: 15

z

Tugas Program

: 15

z

Presensi @ 1

: 12

z

Tes Kecil 2x

: 10

(14)
(15)

Short History of Compiler Construction

Formerly "a mystery", today one of the best-known areas of computing

1957 Fortran first compilers

(arithmetic expressions, statements, procedures)

1960 Algol first formal language definition

(grammars in Backus-Naur form, block structure, recursion, ...)

1970 Pascal user-defined types, virtual machines (P-code)

1985 C++ object-orientation, exceptions, templates

1995 Java just-in-time compilation

We only look at imperative languages

Functional languages (e.g. Lisp) and logical languages (e.g. Prolog) require different

(16)

Why should I learn about compilers?

• How do compilers work? • How do computers work?

(instruction set, registers, addressing modes, run-time data structures, ...)

• What machine code is generated for certain language constructs?

(efficiency considerations)

• What is good language design?

It's part of the general background of a software engineer

Also useful for general software development

• Reading syntactically structured command-line arguments

• Reading structured data (e.g. XML files, part lists, image files, ...) • Searching in hierarchical namespaces

• Interpretation of command codes • ... etc

(17)

Compiler

z Adalah sebuah program yang menerima input berupa source

program (source language), kemudian ditranslasikan menjadi bentuk bahasa lain (target language) .

z Source language biasanya merupakan bahasa pemrograman

tingkat menengah atau tinggi

z Target language biasanya berupa bahasa level rendah atau bahasa

mesin

z Melaporkan error dan warning untuk membantu programmer z Contoh kompiler selain untuk programming:

z Mentranslasikan javadoc ke html

z Mendapatkan hasil dari SQL query (query optimization) z Printer parsing PostScript file

z Screen Scrapping

(18)

Fase Kompiler

z

Analisis (front-end)

z Memecah source code dan menciptakan

intermediate representation

z language dependent

z

Sintesis (back-end)

z Membuat target program dari intermediate

representation yang dihasilkan oleh tahap analisis

(19)

analysis Front End

synthesis Back End

(20)
(21)

Analisys Stage

z

First step: recognize letters and words.

z Words are smallest unit above letters

This is a sentence.

z

Note the

z Capital “T” (start of sentence symbol) z Blank “ “ (word separator)

(22)

Analisys Stage

z

Scanning / Lexical Analysis: memecah

(23)

Diagramming a Sentence

This

line

is

a

longer sentence

verb

article noun

article adjective

noun

subject

object

(24)

Parsing Programs

z

Parsing program expressions is the same

z

Consider:

If x == y then z = 1; else z = 2;

z

Diagrammed:

if-then-else

x

==

y

z

1

z

2

assign

relation

assign

(25)

Analysis Stage

z

Parsing / Syntax Analysis

z Mengelompokkan daftar hasil scanning ke dalam

aturan grammar Tata Bahasa Bebas Konteks dan divalidasi

(26)

Semantic Analysis in English

z

Example:

Jack said Jerry lost his assignment yesterday.

What does “his” refer to? Jack or Jerry?

z

Even worse:

How many Jacks are there?

Which one lost the assignment?

(27)

Semantic Analysis in

Programming

z

Programming

languages define

strict rules to avoid

such ambiguities

z

This C++ code prints

“4”; the inner

definition is used

{

int Jack = 3;

{

int Jack = 4;

cout << Jack;

}

}

(28)

More Semantic Analysis

z

Compilers perform many semantic checks

besides variable bindings

z

Example:

Jack lost her homework yesterday.

z

A “type mismatch” between

her

and

Jack

; we

know they are different people

(29)

Example: Semantic

z

X = a + b * c – d / f

z

What are the correct solutions?

z X = ( a + b ) * ( c – d) / f z X = a + ((b * c) – d) / f z X = a + (b * c) – (d / f) z X = ((a + b) * c) / f

(30)

Analysis Stage

z

Intermediate Code Generation: menghasilkan

(31)

Syntesis Stage

z Intermediate Code Optimization

z Object Code Generation: menjadi bahasa mesin z Object Code Optimization

(32)

Optimization is tricky

for (i = 0; i < N; i += 1)

A[i] *= D/A[0];

should be

tmp1 = D/A[0];

for (i = 0; i < N; i += 1)

A[i] *= tmp1;

(33)
(34)
(35)
(36)

Hal-hal lain tentang Kompiler

z

Tabel Simbol

z

Error Handling

z

Programming language design

z

Programming paradigms

(37)

NEXT

z

Teori Kompiler

z

Fase Kompilasi – detail

Figur

Memperbarui...

Related subjects :