ToolSphere

FIRST and FOLLOW Calculator

Computer science

Compute FIRST and FOLLOW sets for every non-terminal in a context-free grammar, with epsilon handled correctly.

Type a grammar to compute the tokens that can start and follow each nonterminal.

FIRST(E) = {(, id}; FOLLOW(E) = {$, )}. All non-terminals are listed below.

Non-terminalFIRSTFOLLOW
E(, id$, )
E'+, ε$, )
T(, id$, ), +
T'*, ε$, ), +
F(, id$, ), *, +

Grammar read back

  • E → T E'
  • E' → + T E'
  • E' → ε
  • T → F T'
  • T' → * F T'
  • T' → ε
  • F → ( E )
  • F → id

The start symbol is E, so its FOLLOW set always contains $. ε appears in a FIRST set when the non-terminal can derive the empty string; it never appears in a FOLLOW set.

On this browser only

Give feedback

Did this tool help?

Tell us what worked, what didn’t, or what you’d like next.

Opens Google Forms in a new tab with this tool’s name filled in. No sign-in or email required. Feedback is optional.

How to use FIRST and FOLLOW Calculator

  1. 1Type the grammar, one rule per line, as E -> T E' with | between alternatives.
  2. 2Use ε (or the word epsilon) for an empty production.
  3. 3Read the FIRST and FOLLOW set for every non-terminal in the table.
  4. 4Move to the LL(1) parser tab to turn those sets into a parsing table.

About FIRST and FOLLOW Calculator

FIRST and FOLLOW Calculator

Computes both sets for every non-terminal of a context-free grammar, which is the step every LL(1) parsing table is built on. The first symbol of the first rule is taken as the start symbol.

What each set means

FIRST(X) is the set of terminals that can begin a string derived from X. It answers the parser's real question: seeing this token next, which production could possibly apply?

FOLLOW(A) is the set of terminals that can appear immediately after A in some derivation. It matters only because of ε: when A can vanish, the parser must decide what to do using whatever comes after A instead, so the production for ε is selected on FOLLOW(A).

Where the marks are usually lost

ε propagates through a sequence. For a rule A → B C, FIRST(A) includes FIRST(B); but if B is nullable you must also add FIRST(C), and only if C is nullable as well does ε itself belong to FIRST(A). Stopping at the first symbol is the single most common error.

ε never belongs to a FOLLOW set. FOLLOW contains terminals that can actually appear after the non-terminal, plus the end marker $. Since ε is not a terminal that can be read, it cannot follow anything.

The start symbol's FOLLOW always contains $. Whatever else is in there, the end of input can always follow the start symbol.

FOLLOW is not FIRST of the next thing when that thing is nullable. For A → B C, FOLLOW(B) gets FIRST(C) minus ε — and if C is nullable, it also gets everything in FOLLOW(A).

From these sets to a parsing table

For each production A → α, the entry goes under every terminal in FIRST(α); if α is nullable, it also goes under every terminal in FOLLOW(A). Two productions landing in the same cell means the grammar is not LL(1) — usually left recursion or a common prefix that needs factoring. The LL(1) parser tab does that construction and names the conflicts.

Where this fits

FIRST and FOLLOW sets are the input to both families of parsers. The LL(1) parser builds its parsing table from them, and the SLR(1) table in the LR parser puts reduce actions only under the FOLLOW set of each rule's left side. Compute the sets here, then check your table there.

Guides and tutorials

student

FIRST and FOLLOW Sets Example, Step by Step

FIRST and FOLLOW sets for the classic expression grammar, worked rule by rule, then the LL(1) parsing table and a full parse of id + id * id.

Read guide →
student

LR(1) vs LALR(1) Parsing and Conflicts

Learn how LR(1) items work, how LALR(1) merges identical cores, and why state merging causes reduce/reduce conflicts that LR(1) avoids, with worked tables.

Read guide →
student

Recursive Descent Parser in C: Worked Example

Generate a predictive C parser for expressions, inspect its rewritten grammar and call trace, then compile and run accepted and rejected inputs.

Read guide →
student

CYK Algorithm Example with CNF Conversion

Step-by-step CYK algorithm tutorial: convert a CFG to Chomsky normal form (CNF), fill the triangular parse table for baaba, and verify string membership.

Read guide →
student

LALR and CLR Parsing Table Worked Example

Build canonical LR(1) items, merge ten states into seven LALR states, read both tables, and see why an assignment grammar is LALR but not SLR.

Read guide →
student

Left Recursion and Left Factoring Examples

Worked direct and indirect left-recursion removal, left factoring and LL(1) tables, including why factoring dangling else does not remove its conflict.

Read guide →
student

SLR Parsing Table Example With LR(0) Items

Build the canonical LR(0) items and the SLR(1) parsing table for the expression grammar, see why LR(0) has conflicts, and trace a parse of id * id + id.

Read guide →
student

Three Address Code: Examples and Quadruples

Three-address code step by step: expressions, quadruples, triples, indirect triples, if-else and while loops, and the DAG, with textbook examples.

Read guide →

Frequently asked questions

Can ε appear in a FOLLOW set?

No. FOLLOW holds terminals that can genuinely appear after the non-terminal, plus the end marker $. ε is the empty string, not a terminal that can be read, so it never belongs there. It appears only in FIRST sets, to record that the non-terminal can derive nothing at all.

Why does FIRST of my rule include symbols from the second position?

Because the first symbol is nullable. For A -> B C where B can derive ε, a string from A can begin with anything in FIRST(C), so FIRST(A) includes both. ε itself is only added when every symbol in the sequence is nullable.

Why is $ in the FOLLOW set of my start symbol?

By definition: input ends after the start symbol derives the whole string, so the end marker can always follow it. Every correct FOLLOW computation seeds the start symbol with $ before anything else.

How do I use these sets to build an LL(1) table?

For A -> alpha, put the production under each terminal of FIRST(alpha); if alpha is nullable, also put it under each terminal of FOLLOW(A). Two productions in one cell means the grammar is not LL(1). The LL(1) parser tab builds the table and reports conflicts.

Does the tool need the grammar to be LL(1)?

No. FIRST and FOLLOW are defined for any context-free grammar, including left-recursive and ambiguous ones. Whether the grammar is LL(1) is a separate question answered when the parsing table is built.

Have a suggestion for FIRST and FOLLOW Calculator?Share your feedback ↗