TEKNIK KOMPILASI
Sulistyo Puspitodjati
sulistyo@staff.gunadarma
Sumber: Compiler Construction by Vana Doufexi users.ece.northwestern.edu/~boz283/cs-322-original/01.ppt
JADWAL KELAS
Sulistyo P
Senin 1/2/3 D661 4IA16 Teknik Kompilasi **
Senin 4/5/6 D348 4IA17 Teknik Kompilasi **
Selasa 3/4/5 G128 4IA07 Teknik Kompilasi **
Selasa 7/8/9 G244 4IA11 Teknik Kompilasi **
PELAKSANAAN
pelaksanaan perkuliahan semester PTA 2020/2021 secara Daring,
Perkuliahan dimulai pada hari Senin, 28 September 2020.
jadwal kuliah daring berlangsung sesuai jadwal kuliah yang telah diinfokan pada website BAAK.
Mekanisme
PELAKSANAAN
Mekanisme:
Virtual Class seperti biasa = 7 kali dilaksanakan pada minggu keempat perkuliahan
google meet atau platform cloud meeting = 7 kali serta virtual class sebanyak 7 kali 3 minggu pertama
Dosen menyiapkan materi sesuai dengan RPS / SAP
Dosen menginformasikan jadwal pelaksanaan perkuliahan sinkron menggunakan google meet atau platform
meetingnya lainnya melalui studentsite atau nomor kontak (nomor HP) ketua kelas/perwakilan kelas.
Daftar hadir mahasiswa dapat dibuat dan dilakukan sesuai dengan keperluan masing-masing Dosen.
Daftar Hadir Dosen pada google form yang sudah disediakan melalui link https://forms.gle/673iVpwENXvsFAxq6
Mode Kelas
7 Google Meeting
Info link melalui vclass
WA ketua/wakil kelas
Student site
7 VClass
Materi ppt/pdf
Tugas kecil
Tugas besar bertahap
UTS
Nilai 50% Tugas besar
UAS
** = Praktikum Penunjang
Compiler?
program yang membaca program yang ditulis dalam suatu bahasa dan di terjemahkan ke program yang ditulis dalam bahasa lain
Modula-2 to C
Java to bytecodes
C++ to C
Bagian dari translator
Translator :
Interpreter
Compiler
7
TRANSLATOR
What is a compiler?
a program that translates an executable program in one language into an executable program in another language
A compiler is a computer program that transforms code written in a high-level programming language into the machine code
convert the code into machine code (create an exe) before program run
What is an interpreter?
a program that reads an executable program and produces the results of running that program
which coverts each high-level program statement into the machine code.
Interpreters convert code into machine code when the program is run
8
Compiler vs Interpreter
Tugas baca: Kunjungi dan pelajari
https://www.guru99.com/difference-compiler-vs-interpreter.html
Pentingkah belajar COMPILER?
Untuk contoh aplikasi yang keren dari teori-teori yg sudah didapat sebelumnya
Data Structures
Teori Bahasa dan Otomata
Algorithms
Computer Architecture
Pengalaman merekayasa SW
Better understanding of programming languages
Fitur compiler
Correctness
preserve the meaning of the code
Kecepatan target code
Kecepatan Kompilasi
Bagusnya penangan dan pelaporan error
Tingkat Cooperation dengan debugger
Support compilation terpisah
Compiler structure
Pakai IR (intermediate representation = representasi antara)
Why?
source code
target Front End IR Back End code
Fase: Analisa Sintesa 2-Pass
Struktur Compiler
Front end
Mengenl legal/illegal programs
melaport/menangani errors
Generate IR (intermedite representtion)
Process dapat diotomatisasi
Back end
Translate IR ke target code
instruction selection
register allocation
instruction scheduling
lots of NPC (Non Programmable Character) problems -- use approximations
Struktur Compiler
Optimization
tujuan
improve running time of generated code
improve space, power consumption, etc.
bagaimana?
perform a number of transformations on the IR
multiple passes
perhatikan: preserve meaning of code tidak ambigu
The Front End
Scanning (a.k.a. lexical analysis)
Mengenal "words" (tokens)
Parsing (a.k.a. syntax analysis)
check syntax
Semantic analysis
Mengartikan (examine meaning ) (e.g. type checking)
Other issues:
symbol table manager (menelusuri identifiers)
error detection/reporting/recovery
Scanner
Its job:
Dari untaian karakter yang diberikan dikenali kata (token) yang terlibat
Mis. x = 1 dikenal sebagai IDENTIFIER EQUAL INTEGER
Mengumpulkan informasi identifier information
Contoh: IDENTIFIER berhubungan dengan lexeme (susunan huruf yang membentuk token x), type (berdasarkan deklarasi identifier x).
Mengabaikan white space dan comments
Melaporkan kesalahan
Good news
the process can be automated
Parser
Its job:
Check dan verifikasi syntax berdasarkan specifikasi aturan syntax
Mis. IDENTIFIER LPAREN RPAREN memebentuk EXPRESSION.
context-free grammars specify syntax
Report errors
Build IR
Dapat berupa syntax tree
Good news
the process can be automated
Contoh syntax in C++
// function returning the max between two numbers int max(int num1, int num2) {
// local variable declaration int result;
if (num1 > num2) result = num1;
else
result = num2;
return result;
}
Semantic analysis
Its job:
Check arti program (coding/script)
Mis x=y, apakah y terdefinisi sebelum digunakan?
Sudahkah dideklarasi ?
Mis x=y, typenya x dan y sesuai ?
Makna script tergantung context
Report errors
Contoh:
Kucing menendang bola vs Bola menendang kucing
IR (intermediate representation)
Graphical
parse tree, DAG
Linear
three-address code
Hybrid
linear for blocks of straight-line code, a graph to connect blocks
Low-level or high-level
Summary
> 2 pass
AGENDA
Sesuai RPS (SAP) lihat sap.gunadarma.ac.id
The scanning process
Main goal: recognize words
How? by recognizing patterns/pola
Contoh: identifier berbentuk susunan huruf atau digits yang diawali dengan huruf
Pola lexical membentuk bahasa regular
Regular languages dapat dirumuskan menggunakan regular expressions (REs)
Dapatkan RE recognizer diotomatisasi?
Yes!
The scanning process
Definition: Regular expressions (over alphabet Σ)
ε is an RE denoting {ε}
If α∈Σ, then α is an RE denoting {α}
If r and s are REs, then
(r) is an RE denoting L(r)
r|s is an RE denoting L(r)∪L(s)
rs is an RE denoting L(r)L(s)
r* is an RE denoting the Kleene closure of L(r)
Property: REs are closed under many operations
This allows us to build complex REs.
The scanning process
Definition: Deterministic Finite Automaton
a five-tuple (Σ, S, δ, s0, F) where
Σ is the alphabet
S is the set of states
δ is the transition function (S×Σ→S)
s0 is the starting state
F is the set of final states (F ⊆ S)
Notation:
Use a transition diagram to describe a DFA
DFAs are equivalent to REs
Hey! We just came up with a recognizer!
The scanning process
Goal: automate the process
Idea:
Start with an RE
Build a DFA
How?
We can build a non-deterministic finite automaton (Thompson's construction)
Convert that to a deterministic one (Subset construction)
Minimize the DFA (Hopcroft's algorithm)
Implement it
Existing scanner generator: flex