Predictive Parsing with FIRST and FOLLOW Sets
Compute lookahead choices for nullable grammar alternatives and diagnose why two productions compete for one table entry.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
FIRST tells which token may start a derivation
For \(S\to A b\) and \(A\to a\mid\epsilon\), FIRST of \(A\) includes \(a\) and the empty case. FIRST of \(A b\) includes \(a\) and \(b\), since a nullable \(A\) may leave \(b\) first. Repeatedly propagate token sets through production prefixes until they stop growing. This is a finite worklist fixed point.
FOLLOW tells which token can come next if the variable vanishes
Here \(b\) belongs to FOLLOW of \(A\). A predictive parser seeing \(a\) uses the first alternative of \(A\). On \(b\) it chooses the empty alternative. A table entry would conflict if two alternatives predicted the same lookahead, including FIRST–FOLLOW overlap for a nullable branch. Refactoring a common prefix can expose the later distinction. Context-free grammars supply the language structure. LR parsing delays some decisions until more of a production has already been read.[1][2]