370 likes | 1.46k Views
Pergerakan LL(1) Parser dengan input abba. S aBa B bB | . stack input output. abba$. $. S. Pergerakan LL(1) Parser dengan input abba. a. S aBa B bB | . S. stack input output $S a bba$ . S aBa. $ S. S aBa. $ aBa. $ aB a. $. a bba$. Pop( a ).
E N D
Pergerakan LL(1) Parser dengan input abba S aBa B bB | stackinputoutput abba$ $ S
Pergerakan LL(1) Parser dengan input abba a S aBa B bB | S stackinputoutput $S abba$ S aBa $S S aBa $aBa $aBa $ abba$ Pop(a) bba$ $aB
Pergerakan LL(1) Parser dengan input abba b S aBa B bB | stackinputoutput $Sabba$ B B bB S aBa $ $aBa Pop(a) abba$ $aB $aB B bB bba$ bba$ ba$ ba$ $a Bb b Pop(b) $aB a$
Pergerakan LL(1) Parser dengan input abba a S aBa B bB | stackinputoutput $Sabba$ B B S aBa $ $aBa Pop(a) abba$ $aB $aB B bB bba$ bba$ ba$ ba$ Pop(b) $a Bb b $aB $aB a$ a$ B $a $a Pop(a) a$ $ $
Pergerakan LL(1) Parser dengan input abba S aBa B bB | stackinputoutput $Sabba$ S aBa $ $aBa Pop(a) abba$ $aB $aB B bB bba$ bba$ ba$ ba$ Pop(b) $a Bb b $aB $aB a$ a$ B $a $a Pop(a) a$ $ $ $ $ Accepted Parsing Sukses
Pergerakan parsing dengan input abba Derivasi language: Input: a b b a S aBa B bB Parse tree: S B a a B b B b
Contoh Top down parsing Tabel parsing: id id id id id + + $ $ E E TE’ Input: id + id E’ E’ E’ E’ +TE’ T T T FT’ T FT’ T’ T’ T’ T’ F F Pergerakan LL(1) Parsing F id F id stackinputoutput Parse tree: E $E E TE’ id+id$ $E’T id+id$ T FT’ $E’T’F F id id+id$ E’ T $E’T’id id+id$ Pop(id) $E’T’ T’ +id$ F T E’ + $E’ E’ +TE’ +id$ $E’T+ Pop(+) +id$ $E’T id$ T FT’ T’ F id $E’T’F id$ F id $E’T’id id$ Pop(id) $ T’ $E’T’ id E’ $E’ $ $ $ accepted
Contoh mencari FIRST E TE = First(T) = First(TE) First(E) = First(FT) = First(F) T FT First(T) F(E) id = { ( , id } First(F) = First(‘(E)’) | First(id) = First(+TE) | First() = { + , } E +TE First(E) = First(FT) | First() = { , } TFT First(T)
Contoh mencari FOLLOW (pada non terminal) Derivasi: E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id (Rule 1) Follow(E) = { $ } E start simbol Rules: 1. If S is the start symbol $ is in FOLLOW(S) 2. If A B is a production rule everything in FIRST() is FOLLOW(B) except 3. If ( A B is a production rule ) or ( A B is a production rule and is in FIRST() ) everything in FOLLOW(A) is in FOLLOW(B).
Contoh mencari FOLLOW (pada non terminal) Follow(E) = { $ } E start simbol Follow(E) = { ), $ } F (E) Follow(E) = { ) } (Rule 2) E TE Follow(T) = Follow(E) = { $, ) } (Rule 3) E • Follow(T)= { +, ), $ } Follow(T) = First(E) - E +TE (Rule 2) = { + } T FT’ Follow(F) = Follow(T) = {+, ), $ } (Rule 3) T’ • Follow(F)={*,+, ), $} Follow(F) = First(T) - T’ *FT’ = {*} (Rule 2) Rules: 1. If S is the start symbol $ is in FOLLOW(S) 2. If A B is a production rule everything in FIRST() is FOLLOW(B) except 3. If ( A B is a production rule ) or ( A B is a production rule and is in FIRST() ) everything in FOLLOW(A) is in FOLLOW(B).
Contoh mencari FOLLOW (pada non terminal) E TE’ Follow(E’) = Follow(E) = { ), $ } (rule 3) dengan = T T FT’ Follow(T’) = Follow(T) = { +, ), $ } (rule 3) dengan = F Rules: 1. If S is the start symbol $ is in FOLLOW(S) 2. If A B is a production rule everything in FIRST() is FOLLOW(B) except 3. If ( A B is a production rule ) or ( A B is a production rule and is in FIRST() ) everything in FOLLOW(A) is in FOLLOW(B).
Pembuatan tabel parsing top-down First(E) = { (, id } First(E’) = { +, } First(T) = { (, id } First(T’) = { , } First(F) = { (, id } Follow(E) = { $, ) } Follow(E’) = { $, ) } Follow(T) = { +, ), $ } Follow(T’) = { +, ), $ } Follow(F) = {+, *, ), $ } Algoritma pembuatan tabel parsing: • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries.
Algoritma pembuatan tabel parsing: E TE’ E’ +TE’ E’ T FT’ T’ *FT’ T’ F (E) F id • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. dengan aturan 1 First(TE’) = { (, id }
Algoritma pembuatan tabel parsing: E’ +TE’ E’ T FT’ T’ *FT’ T’ F (E) F id • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. dengan aturan 1 First(+TE’) = { + } E TE’ E TE’
Algoritma pembuatan tabel parsing: E’ T FT’ T’ *FT’ T’ F (E) F id • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. dengan aturan 2 Follow(E’) = { $, ) } E TE’ E TE’ E’ +TE’
Algoritma pembuatan tabel parsing: T FT’ T’ *FT’ T’ F (E) F id • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. dengan aturan 1 First(FT’) = { (, id } E TE’ E TE’ E’ +TE’ E’ E’
Algoritma pembuatan tabel parsing: T’ *FT’ T’ F (E) F id • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. dengan aturan 1 First(*FT’) = { * } E TE’ E TE’ E’ +TE’ E’ E’ T FT’ T FT’
Algoritma pembuatan tabel parsing: • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. T’ F (E) F id dengan aturan 2 & 3 Follow(T’) = { +, ), $ } E TE’ E TE’ E’ +TE’ E’ E’ T FT’ T FT’ T’ *FT’
Algoritma pembuatan tabel parsing: • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. F (E) F id dengan aturan 1 First(“(E)”) = { ( } E TE’ E TE’ E’ +TE’ E’ E’ T FT’ T FT’ T’ T’ *FT’ T’ T’
Algoritma pembuatan tabel parsing: • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. F id dengan aturan 1 First(id) = { id } E TE’ E TE’ E’ +TE’ E’ E’ T FT’ T FT’ T’ T’ *FT’ T’ T’ F (E)
Algoritma pembuatan tabel parsing: • for each production rule A of a grammar G • 1. for each terminal a in FIRST() add A to M[A,a] • 2. If in FIRST() for each terminal a in FOLLOW(A) add A to M[A,a] • 3. If in FIRST() and $ in FOLLOW(A) add A to M[A,$] • All other undefined entries of the parsing table are error entries. Selain itu adalah error error error error error E TE’ E TE’ error error error E’ +TE’ E’ E’ error error error error T FT’ T FT’ error error T’ T’ *FT’ T’ T’ error error error error F id F (E)
Self Assessment 1. Jika diketahui grammar dengan derivasi sbb: S aBa B bB | Maka nilai First(B) adalah: a.) a b.) b c.) $ d.) S 2. Dari soal no.1 nilai dari First(S) adalah: a.) a b.) b c.) $ d.) S 3. Dari soal no. 1 nilai dari Follow(S) adalah: a.) a b.) b c.) $ d.) S 4. Simbol yang digunakan sebagai tanda akhir input dalam pergerakan parsing adalah: a.) a b.) b c.) $ d.) S
Summary • Top down parsing melakukan parsing dari start simbol sehingga terbentuk parse tree • Untuk melakukan top down parsing dengan stack pertama-tama isi stack adalah $ dan start simbol • Untuk membuat tabel parsing harus dicari dulu nilai first dan follow dari setiap non terminal simbol.