You are on page 1of 6

2301955996 - Johanes Setiadi

2301953851 - Naufal Retyan Ajisasongko

2301957641 - Dylan Wijaya

2301957206 - Darwan

Tugas Kelompok ke-4

Minggu ke 9, Sesi ke 13

1. Perhatikan potongan program berikut ini:

int n=10, i, flag = 0, score=0;

for(i=2; i<=n/2; ++i)

if(n%i==0)

flag=1;

break;

if (flag==0)

score = 100;

else

score = 50;

a. Buatlah three address code dari potongan program diatas.

1. n = 10;

2. i = 0;

3. flag = 0;

COMP6199 – Software Engineering


4. score = 0;

5. i = 2;

6. if (i > n/2) goto 12

7. if (n%1 == 1) goto 9

8. flag = 1;

9. t1 = i + 1;

10. i = t1;

11. goto (6);

12. if (flag != 0) goto 14

13. score = 100;

14. score = 50;

b. Buatlah flow-graph dari three address code pada nomor a.

COMP6199 – Software Engineering


c. Lakukan optimisasi pada three address code tersebut menggunakan basic block yang
sudah dibuat pada nomor b.

1. 1. n = 5;

COMP6199 – Software Engineering


2. i = 0;

3. score = 0;

4. i = 2;

5. if (i > n) goto 13

6. if (n%1 == 1) goto 9

7. score = 100;

8. goto (10)

9. score = 50;

10. t1 = i + 1;

11. i = t1;

12. goto (6);

13. END

2. Buatlah compiler sederhana yang mana merupakan bagian dari front-end (tahap
analisis) atau back-end (tahap sintesis) compiler. Misalnya Anda membuat program
sederhana yang berperan sebagai Scanner, atau membuat parsing, dll. Anda bebas
menggunakan Bahasa pemrograman apapun.

import java.io.BufferedReader;

import java.io.InputStreamReader;

class Parser

public static void main(String[] args) throws Exception

int no, loc=0, i, j;

String str, stack="", temp;

String[][] productions;

COMP6199 – Software Engineering


BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter no. of productions: ");

no = Integer.parseInt(in.readLine());

productions = new String[no][2];

System.out.println("Enter the productions:");

for (i=0; i < no; i++)

System.out.print("LHS for production "+(i+1)+": ");

productions[i][0] = in.readLine();

System.out.print("RHS for production "+(i+1)+": ");

productions[i][1] = in.readLine();

System.out.println("The productions are:");

for (i=0; i < no; i++)

System.out.println(productions[i][0]+" -> "+productions[i][1]);

System.out.print("Enter a string: ");

str = in.readLine();

while (loc < str.length()-1)

temp = str.substring(loc, str.indexOf(' ', loc));

loc = str.indexOf(' ', loc)+1;

for (i=0; i < no; i++)

if (temp.equals(productions[i][1]))

temp = productions[i][0];

break;

COMP6199 – Software Engineering


}

stack = stack+temp;

System.out.println("Stack contents: "+stack);

for (i=0; i < no; i++)

if (stack.equals(productions[i][1]))

stack = productions[i][0];

break;

System.out.println("Stack contents: "+stack);

if (stack.equals(productions[0][0]))

System.out.println("Accepted.");

else

System.out.println("Rejected.");

COMP6199 – Software Engineering

You might also like