Hello! Welcome back to our study of the Theory of Computation.
In our last lesson, we formally defined a Context-Free Grammar (CFG) as a 4-tuple . We established that these grammars, through their production rules, provide a way to generate languages with recursive, nested structures that are beyond the capabilities of finite automata.
Now that we know what a CFG is, we will learn how to use it. This lesson focuses on the core process of applying production rules to generate strings. This process is called derivation. Your experience with compilers and interpreters will be a useful mental model here; parsing source code is fundamentally an attempt to find a valid derivation for it according to the programming language's grammar.
By the end of this lesson, you will be able to construct the two most important types of derivations for any given string and grammar: leftmost and rightmost derivations.
1. From Rules to Strings: The Derivation Process
A derivation is a sequence of steps that starts with the start symbol and ends with a string of terminals. In each step, we replace one non-terminal with the body of one of its production rules.
Let's formalize this. Given a CFG, the relation (read as "yields in one step") means that we can get from one string to another by applying a single production rule. For a string , where is a non-terminal and and are any strings of grammar symbols (terminals or non-terminals), if there's a production , we can write:
This means we've replaced with . The string is what we call a sentential form—an intermediate string containing terminals and/or non-terminals derived from the start symbol.
When we can get from a string to a string in one or more steps, we write . If it takes zero or more steps (i.e., can be the same as ), we use the reflexive, transitive closure, written as .
A string consisting only of terminals () is in the language of the grammar , denoted , if it can be derived from the start symbol . Formally: if and only if .
To see a more rigorous treatment of these definitions, you can refer to the following text.
Chapter 7 Context-Free Languages and PDA's
This document, from a course at the University of Pennsylvania, provides the formal mathematical definitions for the one-step derivation relation and its closures.
Read Section 7.2, "Derivations and Context-Free Languages," from the start up to (but not including) the discussion of grammar G1 on page 299. Focus on understanding Definition 7.2, which formalizes the derivation relation \Rightarrow_G.
2. Resolving Ambiguity: Leftmost and Rightmost Derivations
Consider the simple expression grammar:
If we start with and apply the rule , our sentential form is . Now, we have two non-terminals, the left and the right . Which one should we expand next? The choice matters and can lead to different derivation sequences.
To bring order to this process, we adopt a convention. There are two standard strategies:
- Leftmost Derivation (): At each step, always expand the leftmost non-terminal in the sentential form.
- Rightmost Derivation (): At each step, always expand the rightmost non-terminal in the sentential form.
These strategies are not just for convenience; they are fundamental to the theory and practice of parsing. Most parsers are built to systematically find either a leftmost or a rightmost derivation.
Let's see these two strategies in action.
This video from Neso Academy provides a clear, step-by-step demonstration of how to construct both a leftmost and a rightmost derivation for the string id+id*id using our example expression grammar.
Please watch from 03:28 to 07:21. The first part (03:28 - 05:37) demonstrates a leftmost derivation, and the second part (05:37 - 07:21) shows a rightmost derivation. Pay close attention to which non-terminal is being replaced at each step.
As you saw, even though the intermediate steps are different, both strategies can successfully generate the target string. Let's look at the formal definitions for these derivation types.
Chapter 7 Context-Free Languages and PDA's
To complement the video's procedural explanation, the same UPenn document provides the rigorous mathematical definitions for these constrained derivations.
Please read the text around Definition 7.4 on pages 302-304, starting from "It is technically very useful..." to the end of the definition of the rightmost derivation relation. This section formally defines the leftmost (\Rightarrow_{lm}) and rightmost (\Rightarrow_{rm}) derivation relations.
3. Let's Practice
Now, let's work through an example together. Consider the following grammar :
- Productions:
- Start Symbol:
- Terminals:
Let's derive the string abbbb using a leftmost derivation. At each step, we identify the leftmost non-terminal and replace it.
- (Rule 1)
- (Rule 2, replacing the leftmost non-terminal )
- (Rule 3, replacing the new leftmost non-terminal )
- This simplifies to .
- (Rule 4, replacing the leftmost non-terminal )
- This is .
- (Rule 4, replacing )
- This is .
- (Rule 5, replacing )
- This simplifies to .
We have successfully derived the string. The sequence is the complete leftmost derivation.
The image below shows both a leftmost and rightmost derivation for this exact problem, which is a great way to check your understanding.

Test your understanding!
Using the same expression grammar from before, , construct a leftmost derivation for the string id * id + id.
Show answer
Here is one possible leftmost derivation for id * id + id:
- (Start with addition to get the final
+operator) - (Expand the leftmost to get the
*operator) - (Expand the new leftmost )
- (Expand the leftmost remaining )
- (Expand the final )
An important theoretical result (stated as Proposition 7.1 in the UPenn notes) is that if a string can be generated by a grammar at all, it has both a leftmost and a rightmost derivation. This makes these two systematic approaches universally applicable.
Conclusion
In this lesson, we transitioned from the static definition of a CFG to the dynamic process of using it to generate strings. We learned that this process, called derivation, is a sequence of substitutions guided by the grammar's production rules.
Key Takeaways:
- A derivation is a sequence of applications of production rules that transforms the start symbol into a string of terminals.
- A sentential form is any intermediate string in a derivation.
- To resolve the ambiguity of which non-terminal to expand next, we use standard strategies.
- A leftmost derivation () always expands the leftmost non-terminal.
- A rightmost derivation () always expands the rightmost non-terminal.
- Any string that belongs to a context-free language has both a leftmost and a rightmost derivation.
Preview of the Next Lesson:
The sequential lists of steps in a derivation can be difficult to visualize. A much more intuitive way to represent a derivation is with a tree structure. In the next lesson, we will learn how to construct parse trees from derivations. This graphical representation will not only make derivations easier to understand but will also lead us directly to the crucial concept of grammatical ambiguity, a property that has significant implications for programming language design.