For example I would like to derive the string 'aabbccdd' from the given set of production:
S -> AB | C
A -> aAb | ab
B -> cBd | cd
C -> aCd | aDd
D -> bDc | bc
I can derive the string from AB using the leftmost and rightmost derivation.
But how about from C? Upon deriving the string, I am always having one variable only.
Derivation from C:
S -> C
S -> aCd
S -> aaDdd
S -> aabDcdd
S -> aabbccdd
What kind of derivation was used and can I consider this grammar ambiguous?
In a rightmost derivation, the rightmost non-terminal is derived. In a leftmost derivation, the leftmost non-terminal is derived. If there is only one non-terminal, the derivation is both leftmost and rightmost. This is not a contradiction.
Since there are two derivations for the target string starting with S
, the grammar is ambiguous.