SLR Parser (with Examples) (original) (raw)

Last Updated : 23 Jul, 2025

**LR parsers is an efficient bottom-up syntax analysis technique that can be used to parse large classes of context-free grammar is called LR(k) parsing.
L stands for left-to-right scanning
R stands for rightmost derivation in reverse
k is several input symbols. when k is omitted k is assumed to be 1.

**Advantages of LR parsing

**Types of LR Parsing Methods

**SLR Parser

**Steps for constructing the SLR parsing table

  1. Writing augmented grammar
  2. LR(0) collection of items to be found
  3. Find FOLLOW of LHS of production
  4. Defining 2 functions:goto[list of terminals] and action[list of non-terminals] in the parsing table

**EXAMPLE - Construct LR parsing table for the given context-free grammar

S-->AA
A-->aA|b

**Solution:

**STEP1: Find augmented grammar
The augmented grammar of the given grammar is:-

S'-->.S [0th production]
S-->.AA [1st production]
A-->.aA [2nd production]
A-->.b [3rd production]

**STEP2: Find LR(0) collection of items
Below is the figure showing the LR(0) collection of items. We will understand everything one by one.

The terminals of this grammar are {a,b}.
The non-terminals of this grammar are {S,A}

**RULE -
If any non-terminal has ' . ' preceding it, we have to write all its production and add ' . ' preceding each of its production.

**RULE -
from each state to the next state, the ' . ' shifts to one place to the right.

**STEP3: Find FOLLOW of LHS of production

FOLLOW(S)=$
FOLLOW(A)=a,b,$

To find FOLLOW of non-terminals, please read follow set in syntax analysis.

**STEP 4: Defining 2 functions:goto[list of non-terminals] and action[list of terminals] in the parsing table. Below is the SLR parsing table.

**APPLICATIONS GALORE: