Diberikan Grammar sebagai berikut : E E + E E E * E E (E) E id lakukan shift-reduce parsing untuk input string:
a. b. c.
1) 2) 3) 4) 5) 6)
E E + T E T TT F T F F (E)
$ acc R2 R4
E 1
Goto T 2
F 3
R2 R4 R6
8 R6
2 9
3 3 10
S11
9 10 11
R1 R3 R5
S7 R3 R5
R1 R3 R5
R1 R3 R5
dengan menggunakan algoritma LR parsing, tentukan pergerakan LR parser untuk input string: a) id * id + id. b) id * (id + id) c) id + id + id 3. Diketahui grammar sebagai berikut: BE BE or BT | BT BT BT and BF | BF BF not BF | ( BE ) | a | b Ditanyakan: a) Buatlah augmented grammar di atas b) Tentukan himpunan item kernel dan non kernel c) Buatlah table SLR dari grammar di atas d) Tunjukan pergerakan bootom up parsing dengan input a and b or not a 4. Kerjakan soal pada buku wajib 1 (Aho) Nomor 4.46 halaman 274
JAWAB: 1.
a.
1 2 3 4 5 6 7 8 9 10 11 $ id $ E$ +E$
id1 + id2 + id3 Action shift reduce by E id shift shift reduce by E id shift shift reduce by E id reduce by E E*E reduce by E E+E accept
b.
1 2 3 4 5 6 7 8 9 10 11 $ id $ E$ *E$
Action shift reduce by E id shift shift reduce by E id shift shift reduce by E id reduce by E E*E reduce by E E+E accept
c.
1 2 3 4 5 6 7 8 9 10 11 12 13 $ ($ ( id $ (E$ (E*$
id1 * id2 * id3 Action shift shift reduce by E id shift shift reduce by E id shift reduce by E E*E reduce by E (E) shift shift reduce by E id reduce by E E+E
14
E$
accept
2. a) id * id + id stack 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0$ 5id0$ 3F0$ 2T0$ 7*2T0$ 5id7*2T0$ 10F7*2T0$ 2T0$ 1E0$ 6+1E0$ 5 id6+1E0$ 3F6+1E0$ 9T6+1E0$ 1E0$ input id * id + id$ * id + id$ * id + id$ * id + id$ id + id$ + id$ + id$ + id$ + id$ id$ $ $ $ $ shift reduce F id reduce T F shift shift reduce F id reduce T T * F reduce E T shift shift reduce F id reduce T F reduce E E + T accept action
b) id * (id + id) stack 1 2 3 4 5 0$ 5id0$ 3F0$ 2T0$ 7*2T0$ input id * ( id + id ) $ * id + id$ * id + id$ * id + id$ ( id + id ) $ shift reduce F id reduce T F shift shift action
6 7 8 9 10 11 12 13 14 15 16 17 18 19
id + id ) $ + id ) $ + id ) $ + id ) $ + id ) $ id ) $
shift reduce F id reduce T F reduce E T shift shift reduce F id reduce T F reduce E E + T shift reduce F (E) reduce T T * F reduce E T accept
3. a) id + id + id stack 1 2 3 4 5 6 7 8 9 10 11 12 13 0$ 5id0$ 3F0$ 2T0$ 1E0$ 6+1E0$ 5id6+1E0$ 3F6+1E0$ 9T6+1E0$ 1E0$ 6+1E0$ 5id6+1E0$ 3F6+1E0$ input id + id + id$ + id + id$ + id + id$ + id + id$ + id + id$ id + id$ + id$ + id$ + id$ + id$ id $ $ $ shift reduce F id reduce T F reduce E T shift shift reduce F id reduce T F reduce E E + T shift shift reduce F id reduce T F action
14 15
9T6+1E0$ 1E0$
$ $
reduce E E + T accept
3.Diketahui grammar sebagai berikut: BE BE or BT | BT BT BT and BF | BF BF not BF | ( BE ) | a | b Ditanyakan: e) Buatlah augmented grammar di atas f) Tentukan himpunan item kernel dan non kernel g) Buatlah table SLR dari grammar di atas h) Tunjukan pergerakan bootom up parsing dengan input string not(a or b) and a a) Augmented gramar: 0. BE BE 1. BE BE or BT 2. BE BT 3. BT BT and BF 4. BT BF 5. BF not BF 6. BF (BE) 7. BF a 8. BF b b) Item kernel: 1. BE .BE 2. BE BE. 3. BE BE. or BT 4. BE BT. 5. BT BT. and BF 6. BT BF. 7. BF not. BF 8. BF (.BE) 9. BF a. 10. BF b. 11. BE BE or. BT 12. BT BT and. BF 13. BF not BF. 14. BF (BE.) 15. BE BE or BT. 16. BT BT and BF. Item non kernel: 0. 1. 2. 3. 4. 5. 6. 7. BE .BE or BT BE .BT BT .BT and BF BT .BF BF .not BF BF .(BE) BF .a BF .b
17. BF (BE). Himpunan Follow: Follow(BE) = { or, ), $ } Follow(BT) = {and, or, ), $ } Follow(BF) = {and, or, ), $ }
Operasi GOTO: I6 BF a. a I7 BF b.
I9 I12 and BE BE or BT. BT BT. and BF I1 BE BT BF not ( I2 BE BE. BE BE. or BT BE BT. BT BT. and BF
BT or
I0
I3 not I 4 ( a b I5 I6 I7
and
I6
I2 I3 I4
BE
I11
BF (BE.) BE BE. or BT b I7 or I8
I14 BF (BE).
c) Tabel LR Parsing:
State 0 1 2 3 4 5 6 7 8 9 10 11 12 13 a S6 b S7 not S4 Action ( or S5 S8 R2 R4 S5 S5 R7 R8 S5 S5 R5 S8 R1 R3 and S9 R4 R7 R8 R5 S9 R3 ) R2 R4 R7 R8 R5 S14 R1 R3 $ ACC R2 R4 11 R7 R8 12 R5 R1 R3 3 13 2 10 3 BE 1 Goto BT 2 BF 3
S6 S6 S6 S6
S7 S7 S7 S7
S4 S4 S4 S4
14
R6
R6
R6
R6
d) Tabel pergerakan parsing dengan input string not(a or b) and a N Stack Input Action o 1 0 not ( a or b ) S4, push(not), push(4), geser ip and a $ 2 0 not 4 ( a or b ) and a S5, push( ( ), push(5), geser ip $ 3 0 not 4 ( 5 a or b ) and a S6, push(a), push(6), geser ip $ 4 0 not 4 ( 5 a 6 or b ) and a $ R7, BF a, pop2x, push(BF), goto(5,BF) 5 0 not 4 ( 5 BF 3 or b ) and a $ R4, BT BF, pop2x, push(BT), goto(5,BT) 6 0 not 4 ( 5 BT 2 or b ) and a $ R2, BE BT, pop2x, push(BE), goto(5,BE) 7 0 not 4 ( 5 BE 11 or b ) and a $ S8, push(or), push(8), geser ip 8 0 not 4 ( 5 BE 11 or b ) and a $ S7, push(b), push(7), geser ip 8 9 0 not 4 ( 5 BE 11 or ) and a $ R8, BF b, pop2x, push(BF), 8b7 goto(8,BF) 1 0 not 4 ( 5 BE 11 or ) and a $ R4, BT BF, pop2x, push(BT), 0 8 BF 3 goto(8,BT) 1 0 not 4 ( 5 BE 11 or ) and a $ R1, BEBE or BT, pop6x, 1 8 BT 12 push(BE), goto(5,BE) 1 0 not 4 ( 5 BE 11 ) and a $ S14, push( ) ), push(14), geser 2 ip 1 0 not 4 ( 5 BE 11 ) and a $ R6, BF (BE), pop6x, push(BF), 3 14 goto(4,BF) 1 0 not 4 BF 10 and a $ R5, BF not BF, pop4x, 4 push(BF), goto(0,BF) 1 0 BF 3 and a $ R4, BT BF, pop2x, push(BT), 5 goto(0,BT) 1 0 BT 2 and a $ S9, push(and), push(9), geser ip 6 1 0 BT 2 and 9 a $ S6, push(a), push(6), geser ip 7 1 0 BT 2 and 9 a 6 $ R7, BF a, pop2x, push(BF), 8 goto(9,BF) 1 0 BT 2 and 9 BF 13 $ R3, BT BT and BF, pop6x, 9 push(BT), goto(0, BT) 2 0 BT 2 $ R2, BE BT, pop2x, push(BE), 0 goto(0,BE) 2 0 BE 1 $ ACC 1