Compiler Design/ Short trick to find the given grammar is (LL1) or not in Compiler Design In English

Compiler Design/ Short trick to find the given grammar is (LL1) or not in Compiler Design In English



play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

 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

  1. FIRST(A) = Set of terminals that appear at the beginning of the derivations of A.
  2. If A → ε, then include FOLLOW(A) in FIRST(A).

Step 2: Compute Follow Sets for Each Non-Terminal

  1. FOLLOW(A) = Set of terminals that can appear immediately after A in some derivation.
  2. 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:

SaA | bB
Aa | ε
Bb

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?

Compiler Design/ Short trick to find the given grammar is (LL1) or not in Compiler Design In English



Leave a Reply

Your email address will not be published. Required fields are marked *

error: