GateMCQSCIENTIFIC PREP LAB
PlanSign Up
INTERMEDIATEFrequently Tested 4 min read 20 views

LL(1) & LR(0), SLR(1), LALR(1), CLR(1) Parsing Power & Conflicts

FIRST/FOLLOW rules, parsing hierarchy Venn relationships, state count equality between SLR(1) and LALR(1), and ambiguity.

LL(1) Grammar Necessary & Sufficient Conditions

  1. Grammar must be free of Left Recursion (an left-recursive grammar can never be LL(k)LL(k)).
  2. For every non-terminal A→α∣βA \to \alpha \mid \beta:
    • FIRST⁡(α)∩FIRST⁡(β)=∅\operatorname{FIRST}(\alpha) \cap \operatorname{FIRST}(\beta) = \emptyset
    • If ϵ∈FIRST⁡(α)\epsilon \in \operatorname{FIRST}(\alpha), then FIRST⁡(β)∩FOLLOW⁡(A)=∅\operatorname{FIRST}(\beta) \cap \operatorname{FOLLOW}(A) = \emptyset.
Common Exam Mistake

Ambiguous Grammars: An ambiguous grammar can NEVER be LL(k)LL(k) or LR(k)LR(k) for any k≥0k \ge 0! Also, when merging CLR(1)CLR(1) states with identical LR(0)LR(0) cores to form LALR(1)LALR(1), no new Shift-Reduce conflict can ever arise — only a Reduce-Reduce conflict can be introduced!

Key Formulas Summary
Expressive Power Inclusion
LR(0)⊊SLR(1)⊊LALR(1)⊊CLR(1)=DCFL\text{LR}(0) \subsetneq \text{SLR}(1) \subsetneq \text{LALR}(1) \subsetneq \text{CLR}(1) = \text{DCFL}
Note: LL(1) subsetneq CLR(1), and every LL(1) or LR(k) grammar is UNAMBIGUOUS!
DFA State Count Relationship
∣StatesLR(0)∣=∣StatesSLR(1)∣=∣StatesLALR(1)∣≤∣StatesCLR(1)∣|\text{States}_{\text{LR}(0)}| = |\text{States}_{\text{SLR}(1)}| = |\text{States}_{\text{LALR}(1)}| \le |\text{States}_{\text{CLR}(1)}|
Note: Merging CLR(1) cores to form LALR(1) can NEVER introduce Shift-Reduce conflicts (only Reduce-Reduce)!
#Compiler Design#Parsing#LL1#LR

Practice Questions on This Chapter (6)

Q1GATE CS Compiler PYQMCQMEDIUM
+2M • -0.67

Which of the following statements about bottom-up LR parsers is FALSE?

⚡ Click an option above to verify instantly
Q2GATE CS CompilerMCQEASY
+1M • -0.33

Which pair of bottom-up LR parsers always have the exact same number of states for any given context-free grammar?

⚡ Click an option above to verify instantly
Q3GATE CS CompilerMCQMEDIUM
+2M • -0.67

If a Context-Free Grammar GG is ambiguous, which of the following statements is guaranteed to be TRUE?

⚡ Click an option above to verify instantly
Q4GATE CS CompilerMCQEASY
+2M • -0.67

How many lexical tokens does a C lexical analyzer emit for the statement: printf("i = %d, &i = %x", i, &i);?

⚡ Click an option above to verify instantly
Q5GATE CS 2022MCQMEDIUM
+2M • -0.67

Consider a grammar that is LR(1) (CLR) and therefore unambiguous. When we merge LR(1) states sharing identical LR(0) cores to construct the LALR(1) parser, which new conflict(s) can possibly be introduced?

⚡ Click an option above to verify instantly
Q6GATE CS 2024 Set-1MCQMEDIUM
+2M • -0.67

For the grammar S→aAb∣bBa,  A→ϵ,  B→ϵS \to aAb \mid bBa,\; A \to \epsilon,\; B \to \epsilon, is the grammar LL(1) and/or SLR(1)?

⚡ Click an option above to verify instantly
Previous TopicTOC 2027 Rank-Decider: Myhill-Nerode Minimal DFA Proofs, Inherent Ambiguity & ReductionsNext Topic Syntax-Directed Translation (SDT), Three-Address Code & Register Liveness