CLR Parsing Compiler Design

CLR Parsing

CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more number of states as compared to SLR(1) parsing. In the CLR(1), the reduced node will be placed only in the lookahead symbols.

The step involves in CLR(1) parsing is given below.

  • Write CFG for the given input symbol
  • Check if the grammar is ambiguous or not.
  • Add an augmented grammar.
  • Create a canonical collection of LR(1) items.
  • Draw DFA
  • Construct a CLR(1) parsing table.

LR(1) item is the collection of LR(0) item and lookahead. The lookahead symbol is used to determine the place of the final item. For every augmented grammar, the lookahead will be $.

Example

Grammar

E => BB

B => cB / d

Add augment production and insert '.' symbol at the beginning of every production in G.  Add the lookahead also.

E’ => .E, $

E => .BB,$

B => .cB, c/d

B => .d, c/d

I0 state

Add starting production to the I0 State and compute the closure.

I0 = closure (E’ => .E)

Add all the production beginning with E into I0 State because "." is at the first place of production before the non-terminal. So, the I0 State becomes:

I0 = E’ => .E, $

        E => .BB, $

Add all the production begins with "B" in the modified I0 State because "." is at the first place of production before the non-terminal. So, the I0 State becomes:

E’ => .E, $

E => .BB, $

B => .cB, c/d

B => .d, c/d

I1 = Go to on (I0 E) = closure (E’ => E., $)

I1 = E’ => E. , $

I2 = Go to on (I0 B) = Closure (E => B.B, $)

Add all the production beginning with B into the I2 State because "." is at the first place of production before the non-terminal. So, the I2 State becomes:

E => B.B, $

B => .cB, $

B => .d, $

I3 = Go to on (I0 c) = Closure (B => c.B, c/d)

Add productions beginning with B in I3.

B => c.B, c/d

B => .cB, c/d

B => .d, c/d

Goto on (I3 c) = Closure (B => c. B, c/d)                       (Same as I3)

Goto on (I3 d) = Cllosure (B => d., c/d)                            (Same as I4)

I4 = Goto on (I0 d) = Closure (B => d. , c/d) = B => d. , c/d

I5 = Goto on (I2 B) = Closure (E => BB., $) = E => BB., c/d

I6 = Goto on (I3 c) = Closure (B =>c.B, $)

Add all the production beginning with B into the I6 State because "." is at the first place of production before the non-terminal. So, the I6 state becomes

B => c.B, $

B => .cB, $

B => .d, $

Go to on (I6, c) = Closure (B => c•B, $) = (same as I6)

Go to on (I6, d) = Closure (B => d•, $) = (same as I7)

I7 = Go to on (I2 d) = Closure (B => d. , $)

I8 = Go to on (I3 B) = Closure (B => cB. , c/d)

I9 = Go to on (I6 B) = Closure (B => cB. , $)

Drawing DFA

Production to be numbered as follows:

E =>BB   (1)

B => cB    (2)

B => d       (3)

StatesActionGo To
 c                                d                                $E                                B
I0S3                              S4                                  2
I1  
I2S6                               S7                                   5
I3S3                                 S4                                   8
I4r3                                  r3 
I5                                                                    r1 
I6S6                                 S7                                    9
I7                                                                     r3 
I8r2                                 r2 
I9                                                                      r2 

The shift move and Goto move in CLR(1) parsing are the same as LR(0) and SLR(1). The only difference is the reduced node. 

  • I4 state have contains the final item which drives (B ? d•, c/d), so action {I4, c} = r3, action {I4, d} = r3.
  • I5 state have the final item which drives (E ? BB•, $), so action {I5, $} = r1.
  • I7 state have the final item which drives (B ? d•,$), so action {I7, $} = r3.
  • I8 state have the final item which drives (B ? cB•, a/b), so action {I8, c} = r2, action {I8, d} = r2.
  • I9 state have the final item which drives (B ? cB•, $), so action {I9, $} = r2.

Related Topics

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.

Machine-Independent Optimizations in Compiler Design

Machine-Independent Optimizations The main aim of machine-independent optimization is to improve the generated intermediate code so that compiler can get better target code. Eliminating unwanted code from the object code or replacing...

8 minutes read.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 minutes read.

LALR 1 Parsing | Compiler Design

LALR (1) Parsing The LALR parsing refers to the "lookahead LR" that has many lesser steps than typical parsers based on LR(1) items. For constructing the LALR(1) parsing table, the canonical...

6 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

2 minutes read.

Finite State Machine

Finite State Machine A finite state machine is a simple machine to recognize patterns. It takes a string of symbols as the input and changes its state to another state, but...

3 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Run-Time Storage Management

Run-Time Storage Management Every executing program has its own logical address space. Logical address space is partitioned into: Code: It is responsible for storing the executable target code. Static: It is used to...

3 minutes read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Lexical Analysis in Compiler Design

Lexical Analysis It is the first phase of the compiler. As we know, it is also known as a scanner. The input for lexical analysis is source code. After taking source...

2 minutes read.

Compiler Design Tutorial

Our compiler design tutorial will provide all the information about compiler from basic to advanced level. This compiler tutorial will help the student for their semester as well as for...

3 minutes read.

Derivation and Parse Tree in Compiler Design

Derivation and Parse Tree In this article, we will learn Derivation and Parse Tree. Derivations The parse tree can be constructed by taking a derivational view in which production is treated as rewriting...

4 minutes read.

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

4 minutes read.

Phases of Compiler

Phases of Compiler The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input. Here we will see all...

2 minutes read.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

3 minutes read.

Symbol Table Compiler Design

Symbol Table It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc.  The...

3 minutes read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

Target Machine

Target Machine A target machine is a byte-addressable machine. This machine has n general-purpose registers, R0, R1,.....Rn-1. A Simple Target Machine Model has three-address instruction. A full-edged assembly language would have...

3 minutes read.