Parsing / Context-Free Grammar/Leftmost and Rightmost Derivations
A derivation of a string for a grammar is a sequence of production rule applications.
The process of getting the string through the derivation is the parsing process.
For example, with the following grammar:
S → S + S (1)
S → 1 (2)
S → a (3)
The string
Leftmost Derivation
In the leftmost derivation, the nonterminals are applied from left to right. Initially, we can apply rule 1 on the initial
Next, the leftmost
The next nonterminal is the second
Now, with the remaining characters from the input, we can apply rule 2 on the first
Finally, the last
The above derivation can be represented as the following parse tree:
Rightmost Derivation
In the rightmost derivation, the nonterminals are applied from right to left.
Initially, we can apply rule 1 on the initial
This time, we apply rule 1 on the rightmost
We found that rule 3 can be applied to the rightmost
Next, we can apply rule 2:
And finally, rule 2 can be applied again:
This derivation can be represented as the following parse tree: