Calculate Follow Set - Oya-Learning-Notes/Compilers GitHub Wiki
Follow Set
First, for any production: $A \to \alpha X \beta, \quad \alpha, \beta \in V^*$ we have:
$$ follow(X) \supseteq first(\beta) - \varepsilon $$
Second, if $\varepsilon \in first(\beta)$:
$$ follow(X) \supseteq follow(A) $$
Thrid, if $S$ is the start symbol,
folllow(S) \supseteq \#
Note that rule 2 could be chaining when dealing with a long sequence, consider example:
$$ A \to abc $$
We first have:
$$ follow(c) \supseteq follow(A) $$
If $\varepsilon \in first(c)$:
$$ follow(b) \supseteq follow(A) $$
If additionally, $\varepsilon \in first(b)$:
$$ follow(a) \supseteq follow(A) $$
Note: $\varepsilon$ is not allowed in follow set.
Intuition
It’s more like that the element of follow set is propagating from LHS to RHS through the rule 2.
In contraction, the first set also have similar recursive or chaining rules, but it’s more like the elements is propagating from RHS to LHS.