Top-down Parsing
- Pastikan tidak ada left recursion dan left factoring pada grammar
- Tentukan nilai First (karakter pertama pada setiap hasil produksi)
- Tentukan nilai Follow
- Buatlah tabel parsing
- Lihat nilai First
- Jika ada empty di first maka lihat follow
- Parsing
Left Factoring
A --> Ba | Bcd | ef
B (a | cd) | ef
A --> B A’ | ef
A’ --> a | cd
---
B --> abc | ad | eq
a (bc | d) | eq
B --> aB’ | eq
B’ --> bc | d
---
D --> abd | bc | abc
ab (d | c) | bc
D --> abD’ | bc
D’ --> d | c
---
E --> bad | bac | b
b (ad | ac | ε)
E --> bE’
E’ --> ad | ac | ε
A (d | c) | ε
E’ --> AE’’ | ε
E’’ --> d | c
Conclusion
E --> bE’
E’’ --> AE’’ | ε
E’’ --> d | c
Left Recursion
A --> Ab | cd
A --> cdA’
A’ --> bA’ | ε
---
D --> bd | De | ba
β α β
D --> bdD’ | baD’
D’ --> eD’ | ε
---
F --> aFc | Fde | a
β α β
F --> aFcF’ | aF’
F’ --> deF’ | ε
Follow
Algoritma:
- if
(S -> ab | c)and(S is start)then $ is the follow - if
(A -> αBβ)then first ofβis the follow ofB, excluding the empty first - a. if
(A -> αB)then the follow ofAis the follow ofB
b. if(A -> αBβ)and(first β is empty)then the follow ofAis the follow ofB
note:
- β is the rest of the grammar
- if there is only one character then pad left with ε
Cheat
Cari dari semua hasil produksi yang berada di belakang (kanan) X dan bukan variabel.
Ex
