What is the difference between SLR, CLR, and LALR Parser in compiler design?


SLR Parser

SLR represents "Simple LR Parser". It is very easy and cost-effective to execute. The SLR parsing action and goto function from the deterministic finite automata that recognizes viable prefixes. It will not make specificially defined parsing action tables for all grammars but does succeed on several grammars for programming languages. Given a grammar G. It augment G to make G’, and from G’ it can construct C, the canonical collection of a set of items for G’. It can construct ACTION the parsing action function, and GOTO, the goto function, from C using the following simple LR Parsing table construction technique. It needed us to understand FOLLOW (A) for each non-terminal A of a grammar.

CLR Parser

CLR refers to canonical lookahead. CLR parsing uses the canonical collection of LR (1) items to construct the CLR (1) parsing table. CLR (1) parsing table make 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.

LALR Parser

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.

For constructing the LALR (1) parsing table, the canonical collection of LR (1) items is used. In the LALR (1) parsing, the LR (1) items with the equal productions but have several look ahead are grouped to form an individual set of items. It is frequently the similar as CLR (1) parsing except for the one difference that is the parsing table.

The overall structure of all these LR Parsers is the same. There are some common factors such as size, class of context-free grammar, which they support, and cost in terms of time and space in which they differ.

Let us see the comparison between SLR, CLR, and LALR Parser.

SLR ParserLALR ParserCLR Parser
It is very easy and cheap to implement.It is also easy and cheap to implement.It is expensive and difficult to implement.
SLR Parser is the smallest in size.LALR and SLR have the same size. As they have less number of states.CLR Parser is the largest. As the number of states is very large.
Error detection is not immediate in SLR.Error detection is not immediate in LALR.Error detection can be done immediately in CLR Parser.
SLR fails to produce a parsing table for a certain class of grammars.It is intermediate in power between SLR and CLR i.e., SLR ≤ LALR ≤ CLR.It is very powerful and works on a large class of grammar.
It requires less time and space complexity.It requires more time and space complexity.It also requires more time and space complexity.

Updated on: 02-Nov-2021

37K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements