What is types of LR Parser in compiler design?

Compiler DesignProgramming LanguagesComputer Programming

There are three types of LR Parsers which are as follows −

  • Simple LR Parser (SLR) − SLR represents "Simple LR Parser". It is very easy and costeffective to execute. But it fails to make a parsing table for some class of grammars, i.e., why CLR and LALR are used which implements mainly all class or type of grammars. It constructs parsing tables which helps to perform parsing of input strings. SLR Parsing can be done if context-free Grammar will be given. In LR (0), 0 means there is no Look Ahead symbol.

The SLR parsing action and goto function from the deterministic finite automata that identifies viable prefixes. It will not make specifically represented parsing action tables for all grammars but does achieve on some grammars for programming languages. Given a grammar G. We augment G to produce G’, and from G’ it can construct C, the canonical collection of a set of items for G’.

  • Canonical Look Ahead LR Parser (CLR) − CLR defines canonical lookahead. CLR parsing uses the canonical collection of LR (1) items to construct the CLR (1) parsing table. CLR (1) parsing table makes more number of states as compared to the SLR (1) parsing. In the CLR (1), it can locate the reduce node only in the lookahead symbols.

Construction of LR (1) collection of items for Grammar

It requires three things

  • Augmented Grammar
  • Closure Function
  • goto Function

Augmented Grammar − It is a new Grammar G′ which contains a new production S′ → S with all other productions of given grammar G.

Closure

  • procedure closure (I)
  • begin Repeat
  • for each item A→α ∙B β, a in I,
    • each production B→γ and
    • each terminal b∈FIRST (β a)
  • If B→ ∙ γ is not in I
  • Add B→ ∙γ,b to I
  • Until no more elements can be joined to I;
  • end

𝐠𝐨𝐭𝐨(𝐈, 𝐗)− If there is a production 𝐀 → 𝛂 ∙ 𝐗 𝛃, 𝐚 𝐢𝐧 𝐈 then 𝐠𝐨𝐭𝐨(𝐈, 𝐗) is the closure of the set of items of 𝐀 → 𝛂 𝐗 ∙ 𝛃, 𝐚.

  • Look Ahead LR Parser (LALR) − LALR Parser is Look Ahead LR Parser. It is intermediate in power between SLR and CLR parser. It is the compaction of CLR Parser, and hence tables obtained in this will be smaller than CLR Parsing Table.

Here, first of all, we will construct LR (1) items. Next, we will look for the items having the same first component, and they are merged to form a single set of items. It means the states have the same first component, but the different second component can be integrated into a single state or item.

raja
Published on 03-Nov-2021 10:51:35

Advertisements