Check Whether the Given Grammar Is CLR(1)

Check Whether the Given Grammar Is CLR(1)

Verified Sources
Nov 4, 2026

In compiler courses, “CLR” almost always refers to CLR(1) grammars (a.k.a. LR(1) grammars). A grammar is CLR(1) iff it can be parsed by a deterministic LR parser built from LR(1) items without conflicts. Concretely: construct the LR(1) canonical collection of items, build ACTION/GOTO, and verify there are no shift/reduce or reduce/reduce conflicts.

This section checks the given grammar:

  • S→Aa∣bBa∣Ba∣bAcS \rightarrow Aa \mid bBa \mid Ba \mid bAc
  • A→cA \rightarrow c
  • B→dB \rightarrow d

We will determine whether it is CLR(1) by examining the LR(1) parsing behavior around the points where the parser must choose between actions.

Key terms you’ll use:

  • LR parsing
  • LR(1) item
  • ACTION/GOTO table
  • Shift/reduce conflict
  • Canonical collection of LR(1) items

LR(1) Parsing and Conflicts (overview)

Step 1: Augment the grammar and identify productions

To build LR(1), we augment:

  • S′→SS' \rightarrow S

So the productions are:

  1. S′→SS' \rightarrow S
  2. S→AaS \rightarrow Aa
  3. S→bBaS \rightarrow bBa
  4. S→BaS \rightarrow Ba
  5. S→bAcS \rightarrow bAc
  6. A→cA \rightarrow c
  7. B→dB \rightarrow d

Terminals: {a,b,c,d}\{a,b,c,d\} (and end-marker $$$).

We will look for a state where the parser’s choice depends on lookahead in a conflicting way.

Determine if the grammar is CLR(1) by locating any LR(1) ACTION conflicts

  1. 1
    Step 1

    Start with [S' \rightarrow •S, \]$ and apply closure to generate initial items; closure introduces items for nonterminals immediately after a dot, with lookaheads computed using FIRST sets.

  2. 2
    Step 2

    From the item [S' \rightarrow •S, \],closureincludesitemsforproductionsofS.SinceSisfollowedbyend−markerin, closure includes items for productions of S. Since S is followed by end-marker in S' \rightarrow S,lookaheadforthoseitemsisinitially, lookahead for those items is initially $.

  3. 3
    Step 3

    The grammar has both S→BaS \rightarrow Ba and S→bBaS \rightarrow bBa. The moment the parser has recognized a prefix like bdb d (i.e., it has seen bb then BB via B→dB \rightarrow d), the next input symbol may cause either a reduction of S→BaS \rightarrow Ba or shifting/continuing for S→bBaS \rightarrow bBa—this is where LR conflict checks matter.

  4. 4
    Step 4

    In an LR(1) construction, conflicts occur when, for the same state II and terminal tt, ACTION has multiple entries. If no such conflicts arise (and there are no reduce/reduce ambiguities), the grammar is CLR(1).

  5. 5
    Step 5

    Because this grammar separates the contexts that lead to each alternative using different fixed following symbols (aa vs cc), the LR(1) lookahead distinguishes reductions; thus no shift/reduce conflict appears, so the grammar is CLR(1).

Step 2: Compute the LR(1) lookahead distinctions for the key ambiguity (after reading bdb d)

The potentially confusing part of the grammar is:

  • S→BaS \rightarrow Ba
  • S→bBaS \rightarrow bBa

Both contain the substring BaBa after possibly consuming an extra leading bb.

Now examine what strings these generate:

  • From S→BaS \rightarrow Ba: since B→dB \rightarrow d, we get S⇒daS \Rightarrow da
  • From S→bBaS \rightarrow bBa: we get S⇒bdaS \Rightarrow b d a

So:

  • The parser should reduce by S→BaS \rightarrow Ba only when the derivation corresponds to having already completed a BB in the correct context.
  • If the input contains a leading bb, then the correct derivation must match S→bBaS \rightarrow bBa, not S→BaS \rightarrow Ba (because S→BaS \rightarrow Ba cannot produce a leading bb).

In LR(1), the lookahead helps the parser decide at the exact reduction time.

Concretely:

  • B→dB \rightarrow d is a complete production when the dot reaches the end: items like [B→d•,a][B \rightarrow d •, a] tell the parser to reduce B→dB \rightarrow d with lookahead aa if aa is the expected following terminal.
  • In S→BaS \rightarrow Ba, aa is required immediately after BB.
  • In S→bBaS \rightarrow bBa, aa is also required immediately after BB (still BaBa).

Thus, after reading bdb d, the next symbol must be aa for those productions to be valid. That means the parser’s action at the state where B→d•B \rightarrow d • items exist will consistently be “reduce B→dB \rightarrow d”, and then “reduce S→bBaS \rightarrow bBa” or “reduce S→BaS \rightarrow Ba” depending on how much of the prefix was consumed—without needing to guess between two different continuations on the same lookahead.

Step 3: Check the other alternative containing AA

The other “almost-overlapping” structure is:

  • S→AaS \rightarrow Aa where A→cA \rightarrow c gives S⇒caS \Rightarrow c a
  • S→bAcS \rightarrow bAc where A→cA \rightarrow c gives S⇒bccS \Rightarrow b c c

Again, the symbols immediately following AA are different:

  • In AaAa, the symbol after AA must be aa.
  • In bAcbAc, the symbol after AA must be cc (and there is also the leading bb already).

So LR(1) lookahead will disambiguate reductions involving A→cA \rightarrow c in the same way.

How to reason about LR(1) conflicts quickly

When a grammar seems ambiguous, look for a single LR(1) state where two different productions could be reduced on the same lookahead terminal. If the grammar forces distinct next terminals (like requiring aa vs cc after a common nonterminal), LR(1) lookahead typically prevents conflicts.

Terminology note: CLR(1) vs general LR

Some courses use “CLR” informally for LR-family parsing. In formal CS theory, CLR(1) corresponds to LR(1). If your course instead means CLR(0) or SLR(1), the conclusion can differ.

CLR(1) Decision Roadmap (applied to this grammar)

Add $S' \rightarrow S$

1. Augment

Required to start LR(1) item construction and define accept with \."

Generate LR(1) item sets

2. Closure/GOTO

Track lookaheads using FIRST sets in closure."

Derive parser actions

3. Build ACTION/GOTO

Shifts from dots before terminals; reductions from completed items."

Look for ACTION conflicts

4. Conflict check

No shift/reduce or reduce/reduce in any state ⇒ CLR(1)."

Declare CLR(1) or not

5. Conclude

Here, forced follow-symbols make LR(1) lookahead distinguish choices, so no conflicts arise."

Common edge questions

Knowledge Check

Question 1 of 4
Q1Single choice

In LR(1)/CLR(1) parsing, conflicts refer to which condition?