# LL(1) & LR(0), SLR(1), LALR(1), CLR(1) Parsing Power & Conflicts
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Compiler Design
Canonical URL: https://gateandnet.com/exams/gate-cs/compiler-design/ll1-and-lr-parsers-hierarchy

> 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)$).
2. For every non-terminal $A \to \alpha \mid \beta$:
   - $\operatorname{FIRST}(\alpha) \cap \operatorname{FIRST}(\beta) = \emptyset$
   - If $\epsilon \in \operatorname{FIRST}(\alpha)$, then $\operatorname{FIRST}(\beta) \cap \operatorname{FOLLOW}(A) = \emptyset$.

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


## Solved Practice Questions & PYQs

### [GATE CS Compiler PYQ | MCQ]
Which of the following statements about bottom-up LR parsers is **FALSE**?
**Correct Answer:** C
**Solution:** **C is FALSE!** State merging from CLR(1) to LALR(1) can **never** create a Shift-Reduce conflict; it can only create a Reduce-Reduce conflict.

### [GATE CS Compiler | MCQ]
Which pair of bottom-up LR parsers **always have the exact same number of states** for any given context-free grammar?
**Correct Answer:** B
**Solution:** **LR(0), SLR(1), and LALR(1)** all have the **exact same number of states** ($n_1$), because SLR(1) uses LR(0) items and LALR(1) merges CLR(1) states sharing identical LR(0) cores! CLR(1) typically has significantly more states ($n_2 \ge n_1$).

### [GATE CS Compiler | MCQ]
If a Context-Free Grammar $G$ is **ambiguous**, which of the following statements is **guaranteed** to be TRUE?
**Correct Answer:** B
**Solution:** Every LL($k$) and LR($k$) grammar is **strictly unambiguous**! Therefore, an ambiguous grammar can **never** belong to LL(1), SLR(1), LALR(1), or CLR(1) unless external precedence/associativity rules resolve conflicts.

### [GATE CS Compiler | MCQ]
How many lexical **tokens** does a C lexical analyzer emit for the statement: `printf("i = %d, &i = %x", i, &i);`?
**Correct Answer:** B
**Solution:** Count each lexeme carefully (the entire quoted string literal is **1 single token**!): `printf` (1), `(` (2), `"i = %d, &i = %x"` (3), `,` (4), `i` (5), `,` (6), `&` (7), `i` (8), `)` (9), `;` (10) $\implies$ **10 tokens**.
