
In formal language theory, particularly in context-free grammars (CFGs) , derivations , derivation trees , and ambiguity are essential concepts that help describe how strings are generated...
In formal language theory, particularly in context-free grammars (CFGs), derivations, derivation trees, and ambiguity are essential concepts that help describe how strings are generated and analyzed within a given grammar.
A derivation is the process of applying the production rules of a grammar to a start symbol in order to generate a string of terminal symbols. In other words, a derivation shows how the start symbol of a grammar can be transformed into a string of terminal symbols (the strings that belong to the language generated by the grammar).
A derivation can proceed in two ways:
Consider the following CFG:
So, the string "ab" is derived.
Consider the same grammar:
This is a rightmost derivation because we applied the production where the rightmost non-terminal (in this case, the whole string ) was replaced.
A derivation tree (also known as a parse tree) is a graphical representation of a derivation, showing how the start symbol of a grammar is expanded into a string of terminal symbols by applying production rules.
In a derivation tree:
A derivation tree makes it easy to visualize how a string is derived from the grammar and how different parts of the string correspond to the non-terminals in the grammar.
Consider the grammar:
To generate the string "ab", we can represent the derivation as a tree.
The resulting derivation tree is:
S
/ \
a b
This tree shows that the start symbol is directly replaced by the terminal string "ab".
To generate the string "aabb", use the following derivation:
The derivation tree for "aabb" would look like this:
S
/ \
a Sb
/ \
a b
This tree shows how the string "aabb" is generated by recursively applying the grammar's production rules.
A grammar is ambiguous if there exists a string in the language that can be derived in more than one way. This means that there are multiple distinct derivation trees (or derivations) for the same string.
Consider the following CFG for arithmetic expressions:
The string "id + id \cdot id" can be derived in two ways:
First derivation:
Second derivation:
Thus, the same string "id + id \cdot id" has two distinct derivations, leading to different interpretations of operator precedence. This shows that the grammar is ambiguous.
For the same string "id + id \cdot id", we have two distinct derivation trees:
E
/ | \
E + E
/ / \
id E E
| |
id id
E
/ | \
E * E
/ / \
id E E
| |
id id
These two trees show that the string "id + id \cdot id" can be interpreted in two different ways, resulting in the ambiguity of the grammar.
Remove Ambiguity:
Disambiguation Rules:
Understanding derivations, derivation trees, and ambiguity is critical for analyzing and designing grammars, especially in contexts like programming languages and compilers.
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.