DIZNR INTERNATIONAL

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

https://www.gyanodhan.com/video/7B2.%20GATE%20CSEIT/Compiler%20Design/269.%20Short%20trick%20to%20find%20the%20given%20grammar%20is%20%28LL1%29%20or%20not%20in%20Compiler%20Design%20In%20English.mp4

 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

Step 2: Compute FOLLOW Sets

Step 3: Check LL(1) Condition

 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