Short trick method to find First of any grammar- Compiler Design
Short trick method to find First of any grammar- Compiler Design
Contents
✅ Short Trick to Find FIRST of Any Grammar (Compiler Design)
In Compiler Design, computing the FIRST set is essential for parsing table construction in LL(1) parsers. Here’s a short trick method with rules to find the FIRST set quickly and accurately.
🧠 What is FIRST?
The FIRST(X) set is the set of terminals that begin the strings derivable from symbol X (where X can be a terminal, non-terminal, or ε).
🔍 Short Trick Rules to Find FIRST
🟠 Rule 1: For terminals
If X is a terminal, then:
🟠 Rule 2: For ε (epsilon)
🟠 Rule 3: For non-terminals (A → α1 α2 α3…)
To find FIRST(A):
-
Go left to right in production A → α1 α2 α3…
-
Add FIRST(α1) to FIRST(A)
-
If FIRST(α1) contains ε, then check α2, then α3, etc.
-
Continue until a symbol does NOT have ε in its FIRST set.
-
If all symbols derive ε, then ε ∈ FIRST(A)
✨ Shortcut Flow (for A → α):
α is… | FIRST(A) contains… |
---|---|
Terminal a |
a |
ε | ε |
Non-terminal B |
FIRST(B), and if ε ∈ FIRST(B), check next symbol |
Sequence B C D... |
FIRST(B), and if ε ∈ FIRST(B), add FIRST(C), and so on |
🔢 Example 1:
Step-by-step:
-
FIRST(A) = { a, ε }
-
FIRST(B) = { b }
-
FIRST(S):
-
From
S → A B
-
FIRST(A) = { a, ε }, so check next
-
FIRST(B) = { b }
-
So FIRST(S) = { a, b }
-
🔢 Example 2:
Then:
-
FIRST(A) = { ε }
-
FIRST(B) = { ε }
-
FIRST(S) = { ε } ✅ (since all parts can derive ε)
🔢 Example 3:
-
FIRST(A) = { c, ε }
-
FIRST(S) = { c, b }
(Because A can be ε, so next terminalb
is also considered)
📝 Tips & Tricks Recap:
✅ Go left to right in RHS
✅ If you see a terminal, add it and stop
✅ If you see ε, skip and check next
✅ If all symbols can derive ε, then add ε to FIRST
✅ Don’t forget to union all FIRSTs if multiple rules exist
Want a short PDF chart, video link, or practice problems with solutions? Let me know!