Anda di halaman 1dari 7

Tugas Personal ke-2

Minggu ke 7, Sesi ke 11

1. Diberikan sebuah Syntax Directed Translation sebagai berikut ini.

Production Semantic Rules


L→En L.val = E.val
E → E1 + T E.val = E1 .val + T.val
E→T E.val =T.val
T → T1 * F T.val = T1.val x F.val
T→F T.val = F.val
F→(E) F.val = E.val
F → digit F.val = digit.lexval

Buatlah annotated parse tree untuk statement berikut ini:


a. (3+4) * (5+6)n

b. 1*2*3*(4+5)n

2. Apa kegunaan dari semantic analyzer? Berikan penjelasan dengan menggunakan kalimat
Anda sendiri.

3. Diberikan sebuah potongan program Java. Identifikasilah tipe dari error dari setiap
potongan program tersebut apakah tipenya lexical, syntactic, semantic static atau
semantic dynamic error. Lexical error terdeteksi oleh Scanner sedangkan Syntactic
terdeteksi oleh parser. Semantic static terdeteksi oleh semantic analyzer pada saat waktu
compile, sementara semantic dynamic terdeteksi pada saat runtime. Berikanlah
penjelasan alasan Anda mengkategorikan error untuk potongan program ini.

a. int num = 4.5;


b. int;
c. int@

4. Buatlah DAG dari ekspresi berikut ini:

((x+y)-((x+y)* (x-y)))+((x+y)* (x-y))

COMP6276 – Compilation Techniques


5. Diberikan sebuah statement a+-(b+c). Dari statement tersebut buatlah:

a. Syntax tree

b. Quadruples

c. Triples

d. Indirect Triples

6. Buat three address code dari potongan program dibawah ini.


int a = 2, b = 8, c = 4, d;
for(j=0; j<=10; j++){
a = a * (j* (b/c));
d = a * (j* (b/c));
}

COMP6276 – Compilation Techniques


Jawaban

1. a

COMP6276 – Compilation Techniques


1. b

2.

a. Maintain symbol table untuk setiap blok

b. Source program untuk semantic errors

c. Mengumpulkan type information untuk code generation

d. Mereport compile-time errors dalam code Generating the object code

3.

a. Semantic static (type mismatch)

b. Syntactic (invalid statement structure)

c. Lexical (no token matches ‘@’)

COMP6276 – Compilation Techniques


4.

5.

a.

b. T1 = b + c

T2 = -T1

T3 = a+T2

COMP6276 – Compilation Techniques


Op Arg 1 Arg 2 Result

+ b c T1
0

1
- T1 T2

2 + a T2

c. Triples

# Op Arg 1 Arg 2

(0) + b c

(1) - (0)

(2) + a (1)

d. Indirect Triples

# Op Arg 1 Arg 2

COMP6276 – Compilation Techniques


(10) + b c

(11) - (10)

(12) + a (11)

List pointers to table

# Statement

(1) (10)

(2) (11)

(3) (12)

COMP6276 – Compilation Techniques

Anda mungkin juga menyukai