HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

How is CLR(1) grammar more powerful than LALR(1) grammar

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
powerfulthanmoreclrlalrgrammarhow

Problem

I am unable to understand how Canonical LR(1) grammar is more powerful than LookAhead LR(1). Both have lookahead symbols in their items and works almost similarly, so how can CLR(1) derive a larger set of languages than LALR(1).

Solution

LALR(1) is the grammar obtained by merging states of CLR(1). The states with the exact same production rules i.e. the exact same core, but different lookahead are combined together.

Specifically, consider
$$I_i: A\rightarrow \alpha \bullet \beta, \; a \;\; \mbox{and}\;\; I_j: A\rightarrow \alpha \bullet \beta, \; b$$

The merged state looks like
$$I_{ij}: A\rightarrow \alpha \bullet \beta, \; a/b$$

This merging introduces reduce-reduce conflict, even if the original LR(1) grammar was free of such conflicts. To show this, we will consider an example.

Let there be two states $I_i$ and $I_j$ in the LR(1) grammar.

$I_i:$

$A\rightarrow \alpha \bullet, \; p$

$B\rightarrow \alpha \bullet, \; q$

and $I_j:$

$A\rightarrow \alpha \bullet, \; r$

$B\rightarrow \alpha \bullet, \; s$

The merged state $I_{ij}$ is

$A\rightarrow \alpha \bullet, \; p/r$

$B\rightarrow \alpha \bullet, \; q/s$

Now, for the LALR(1) grammar to have a reduce-reduce conflict either

  • $p=s$ which is possible without a reduce-reduce conflict in $I_i$ and $I_j$ OR



  • $q=r$ which is also possible without a reduce-reduce conflict in $I_i$ and $I_j$



Thus, it proves why LALR(1) is less powerful than CLR(1).

Context

StackExchange Computer Science Q#140528, answer score: 5

Revisions (0)

No revisions yet.