1. Gyanastra
  2. Compiler Design
  3. LR(0) and SLR(1) Parsing
  4. LR Parsing Algorithm and Role of the Parsing Table
Free lesson, no account needed 4 min read 3 practice questions after it
Asked in past papers1 time
  • UGC NET2019

Compiler Design · LR(0) and SLR(1) Parsing

LR Parsing Algorithm and Role of the Parsing Table

Overview

All LR-family parsers — LR(0), SLR(1), CLR(1) and LALR(1) — share one parsing algorithm and one overall structure; only the parsing table differs between them.
Understanding this split is what lets a single algorithm drive every LR parser, with table construction (and the choice of item type) as the only variable.

Key concepts

  • Common parsing algorithm: The parsing algorithm is identical for LR(0), SLR(1), CLR(1) and LALR(1). It does not change from one parser to the next.
  • Parsing table: The only component that varies among the LR parsers; everything other than the parsing table is identical.
  • Item type determines the table: The construction of the parsing table depends on which kind of items are used.
    • LR(0) and SLR(1) tables are constructed using the canonical collection of LR(0) items.
    • CLR(1) and LALR(1) tables are constructed using the canonical collection of LR(1) items.
  • Item: The basic unit of table construction; one must know what an item is, and the difference between an LR(0) item and an LR(1) item, before constructing any table.
  • Closure and goto: The tools used to build an LR parsing table (of any kind), just as FIRST and FOLLOW were used to build the LL(1) parsing table.

How it works

  1. Fix the parsing algorithm — it is common to all LR parsers and does not change from LR(0) to SLR(1) to CLR(1) to LALR(1).
  2. Construct the parsing table — the only part that varies between the parsers.
  3. Choose the item set used to build the table:
    • LR(0) and SLR(1): canonical collection of LR(0) items.
    • CLR(1) and LALR(1): canonical collection of LR(1) items.
  4. Know what an item is, and what an LR(0) item and an LR(1) item are, before constructing any table.
  5. Build the simplest parser first — the LR(0) parser — using LR(0) items, then apply the parsing algorithm and its implementation to that example; the algorithm stays the same for all parsers, only the parsing table varies.
  6. Build the table using closure and goto, mirroring how the LL(1) table was built using FIRST and FOLLOW.
Figure. The algorithm is fixed for all LR parsers; only the table changes. LR 0 and SLR 1 use LR 0 items, CLR 1 and LALR 1 use LR 1 items, built with closure and goto.

Comparison

ParserItem set used to construct the parsing table
LR(0)Canonical collection of LR(0) items
SLR(1)Canonical collection of LR(0) items
CLR(1)Canonical collection of LR(1) items
LALR(1)Canonical collection of LR(1) items
AspectSame across all LR parsersDiffers across LR parsers
Parsing algorithmYesNo
Parsing table constructionNoYes (LR(0) items vs LR(1) items)

Revision summary

  • The parsing algorithm is identical for LR(0), SLR(1), CLR(1) and LALR(1); only the parsing table construction changes.
  • LR(0) and SLR(1) tables come from the canonical collection of LR(0) items.
  • CLR(1) and LALR(1) tables come from the canonical collection of LR(1) items.
  • Know what an item is, and the difference between an LR(0) item and an LR(1) item, before constructing tables.
  • LR table construction uses closure and goto, just as LL(1) table construction used FIRST and FOLLOW.
  • Trap: do not attribute the difference between LR parsers to the algorithm — the algorithm never changes; the table does.

Previous-year exam insights

  • A shift-reduce (LR) parser consists of three components: an input buffer, a stack, and a parsing table (plus a driver program that reads the table); the parse table is an essential component, not optional.
    Asked inUGC NET 2019
  • The LR parsing table has two parts: ACTION (indexed by state and terminal, giving shift/reduce/accept/error) and GOTO (indexed by state and nonterminal, giving the next state).
  • A configuration of an LR parser is (stack contents, remaining input); the parser's behaviour is determined solely by the top stack state and the current input symbol, i.e. by the table entry ACTION[top, a].
  • The four possible actions in an LR parser are shift (push the input symbol and go to the new state), reduce (pop the handle's states and push GOTO), accept (parsing successful), and error.
  • The LR parser is a bottom-up (shift-reduce) parser that reads input left to right and produces a rightmost derivation in reverse; it detects a syntax error as soon as the erroneous symbol is examined (viable-prefix property).
  • The driver program of the LR parser is the same for all LR parsers; only the table differs, so a single generic algorithm plus a different table yields LR(0), SLR(1), CLR(1) or LALR(1) behaviour.
Pages 1–2 of 2