Upload
rahadyan-permana
View
399
Download
12
Embed Size (px)
Citation preview
1
Pertemuan 9 Semantic AnalyzerAries Yoshan Sekolah Tinggi Teknik Surabaya
Compilers: Principles, Techniques, and Tools Aho, Sethi, Ullman Bab 6: Type Checking Bab 2: A Simple One-Pass Compiler
2 Sekolah Tinggi Teknik Surabaya
Bertugas memeriksa program asal terhadap kesalahan semantik dan mencari informasi tipe data yang nantinya dipakai untuk pembentukan kode tujuan. Nama lain: Static Checker.
3 Sekolah Tinggi Teknik Surabaya
Type checker Declaration checker Uniqueness checker Flow-of-control checker Name-related checker
4 Sekolah Tinggi Teknik Surabaya
Memeriksa apakah setiap operator yang ada terpasang pada operand yang kompatibel. Contoh: dim i as integer for i = 1 to 3.5 next dim a(5) as double a(2.5) = 105 Sekolah Tinggi Teknik Surabaya
Proses pemeriksaan tipe (type checker) membutuhkan dua checker lain: Declaration checker Memeriksa bahwa setiap identifier harus dideklarasikan terlebih dahulu sebelum digunakan. Uniqueness checker Memeriksa bahwa nama identifier harus unique, tidak boleh kembar.6 Sekolah Tinggi Teknik Surabaya
Memeriksa bahwa setiap identifier harus dideklarasikan terlebih dahulu sebelum digunakan. Contoh: for i = 1 to 10 next
variabel i harus dideklarasikan terlebih dahulu dengan menggunakan statement dim.7 Sekolah Tinggi Teknik Surabaya
Memeriksa bahwa nama identifier harus unique, tidak boleh kembar, saat pendeklarasiannya. Contoh: dim a as integer dim a as double dim x as integer function x as double end function8 Sekolah Tinggi Teknik Surabaya
Memeriksa bahwa setiap statement yang menyebabkan flow-of-control berpindah, harus diletakkan pada tempat yang tepat. Contoh: sub x exit while end sub
exit while harus diletakkan dalam konstruksi while ... end while Sekolah Tinggi Teknik Surabaya
9
Ada kalanya sebuah nama yang sama harus muncul lebih dari satu kali. Checker jenis ini memastikan kemunculannya. Contoh: for i = 1 to 10 next i
i yang ada setelah keyword next harus sama dengan i yang ada setelah keyword for, untuk menyatakan sebuah loop for. Sekolah Tinggi Teknik Surabaya
10
Semua checker, kecuali flow-of-control checker, memeriksa apa yang namanya identifier. Declaration checker: setiap identifier harus dideklarasikan. Uniqueness checker: nama identifier harus unique. Name-related checker: identifier tertentu harus muncul lebih dari satu kali pada sebuah konstruksi tertentu. Sekolah Tinggi Teknik Surabaya
11
Type checker: apabila operand berupa identifier, maka checker membutuhkan pengetahuan tentang identifier tersebut. Contoh pengetahuan tentang identifier yang dibutuhkan: Jenis identifier: variabel, konstanta, prosedur, fungsi, dsb. Tipe data: integer, real, boolean, char, string, dsb. Sekolah Tinggi Teknik Surabaya
12
Tabel simbol adalah tabel yang menampung pengetahuan tentang setiap identifier. Nama lain: tabel identifier.
13 Sekolah Tinggi Teknik Surabaya
Alternatif implementasi: Array Binary search tree Hash table Implementasi yang paling efisien: hash table Untuk mempermudah penjelasan, slide berikutnya mengasumsikan tabel simbol diimplementasikan dengan array. Sekolah Tinggi Teknik Surabaya
14
public class Simbol implements Comparable { String nama; int jenis; int tipeData; ... public int compareTo(Simbol o) { return nama.compareTo(o.nama); } }
compareTo bertujuan agar pencarian cepat dengan Binary Search dapat dilakukan Sekolah Tinggi Teknik Surabaya
15
public class TabelSimbol { private ArrayList tabelSimbol = new ArrayList(); public Simbol cari(String nama) { Collections.sort(tabelSimbol); int posisi = Collections.binarySearch( tabelSimbol, new Simbol(nama)); if (posisi < 0) return null; else return tabelSimbol.get(posisi); } ... } Sekolah Tinggi Teknik Surabaya
16
public class TabelSimbol { ... public boolean tambah(Simbol o) { Collections.sort(tabelSimbol); int posisi = Collections.binarySearch( tabelSimbol, o); if (posisi >= 0) return false; else { tabelSimbol.add(o); return true; } } Operasi penting tabel simbol adalah } cari simbol dan tambah simbol baru Sekolah Tinggi Teknik Surabaya
17
%{ import java.util.*; import java.io.*; %} ... %% program : stmt_list ... %% static TabelSimbol tabelSimbol = new TabelSimbol(); ...18 Sekolah Tinggi Teknik Surabaya
Semantic rule terkait uniqueness checker dipasang pada production terkait deklarasi variabel, konstanta, prosedur, fungsi, dan sebagainya.program : stmt_list : | stmt : | | | | stmt_liststmt_list stmt dim_stmt sub_declaration function_declaration ... NL
19
Sekolah Tinggi Teknik Surabaya
Semantic rule akan memeriksa keberadaan identifier dalam tabel simbol. Jika ditemukan, maka checker memberikan pesan kesalahan. Jika tidak ditemukan, maka checker menambahkan identifier tersebut ke dalam tabel simbol. Slide berikut memperlihatkan contoh checker pada deklarasi variabel. Sekolah Tinggi Teknik Surabaya
20
dim_stmt id_list
: : | id_unique :
DIM id_list AS nama_tipe id_unique id_list ',' id_unique ID { Simbol o = new Simbol( $1, Simbol.VARIABEL, Simbol.NONE); if (!tabelSimbol.tambah(o)) yyerror("Duplicate ID"); }
21 Sekolah Tinggi Teknik Surabaya
Semantic rule terkait declaration checker dipasang pada production terkait penggunaan variabel, konstanta, prosedur, fungsi, dan sebagainya.Contoh production terkait penggunaan variabelassign_stmt : ID '=' expr NL for_stmt : FOR ID '=' expr TO expr step_opt NL stmt_list NEXT id_opt NL expr : ... factor : ... | ID Sekolah Tinggi Teknik Surabaya
22
assign_stmt : ID { Simbol o = tabelSimbol.cari($1); if (o == null) yyerror("Unknown ID"); if (o.jenis != Simbol.VARIABEL) yyerror("Invalid ID"); } '=' expr NL
23 Sekolah Tinggi Teknik Surabaya
Terkait dengan identifier, diperlukan pengetahuan tentang tipe data yang dikandung oleh sebuah identifier. Pada uniqueness checker, selain bahwa checker berusaha menambahkan identifier ke dalam tabel simbol, checker juga berusaha mencari tipe data identifier tersebut dan menyimpan tipe data identifier tersebut ke dalam tabel simbol. Sekolah Tinggi Teknik Surabaya
24
id_unique : ID { Simbol o = new Simbol( $1, Simbol.VARIABEL, Simbol.NONE); if (!tabelSimbol.tambah(o)) yyerror("Duplicate ID"); $$ = o; }
Atribut id_unique berisi objek simbol yang berhasil ditambahkan ke dalam tabel simbol25 Sekolah Tinggi Teknik Surabaya
id_list
: id_unique { ArrayList list = new ArrayList(); list.add((Simbol)$1); $$ = list; } | id_list ',' id_unique { ((ArrayList)$1).add( (Simbol)($3)); }
Atribut id_list berisi objek ArrayList yang menampung daftar objek simbol yang berhasil ditambahkan ke dalam tabel simbol Sekolah Tinggi Teknik Surabaya
26
nama_tipe : | | | dim_stmt :
INTEGER { $$ = Simbol.INTEGER; } DOUBLE { $$ = Simbol.REAL; } BOOLEAN { $$ = Simbol.BOOLEAN; } ... DIM id_list AS nama_tipe { for (Simbol o : (ArrayList)$2) { o.tipeData = $4; } }
27 Sekolah Tinggi Teknik Surabaya
Pengetahuan tentang identifier dimanfaatkan oleh type checker untuk memeriksa apakah tipe data operand sesuai dengan operator yang sedang diperiksa. Pesan kesalahan apabila tipe data tidak sesuai adalah type mismatch.
28 Sekolah Tinggi Teknik Surabaya
Sejumlah compiler berusaha melakukan konversi tipe data otomatis apabila tipe data tidak sesuai. Contoh: BASIC. Walau slide berikut memperlihatkan type checker pada bahasa BASIC, asumsi yang diambil adalah tidak ada konversi tipe data otomatis; dengan demikian bila tipe data tidak sesuai akan muncul pesan kesalahan.29 Sekolah Tinggi Teknik Surabaya
Contoh asumsi: Operator \ (bagi bulat) dan mod hanya untuk operand bertipe integer. Operator + diperbolehkan apabila kedua operand sama-sama bertipe string, atau kedua operand sama-sama bertipe numerik (integer atau real). Operator -, *, /, dan ^ hanya untuk operand bertipe integer atau real. Operator & hanya untuk operand bertipe string. Sekolah Tinggi Teknik Surabaya
30
Hasil operasi \ (bagi bulat) dan mod adalah bertipe integer. Hasil operasi + adalah bertipe string apabila kedua operand bertipe string. Hasil operasi +, -, dan * adalah bertipe real apabila salah satu operand bertipe real. Hasil operasi +, -, dan * adalah bertipe integer apabila kedua operand bertipe integer. Hasil operasi / dan ^ adalah bertipe real. Hasil operasi & adalah bertipe string. Sekolah Tinggi Teknik Surabaya
31
Bagian 1: Konstantafactor : | | | | | | '+' factor '-' factor '(' expr ')' INTCONST { $$ = Simbol.INTEGER; } REALCONST { $$ = Simbol.REAL; } STRCONST { $$ = Simbol.STRING; } ID
32 Sekolah Tinggi Teknik Surabaya
Bagian 2: Variabelfactor : | | | | '+' factor '-' factor '(' expr ')' ... ID { Simbol o = tabelSimbol.cari($1); if (o == NULL) yyerror("Unknown ID"); if (o.jenis != Simbol.VARIABEL) yyerror("Invalid ID"); $$ = o.tipeData; }33 Sekolah Tinggi Teknik Surabaya
Bagian 3: Propagasi hasilfactor : | | | | | | '+' factor '-' factor '(' expr ')' { $$ = $2; } INTCONST REALCONST STRCONST ID
34 Sekolah Tinggi Teknik Surabaya
Bagian 4: Type checkerfactor : '+' factor { if ($2 != Simbol.INTEGER && $2 != Simbol.REAL) yyerror("Type mismatch"); $$ = $2; } | '-' factor { if ($2 != Simbol.INTEGER && $2 != Simbol.REAL) yyerror("Type mismatch"); $$ = $2; } | '(' expr ')' | ... Sekolah Tinggi Teknik Surabaya
35
Walaupun memeriksa identifier, checker ini tidak membutuhkan tabel simbol.for_stmt : FOR ID '=' expr TO expr step_opt NL stmt_list NEXT id_opt { if ($11 != null && !$2.equals($11)) yyerror("Invalid ID"); } NL id_opt : ID { $$ = $1; } | { $$ = null; }36 Sekolah Tinggi Teknik Surabaya
Agar sebuah simbol, baik terminal maupun non-terminal, dapat mempunyai atribut, tipe atribut simbol tersebut harus dideklarasikan. Pada BYACC/J, ada empat tipe atribut: ival: bertipe int dval: bertipe double sval: bertipe String obj: bertipe Object37 Sekolah Tinggi Teknik Surabaya
Cara mendeklarasikan tipe atribut simbol non-terminal:%type non_term
Cara mendeklarasikan tipe atribut simbol terminal:%token term
38 Sekolah Tinggi Teknik Surabaya
Ada empat tipe atribut:%type %type %type %type ... ... ... ... %token %token %token %token ... ... ... ...
Khusus untuk tipe Object, akses nilai ke atribut tersebut harus menggunakan type casting.
39 Sekolah Tinggi Teknik Surabaya
%type factor ... %% factor : '+' factor { if (...) ...; $$ = $2; } | '-' factor { if (...) ...; $$ = $2; } | '(' expr ')' { $$ = $2; } | INTCONST { $$ = Simbol.INTEGER; } | REALCONST { $$ = Simbol.REAL; } | STRCONST { $$ = Simbol.STRING; } | ID { Simbol o = ...; ... 40 $$ = o.tipeData; } Sekolah Tinggi Teknik Surabaya
%type id_list id_unique ... Akses nilai harus menggunakan type casting %% id_list : id_unique { ArrayList list = new ArrayList(); list.add((Simbol)$1); $$ = list; } | id_list ',' id_unique { ((ArrayList)$1).add( (Simbol)($3)); } id_unique : ID { Simbol o = new Simbol( $1, Simbol.VARIABEL, Simbol.NONE); ... $$ = o; } Sekolah Tinggi Teknik Surabaya
41
%% program : stmt_list ... %% static TabelSimbol tabelSimbol = new TabelSimbol(); public void yyerror(String error) { System.err.println("Error: " + error); System.exit(0); } ...
yyerror dipakai untuk menampilkan pesan kesalahan dan proses compilation dipaksa berhenti Sekolah Tinggi Teknik Surabaya
42