09 / 1 - 9
LR - PARSER
KONSEP DASAR Fungsi ini digunakan untuk menangani CFG yang berukuran besar dan teknik sering disebut dengan LR (k) parsing, dimana : L : left to right R : konstruksi derivasi rightmost K : bilangan input simbol
Teknik Kompilasi
09 / 2 - 9
Komponen LR-Parser : input, output, stack, program driver dan parsing table (action dan goto) Metode Kerja : Program parsing membaca karakter dari input buffer satu setiap waktu Progam menggunakan sebuah stack untuk menyimpan suatu string dari suatu bentuk : s0x1 s1x2smxm dimana : sm = top of stack xi = simbol grammar si = state
Teknik Kompilasi
09 / 3 - 9
LR parsing program
Teknik Kompilasi
09 / 4 - 9
ALGORITMA LR-PARSING
set ip to point to first simbol of w$; repeat forever begin let s be the state on top of the stack and a the symbol pointed to by ip; if action[s, a] = shift s then begin push a then s on top of the stack; advance ip to the next input simbol end else if action[s, a] = reduce A then begin pop 2 * || symbol off then stack; let s be the state now on top of the stack; output the production A end else if action [s, a] = accept then return else error () end
Teknik Kompilasi
09 / 5 - 9
Parsing Table :
State 0 1 2 3 4 5 6 7 8 9 10 11 id S5 + S6 R2 R4 S5 R6 S5 S5 S6 R1 R3 R5 S7 R3 R5 R6 S4 S4 S11 R1 R3 R5 R1 R3 R5 Action * ( S4 S7 R4 S4 R6 R6 9 3 10 ) R2 R4 $ acc R2 R4 8 2 3 E 1 Goto T 2 F 3
Teknik Kompilasi
09 / 6 - 9
Catatan : Si = shift dan push state i Rj = reduksi dengan produksi nomor j acc = accept kosong = error
Teknik Kompilasi
09 / 7 - 9
LR-GRAMMAR
Suatu Grammar dimana kita dapat membuat parsing table, LR-Grammar menggunakan shift-reduce untuk k simbol input biasa disebut LR(k) Grammar. Sebuah LR(0) item grammar G adalah suatu produksi dari grammar G dengan penempatan dot pada pada posisiposisi tertentu di rightside Contoh : G : A XYZ menghasilkan 4 item yaitu : A .XYZ A X.YZ A XY.Z A XYZ.
Teknik Kompilasi
09 / 8 - 9
OPERASI CLOSURE 1. Setiap item dalam I ditambahkan dalam closure (I) 2. Jika A B dalam closure(I) dan B Contoh : E E E E + T | T T T * F | F F (E) | id Operasi closure dengan LR(0) adalah :
I0 : E E E E+T E T T T*F T F F (E) F id I1 : E E E E +T I5: F id I6: E E+ T T T*F T F F (E) F id I7: T T* F F (E)
canonical
Teknik Kompilasi
09 / 9 - 9
F id I2 : E T T T *F I3 : T F I4 : F ( E) E E+T E T T T*F T F F (E) F id I8: F (E ) E E +T I9: F E+T T T *F I10: T T*F I11: F (E)