COMPILER DESIGN/Student Engineering Notebook
Page 06·notes
BOTTOM-UP SYNTAX ANALYSIS & LR PARSERS—~32 MIN READ

SLR(1) Parsing Tables & Conflict Resolution

“FOLLOW-set guided reductions, Shift-Reduce (SR) and Reduce-Reduce (RR) conflict diagnosis.”

1.The Power and Conflict Taxonomy of SLR(1)

The Simple LR (SLR(1)) parser significantly strengthens LR(0) by using 1 token of lookahead from the context-free grammar to guard reduction actions.

In an LR(0) parser, whenever an item is completed (A o alpha cdot), the parser unconditionally reduces on any lookahead token. This generates false conflicts whenever a shift transition is also available.

SLR(1) Resolution Principle: When in state I_k with completed item A o alpha cdot, reduction is logically permissible only if the incoming lookahead token could legitimately follow A in some valid derivation. Hence, the parser writes r_i into column a if and only if a in FOLLOW(A).

Conflict Taxonomy:

1. Shift-Reduce (SR) Conflict: The parser cannot decide whether to shift the current input token onto the stack or reduce the current stack contents using a production.

2. Reduce-Reduce (RR) Conflict: The parser identifies two different productions whose handles are both complete on top of the stack and cannot decide which production to reduce.

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

2. Worked Analysis: Identifying SLR(1) Shift-Reduce Conflict

⚠️ Pitfall Warning:

Check if the shift terminal symbol appears in the FOLLOW set of the non-terminal being reduced.

Consider the classic assignment grammar:

S -> L = R | R

L -> * R | id

R -> L

Show that this grammar is NOT SLR(1) by finding the exact conflict in its canonical LR(0) items.

Step 1: Compute FOLLOW sets:

- FOLLOW(S) = {$}

- In S -> L = R: symbol '=' follows L. Also S -> R and R -> L, so FOLLOW(L) contains FOLLOW(R).

- FOLLOW(R) contains FOLLOW(S) = {} and from S -> L = R, FOLLOW(R) contains FOLLOW(S) = {}.

- Since R -> L, FOLLOW(L) contains FOLLOW(R) = {$, =}.

- Therefore, FOLLOW(R) = {, =} and FOLLOW(L) = {, =}.

Step 2: Generate State I0 and Transitions:

- I0 = CLOSURE({S' -> . S}) contains:

S' -> . S

S -> . L = R

S -> . R

L -> . * R

L -> . id

R -> . L

Step 3: Compute GOTO(I0, L) = I2:

- Advance dot past L in items containing '. L':

S -> L . = R

R -> L .

Step 4: Check for conflicts in State I2:

- Item 1: S -> L . = R (wants to SHIFT on symbol '=')

- Item 2: R -> L . (completed item, wants to REDUCE by R -> L on all symbols in FOLLOW(R))

- Does symbol '=' belong to FOLLOW(R)?

- Yes! We determined FOLLOW(R) = {$, =}.

- Therefore, on input symbol '=', State I2 has BOTH:

- Action[I2, =] = Shift

- Action[I2, =] = Reduce by R -> L

Conclusion: State I2 contains a Shift-Reduce conflict on '='. The grammar is NOT SLR(1).

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: SLR uses whole FOLLOW sets, which are too coarse here. LALR and CLR track which tokens can really follow in each state.

Worked example: why SLR(1) fails

Problem Statement:
S → L = R | R, L → * R | id, R → L. Why does an SLR(1) table get a conflict?
Step 1: The problem state

After reading an L, a state holds S → L • = R and R → L •.

Step 2: SLR's rule

Reduce R → L on every token in FOLLOW(R). Since S → L = R and R → L, = is in FOLLOW(R).

Step 3: The conflict

On =, the parser can shift (for S → L • = R) or reduce (R → L). It's a shift-reduce conflict.

Step 4: What LALR does

With exact lookaheads, R → L • in this state can only be followed by $, so there's no conflict.

Final Answer:Not SLR(1), but LALR(1)
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · FOUNDATION

Which LR method recognises the most grammars?

Problem 2 · GATE-LEVEL

Can an LR(0) parser handle the grammar S → a | a b?

Problem 3 · TRICKY

Every LL(1) grammar is also:

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
Compilers: Principles, Techniques, and Tools ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Gate Smashers · Compiler Design↗Instructor: Varun Singla · Covers: Lexical analysis, parsing (LL/LR), SDT, code generation

GATE-focused with worked FIRST/FOLLOW and LR parsing table numericals.

Open YouTube↗
Knowledge Gate · Compiler Design for GATE↗Instructor: Sanchit Jain · Covers: Parsing conflicts, operator precedence, syntax-directed translation

Exam-oriented explanations of ambiguous grammars and parser conflicts.

Open YouTube↗
Stanford · Compilers (Coursera)↗Instructor: Prof. Alex Aiken · Covers: Lexing, parsing, semantic analysis, optimization, code generation

University-depth treatment of the full compiler pipeline — build a real compiler.

Open YouTube↗