# Explain the concept of derivation in TOC

Data Structure AlgorithmsComputer ScienceComputers

Derivation is a sequence of production rules. It is used to get input strings. During parsing, we have to take two decisions, which are as follows

• We have to decide the non-terminal which is to be replaced.
• We have to decide the production rule by which the non-terminal will be replaced.

Two options to decide which non-terminal has to be replaced with the production rule are as follows −

• Left most derivation
• Right most derivation.

Let us understand these two options in detail.

## Left Most Derivation

In the leftmost derivation, the input is scanned and then replaced with the production rule from left side to right. So, we have to read that input string from left to right.

## Example

Production rules:

E=E+E rule1
E=E-E rule2
E=a|b rule3

Let the input be a-b+a

Now, when we perform the Left Most Derivation, the result will be as follows −

E=E+E
E=E-E+E from rule2
E=a-E+E from rule3
E=a-b+E from rule3
E=a-b+a from rule3
Finally, the given string is parsed

## Right Most Derivation

In Right most derivation, the input is scanned and replaced with the production rule right to left. So, we have to read the input string from right to left.

## Example

Production rule:

E=E+E rule1
E=E-E rule2
E=a|b rule3

Let the input be a-b+a

Now, when we perform the Right Most Derivation, we get the following result −

E=E-E
E=E-E+E from rule1
E=E-E+a from rule3
E=E-b+a from rule3
E=a-b+a from rule3