Prediction sets for grammar are defined in terms of lookahead sets for each of its non-terminals, which, in turn, rely on a data set for each product. Defining lookahead sets can help us determine if LL (1) is a grammar, and if so, what information do we need to create a recursive descent parser for it.
Definition: LOOKAHEAD (X → α) and LOOKAHEAD (X):
LOOKAHEAD(X -> α) = FIRST(α) U FOLLOW(X), if NULLABLE(α)
LOOKAHEAD(X -> α) = FIRST(α), if not NULLABLE(α)
LOOKAHEAD(X) = LOOKAHEAD(X -> α) U LOOKAHEAD(X -> β) U LOOKAHEAD(X -> γ)
FIRST (α) - , α, FOLLOW (X) - , X , NULLABLE (α) - , α ( ε). Torben Mogensen, . .
: NULLABLE (X):
NULLABLE(ε) = true
NULLABLE(x) = false, if x is a terminal
NULLABLE(αβ) = NULLABLE(α) and NULLABLE(β)
NULLABLE(P) = NULLABLE(α_1) or NULLABLE(α_2) or ... or NULLABLE(α_n),
if P is a non-terminal and the right-hand-sides
of all its productions are α_1, α_2, ..., α_n.
: FIRST (X):
FIRST(ε) = Ø
FIRST(x) = {x}, assuming x is a terminal
FIRST(αβ) = FIRST(α) U FIRST(β), if NULLABLE(α)
= FIRST(α), if not NULLABLE(α)
FIRST(P) = FIRST(α_1) U FIRST(α_2) U ... U FIRST(α_n),
if P is a non-terminal and the right-hand-sides
of all its productions are α_1, α_2, ..., α_n.
: FOLLOW (X):
a FOLLOW (X) , S , S ⇒ αX aβ, α β - (, ) .
: FOLLOW (X):
, X . , ( ), FOLLOW (X). , X (, A -> foo X), - , ε (, A -> foo X B B -> ε), , A , X (.. FOLLOW(A) ⊆ FOLLOW(X)).
. FOLLOW (X) Torben .
:
E -> n A
A -> E B
A -> ε
B -> + A
B -> * A
, NULLABLE FIRST :
NULLABLE(E) = NULLABLE(n A) = NULLABLE(n) ∧ NULLABLE(A) = false
NULLABLE(A) = NULLABLE(E B) ∨ NULLABLE(ε) = true
NULLABLE(B) = NULLABLE(+ A) ∨ NULLABLE(* A) = false
FIRST(E) = FIRST(n A) = {n}
FIRST(A) = FIRST(E B) U FIRST(ε) = FIRST(E) U Ø = {n} (because E is not NULLABLE)
FIRST(B) = FIRST(+ A) U FIRST(* A) = FIRST(+) U FIRST(*) = {+, *}
FOLLOW E' -> E $, $ " ". FOLLOW:
FOLLOW(E): Let β = $, so add the constraint that FIRST($) = {$} ⊆ FOLLOW(E)
Let β = B, so add the constraint that FIRST(B) = {+, *} ⊆ FOLLOW(E)
FOLLOW(A): Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(A).
Because NULLABLE(ε), add the constraint that FOLLOW(E) ⊆ FOLLOW(A).
Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(A).
Because NULLABLE(ε), add the constraint that FOLLOW(B) ⊆ FOLLOW(A).
Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(A).
Because NULLABLE(ε), add the constraint that FOLLOW(B) ⊆ FOLLOW(A).
FOLLOW(B): Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(B).
Because NULLABLE(ε), add the constraint that FOLLOW(A) ⊆ FOLLOW(B).
( ),
{+, *, $} ⊆ FOLLOW(E)
FOLLOW(E) ⊆ FOLLOW(A)
FOLLOW(A) = FOLLOW(B)
FOLLOW(E) = FOLLOW(A) = FOLLOW(B) = {+, *, $}.
LOOKAHEAD :
LOOKAHEAD(E -> n A) = FIRST(n A) = {n} because ¬NULLABLE(n A)
LOOKAHEAD(A -> E B) = FIRST(E B) because ¬NULLABLE(E B)
= FIRST(E) = {n} because ¬NULLABLE(E)
LOOKAHEAD(A -> ε) = FIRST(ε) U FOLLOW(A) because NULLABLE(ε)
= Ø U {+, *, $} = {+, *, $}
LOOKAHEAD(B -> + A) = FIRST(+ A) because ¬NULLABLE(+ A)
= FIRST(+) = {+} because ¬NULLABLE(+)
LOOKAHEAD(B -> * A) = {*} for the same reason
, LOOKAHEAD :
LOOKAHEAD(E) = LOOKAHEAD(E -> n A) = {n}
LOOKAHEAD(A) = LOOKAHEAD(A -> E B) U LOOKAHEAD(A -> ε) = {n} U {+, *, $}
LOOKAHEAD(B) = LOOKAHEAD(B -> + A) U LOOKAHEAD(B -> * A) = {+, *}
, LL (1), . (I.e., , , .)