First set
The FIRST set of a symbol in a grammar is the set of tokens that can appear at the beginning of
some string derived from that symbol. For each non-terminal symbol (e.g., S
, E
), the
FIRST set includes:
- Terminal tokens that appear first in any rule for that symbol.
- Tokens from the FIRST set of other symbols if those symbols can appear first in the derivation.
Constructing the FIRST set
Follow set
The FOLLOW set of a symbol in a grammar represents the set of tokens that can appear immediately after that symbol in some derivation of the grammar. This set is crucial for constructing parsers, particularly for handling situations like predictive parsing (LL parsers).
- End-of-input token (
$
): The start symbol always has$
in its FOLLOW set since it should be followed by the end of the input. - Tokens after a symbol: If a symbol
S
is followed by some tokent
in a rule,t
will be in the FOLLOW set ofS
. - Tokens from FIRST sets: If a symbol
S
is followed by another symbolA
, andA
can produce some tokent
(i.e.,t
is in the FIRST set ofA
), thent
will also be in the FOLLOW set ofS
. - Propagation from FOLLOW sets: If a symbol
S
is followed by another symbolA
at the end of a rule, then all tokens in the FOLLOW set ofA
are added to the FOLLOW set ofS
.
Constructing the FOLLOW set
For more info visit the dotlr library docs