Compiler Design/ Short trick to find the given grammar is (LL1) or not in Compiler Design In English
Contents
- 1 How to Quickly Check if a Grammar is LL(1) – Compiler Design Short Trick
- 2 Quick Steps to Check if a Grammar is LL(1)
- 3 Step 1: Compute First Sets for Each Production
- 4 Step 2: Compute Follow Sets for Each Non-Terminal
- 5 Step 3: Apply the LL(1) Condition
- 6 Short Trick to Check LL(1) Grammar
- 7 Example to Check LL(1)
- 8 Step 1: Compute FIRST Sets
- 9 Step 2: Compute FOLLOW Sets
- 10 Step 3: Check LL(1) Condition
- 11 When is a Grammar NOT LL(1)?
- 12 Compiler Design/ Short trick to find the given grammar is (LL1) or not in Compiler Design In English
- 13 COMPILER DESIGN (R15A0512)
How to Quickly Check if a Grammar is LL(1) – Compiler Design Short Trick
LL(1) Grammar is a type of context-free grammar (CFG) used in top-down parsing. To determine whether a given grammar is LL(1), follow this simple short trick based on First and Follow sets.
Quick Steps to Check if a Grammar is LL(1)
Step 1: Compute First Sets for Each Production
- FIRST(A) = Set of terminals that appear at the beginning of the derivations of A.
- If A → ε, then include FOLLOW(A) in FIRST(A).
Step 2: Compute Follow Sets for Each Non-Terminal
- FOLLOW(A) = Set of terminals that can appear immediately after A in some derivation.
- If the start symbol is S, then $ (End-of-input marker) is in FOLLOW(S).
Step 3: Apply the LL(1) Condition
For the grammar to be LL(1), it must satisfy the following condition for every non-terminal A:
For two productions A → α | β:
FIRST(α) ∩ FIRST(β) = ∅ (No common terminal symbols)
If ε ∈ FIRST(α), then FIRST(β) ∩ FOLLOW(A) = ∅
If the condition holds for all non-terminals, the grammar is LL(1).
Short Trick to Check LL(1) Grammar
Step 1: Compute FIRST & FOLLOW sets.
Step 2: Check if any FIRST sets overlap for a single non-terminal.
Step 3: If a production has ε, ensure FOLLOW(A) and FIRST(A) are disjoint.
Step 4: If all conditions hold, it is LL(1); otherwise, it is NOT LL(1).
Example to Check LL(1)
Example Grammar:
Step 1: Compute FIRST Sets
- FIRST(S) = {a, b}
- FIRST(A) = {a, ε}
- FIRST(B) = {b}
Step 2: Compute FOLLOW Sets
- FOLLOW(S) = { $ }
- FOLLOW(A) = { $ }
- FOLLOW(B) = { $ }
Step 3: Check LL(1) Condition
- FIRST(A) and FIRST(B) do not overlap
- A → ε, but FIRST(A) ∩ FOLLOW(A) = ∅
Since all conditions are satisfied, this grammar is LL(1).
When is a Grammar NOT LL(1)?
If FIRST(α) ∩ FIRST(β) ≠ ∅, the grammar is not LL(1).
If ε is in FIRST(A) but FIRST(A) ∩ FOLLOW(A) ≠ ∅, the grammar is not LL(1).
Would you like a practice problem or more examples?