- There are two sets we will compute to aid in parsing.
- First(α) is the set of terminals that begin strings derived from α
- Computing FIRST(α)
FIND_FIRSTS
- foreach α ∈ TERMINALS
- FIRST(α) = {α}
- foreach α ∈ NON_TERMINALS
- FIRST(α) ← {}
- repeat until no set changes
- foreach α ∈ NON_TERMINALS
- FIND_FIRST(α)
FIND_FIRST(X)
- foreach production X → Y1Y2 ... Yn
- i ← 1
- while i ≤ n do
- FIRST(X) ← FIRST(X) ∪ FIRST(Yi)
- if Yi =>* ε then
- i ← i + 1
- else
- i ← n+1
- if Y1Y2 ... Yn =>* ε then
- FIRST(X) ← FIRST(X) ∪ {ε}
- if X → ε then
- FIRST(X) ← FIRST(X) ∪ {ε}
- in short : all terminals are their own first.
- All nonterminals contain the terminals they first derive, plus possibly ε
- Derive first for
E → TE'
E' → +TE' | ε
T → FT'
T' → *FT' | ε
F → (E) | id
FIRST(i ) = {i} where i ∈ { (, ), id, +, *}
FIRST(F) = { (, id}
FIRST(T) = FIRST(F) = {(,id}
FIRST(E) = FIRST(T) = {(,id}
FIRST(T') = {*} ∪ {ε} = { *, ε}
FIRST(E') = {+} ∪ {ε} = { +, ε}
- The FOLLOW(A), for a nonterminal A, is the set of terminals a that can appear immediately to the right of A in some from.
- The set of a such that A =>* αAaβ
- For this discussion $ is the end of line marker.
- Algorithm
FIND_FOLLOW
- foreach α ∈ NON_TERMINALS
- FOLLOW(α) ← {}
- FOLLOW(S) ← {$}
- repeat until no set changes
- foreach α ∈ NON_TERMINALS
- FIND_FOLLOW(α)
FIND_FOLLOW(A)
- foreach production A → α B β
- FOLLOW(B) ← FOLLOW(B) ∪ (FIRST(β)-{ε})
- if ε ∈ FIRST(β) then
- FOLLOW(B) ← FOLLOW(B) ∪ FOLLOW(A)
- foreach production A → α B
- FOLLOW(B) ← FOLLOW(B) ∪ FOLLOW(A)
- With the above grammar.
FOLLOW(E) = {$}
Consider F → (E)
FOLLOW(E) = FOLLOW(E) ∪ FIRST()) ({)}) = {$, )}
Consider E → TE'
A → αTβ
FOLLOW(T) = FOLLOW(T) ∪ (FIRST(E')-{ε}) = {+}
since ε ∈ FIRST(E')
FOLLOW(T) = FOLLOW(T) ∪ FOLLOW(E) = { +,),$}
A → α E'
FOLLOW(E') = FOLLOW(E') ∪ FOLLOW(E) = {$,)}
E' → +TE'
E' → α T β
FOLLOW(T) = FOLLOW(T) ∪ (FIRST(E')-{ε}) = {+}
since ε ∈ FIRST(E')
FOLLOW(T) = FOLLOW(T) ∪ FOLLOW(E) = { +,),$}
(NO CHANGE)
T → FT'
T → αFβ
FOLLOW(F) = FOLLOW(T) ∪ (FIRST(T')-{ε}) = {*}
since ε ∈ FIRST(T')
FOLLOW(F) = FOLLOW(F) ∪ FOLLOW(F) = {*, +,),$}
A → α E'
FOLLOW(T') = FOLLOW(T') ∪ FOLLOW(T) = {+,),$}
E' → +TE'
E' → α T β
FOLLOW(T) = FOLLOW(T) ∪ (FIRST(E')-{ε}) = {+}
since ε ∈ FIRST(E')
FOLLOW(T) = FOLLOW(T) ∪ FOLLOW(E) = { +,),$}
(NO CHANGE)
T → FT'
T → αFβ