# Derive the string тАЬabbтАЭ for leftmost and rightmost derivation

## Problem

Let’s consider a grammar to derive “abb” string using LMD and RMD

S->AB/ ε

A-> aB

B-> Sb

## Solution

We have to use context free grammar.

Derivation is a sequence of production rules, which is used to get input strings.

During parsing, we have to take two decisions, which are as follows −

• We have to decide which non-terminal has to be replaced.
• We have to decide which non-terminal has to be replaced by using which production rule.

There are two options to decide which non-terminal has to be replaced with production rule −

• Leftmost derivation
• Rightmost derivation

## Leftmost derivation

The input is scanned and replaced with production rules from left to right.

The given production rules are:
S->AB/ ε
A-> aB
B-> Sb
Let’s derive the string “abb” using LMD
S->AB
->aBB {A->aB}
->aSbB {B->Sb}
->abB {S-> ε}
->abSb {B->Sb}
->abb {S-> ε}
Which is our final string

## Rightmost derivation

The input is scanned and replaced with a production rule from right to left.

The given production rules are:
S->AB/ ε
A-> aB
B-> Sb
Let’s derive the string “abb” using RMD
S -> AB
->ASb {B->Sb}
->Ab {S-> ε}
->aBb {A->aB}
->aSbb {B->Sb}
->abb {S-> ε}
Reached our final string