LR Parser Compiler Design

LR Parser

LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands for constructing a right-most derivation in reverse.

LR parser is divided into four categories:

LR (1) Parsing

LR (1) parsing involves various steps as follows:

  • Write a CFG for a given input string.
  • Check if the grammar is ambiguous or not.
  • Add augmented production in the grammar.
  • Create a canonical collection of LR (0) items.
  • Draw DFD.
  •  LR (1) parsing table construction.

Augmented Grammar

If X is a grammar with start symbol A, then X’ will be the augmented grammar for X, which is the grammar with new start symbol X' and production X’ => X. Augmented production helps the parser to identify when the parsing action should be stopped and announce the acceptance of input.

Example:

S => AA

A => aA | b

The augmented grammar for the above grammar will be

S’ => S

A => AA

A => aA | b

Canonical Collection of LR(0) Item

An LR(0) item of a grammar G is a production of G with a dot at some position on the right-side of the production. Thus production S => XYZ yields four items:

S => .XYZ

S => X.YZ

S => XY.Z

S => XYZ.

The collection of LR(0) items is known as canonical LR(0) collection, which is helpful in constructing deterministic finite automata to make parsing decisions. In the LR (0), we need to put the reduce node in the entire row.

Example

Grammar

S => AA

A => aA | b

Add an augmented grammar in a given grammar.

S’ =>S

S => AA

A => aA | b

Take the LR(0) item for all the production. The first production is the starting production.

S’ => .S

S => .AA

A => .aA

A => .b

I0 State

Add starting production to the I0 State and Compute the Closure.

S’ => .S

Since "." is on the left side of the symbol. It means we have not seen anything on the right-hand side. So all productions starting with S will be added into I0 State because "." is followed by the non-terminal. So, the modified I0 State will be:

S’ => .S

S => .AA

Here we can see that “.” is on the left of the variable A. So all productions starting with A will be added in I0 State because "•" is followed by the non-terminal. So, the modified I0 State becomes.

        S’ => .S

        S => .AA

        A => .aA

        A => .b

I1 = Here we have applied Goto on (I0 S) = S’ => S.

Here, "." is on the right side of the symbol, so the production is reduced so close the state.

I1 becomes S’ => S.

I2 = we have applied Goto on (I0 A) = (S => A.A)

We apply closure, and we will get all the A production with "." in the beginning. So I2 becomes

         S => A.A

         A => .aA

         A => .b

Goto on (I2,a) = Closure (A ? a•A) = (same as I3)

Goto on  (I2, b) = Closure (A ? b•) = (same as I4)

I3=Goto on (I0,a) = Closure (A ? a•A)

Add productions starting with A in I3.

A => a.A

A => .aA

A => .b

Goto on (I3, a) = Closure (A ? a•A) = (same as I3)

Goto on (I3, b) = Closure (A ? b•) = (same as I4)

I4 = Go to on (I0, b) = closure (A ? b•) = A ? b•

I5 = Go to on(I2, A) = Closure (S ? AA•) = SA ? A•

I6 = Go to on (I3, A) = Closure (A ? aA•) = A ? Aa•

DFA for a given production

LR(0) Table

  • Shift Move: When a state is going to some other state on a terminal, then it is a shift move.
  • Goto Move: When a state is going to some other state on a variable, then it is a Goto moves.
  • In LR(0) parsing table, whenever any state having a final item in the particular row, we will write reduce node completely.
StatesActionGo to
 a                 b                  $A                               S
I0S3               S4                2                                 1
I1                                  accept 
I2S3              S45
I3S3               S46
I4r3                r3                r3 
I5r1                 r1               r1 
I6r2                 r2                r2 

Explanation

  • Applying Goto on I0 using S is going to I1, so we will write it as 1.
  • Applying Goto on I0 using A is going to I2, so we will write it as 2.
  • Applying Goto on I2 using A is going to I5, so we will write it as 5.
  • Applying Goto on I3 using A is going to I6, so we will write it as 6.
  • Applying Goto on I0, I2, and I3 using a are going to I3, so we will write it as S3, which is shift 3.
  • Applying Goto on I0, I2, and I3 using b are going to I4, so we will write it as S4, which is shift 4.
  • I4, I5, and I6 state contain the final item because the ‘•’ is in the right-most end.

Productions are numbered as follows:

S => AA       (1)

A => aA        (2)

A => b           (3)

  • I1 state has the final item which drives(S` ? S•) because "." is on the right side of the variable. So action {I1, $} = Accept.
  • I4 state has the final item which drives A ? b•, and this production is corresponding to the production number 3, so we will write the entire row as r3.
  • I5 state has the final item which drives S ? AA•, and this production is the same as the production number 1, so we have written the entire row as r1.
  • I6 state has the final item, which drives A ? aA•, and this is the same as the production number 2, so we have written the entire row as r2.

Related Topics

LR Parser Compiler Design

LR Parser LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands...

6 minutes read.

Shift Reduce Parsing in Compiler Design

Like the bottom-up parsing, the shift reduce parser also builds the parse tree from the leaves (bottom) to the root (up). The LR parser is a more versatile variation of...

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.

Three-Address Code

Three-Address Code If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted....

2 minutes read.

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.

Translation of Array References in Compiler Design

Translation of Array References We can access the elements of an array stored in consecutive blocks very easily and quickly. In a programming language like C and Java, the size of...

2 minutes read.

Syntax Analysis Compiler Design

Syntax Analysis This article will describe the parsing method used in the compiler. The grammatical rule of programming language can be constructed with the help of context-free grammars or BNF (Backus–Naur...

3 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.

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.

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

6 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.

SLR 1 Parsing Compiler Design

SLR(1) Parsing It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1)...

5 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.

Basic Blocks and Flow Graphs in Compiler Design

Basic Blocks and Flow Graphs In this section, we are going to learn how to work with basic block and flow graphs in compiler design. Basic Block The basic block is a set...

3 minutes read.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 minutes read.

Optimization of Basic Blocks in Compiler Design

Optimization of Basic Blocks We can apply the optimization process on a basic block. While optimization, there is no need to change the set of expressions computed by the block. The basic...

3 minutes read.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 minutes read.

LEX

LEX Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows...

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.

Switch Case Statement Compiler Design

Case Statement The “case” or “switch” statement is available in various languages. The following is the syntax for the case statement: switch (E)  {             case V1: S1             case V2: S2 ...

1 minute read.