Derivation Trees
Derivation Trees
Derivation Trees
Parse Tree-
1. Leftmost derivation-
The process of deriving a string by expanding the leftmost non-terminal at each step is called as a leftmost
derivation.
The geometrical representation of leftmost derivation is called as a leftmost derivation tree.
Example-
Leftmost derivation-
S → aB
→ aaBB (Using B → aBB)
→ aaaBBB (Using B → aBB)
→ aaabBB (Using B → b)
→ aaabbB (Using B → b)
→ aaabbaBB (Using B → aBB)
→ aaabbabB (Using B → b)
→ aaabbabbS (Using B → bS)
→ aaabbabbbA (Using S → bA)
→ aaabbabbba (Using A → a)
2. Rightmost derivation-
The process of deriving a string by expanding the rightmost non-terminal at each step is called as a rightmost
derivation.
The geometrical representation of rightmost derivation is called as a rightmost derivation tree.
Example-
S → aB
→ aaBB (Using B → aBB)
→ aaBaBB (Using B → aBB)
→ aaBaBbS (Using B → bS)
→ aaBaBbbA (Using S → bA)
→ aaBaBbba (Using A → a)
→ aaBabbba (Using B → b)
→ aaaBBabbba (Using B → aBB)
→ aaaBbabbba (Using B → b)
→ aaabbabbba (Using B → b)
NOTE
For unambiguous grammars, Leftmost derivation and Rightmost derivation represents the same parse tree.
For ambiguous grammars, Leftmost derivation and Rightmost derivation represents different parse trees.
Here,
Since the given grammar was unambiguous, therefore leftmost derivation and rightmost derivation represents the
same parse tree.
Properties of a parse tree-
Root node of a parse tree is the start symbol of the grammar.
Each leaf node of a parse tree represents a terminal symbol.
Each interior node of a parse tree represents a non-terminal symbol.
Parse tree is independent of the order in which the productions are used during derivations. (Important)
Solution-
1. Leftmost derivation-
S → bB
→ bbBB (Using B → bBB)
→ bbaB (Using B → a)
→ bbaaS (Using B → aS)
→ bbaabB (Using S → bB)
→ bbaabaS (Using B → aS)
→ bbaababB (Using S → bB)
→ bbaababa (Using B → a)
2. Rightmost derivation-
S → bB
→ bbBB (Using B → bBB)
→ bbBaS (Using B → aS)
→ bbBabB (Using S → bB)
→ bbBabaS (Using B → aS)
→ bbBababB (Using S → bB)
→ bbBababa (Using B → a)
→ bbaababa (Using B → a)
3. Parse Tree-
Whether we consider the leftmost derivation or rightmost derivation, we will get the above parse tree.
The reason is given grammar is unambiguous for which leftmost derivation and rightmost derivation represents the
same parse tree.
Problem-02:
Solution-
1. Leftmost derivation-
S → A1B
→ 0A1B (Using A → 0A)
→ 00A1B (Using A → 0A)
→ 001B (Using A → ∈)
→ 0010B (Using B → 0B)
→ 00101B (Using B → 1B)
→ 00101 (Using B → ∈)
2. Rightmost derivation-
S → A1B
→ A10B (Using B → 0B)
→ A101B (Using B → 1B)
→ A101 (Using B → ∈)
→ 0A101 (Using A → 0A)
→ 00A101 (Using A → 0A)
→ 00101 (Using A → ∈)
3. Parse Tree-
Whether we consider the leftmost derivation or rightmost derivation, we will get the above parse tree.
The reason is given grammar is unambiguous for which leftmost derivation and rightmost derivation represents the
same parse tree.
Example:
S → aSS (Rule: 1)
A→b (Rule: 2)
To obtain the string ‘w’ the sequence followed is “left most derivation”, following “1121222”.
Example:
S → aSS (Rule: 1)
A→b (Rule: 2)
S ⇒ aSS (Rule: 1)
⇒ aSb (Rule: 2)
⇒ aaSSb (Rule: 1)
⇒ aaSaSSb (Rule: 1)
⇒ aaSaSbb (Rule: 2)
⇒ aaSabbb (Rule: 2)
⇒ aababbb (Rule: 2)
To obtain the string ‘w’ the sequence followed is “right most derivation”, following “1211222”.
Mixed Derivation: In a mixed derivation the string is obtained by applying production to the leftmost
variable and rightmost variable simultaneously as per the requirement in each successive step.
S ⇒ aSS (Rule: 1)
⇒ aSb (Rule: 2)
⇒ aaSSb (Rule: 1)
⇒ aabSb (Rule: 2)
⇒ aabaSSb (Rule: 1)
⇒ aabaSbb (Rule: 2)
⇒ aababbb (Rule: 2)
To obtain the string ‘w’ the sequence followed is “mixed derivation”, following “1212122”.
S ⇒ aAB (Rule: 1)
⇒ aBbaB (Rule: 2)
⇒ acbaB (Rule: 4)
⇒ acbabB (Rule: 3)
⇒ acbabc (Rule: 4)
Left most derivation Tree to obtain the string ‘w’ as follows;
S ⇒ aAS (Rule: 2)
⇒ abS (Rule: 3)
⇒ abaAS (Rule: 1)
⇒ abaabSS (Rule: 2)
⇒ abaabaS (Rule: 3)
⇒ abaabaa (Rule: 3)
E→E+E (Rule: 1)
E→E*E (Rule: 2)
E→(E) (Rule: 3)
E → id (Rule: 4)
(i)
E⇒E+E (Rule: 1)
⇒E*E+E (Rule: 2 and left derivation)
⇒ id * id + id (Rule: 4 and left derivation)
E⇒E*E (Rule: 2)
⇒(E)*E (Rule: 3 and left derivation)
⇒(E+E)*E (Rule: 1 and left derivation)
⇒ ( id + id ) * id (Rule: 4)
Derivation Tree solved example 3.2
Example 4: A grammar G = (N, T, P, S) with N = {S, A}, T = {a, b} and P are as follow;
S → aS (Rule: 1)
S → aA (Rule: 2)
A → bA (Rule: 3)
A→b (Rule: 4)
Solution:
(i) S ⇒ aA (Rule: 2)
⇒ ab (Rule: 4)
(ii) S ⇒ aS (Rule: 1)
⇒ aaA (Rule: 3)
⇒ aab (Rule: 4)
(iii) S ⇒ aS (Rule: 1)
⇒ aaS (Rule: 1)
⇒ aaaA (Rule: 3)
⇒ aaab (Rule: 4)
(iv) S ⇒ aS (Rule: 1)
⇒ aaS (Rule: 1)
⇒ aabA (Rule: 3)
⇒ aabb (Rule: 4)
(v) S ⇒ aS (Rule: 1)
⇒ aaS (Rule: 1)
⇒ aaaS (Rule: 1)
⇒ aaaaA (Rule: 3)
⇒ aaaab (Rule: 4)
Hence; Language generated by the above grammar L(G) = { ab, aab, aaab, aabb, aaaab, .. .. .. ..}
By analyzing the above generated string form the grammar G, there has a similar pattern in all
computed strings, i.e.
The minimum length of the string consist ab always which means occurrence of a’s = 1, and b’s = 1 as well.
In the generated strings a’s followed by b’s always that means strings are always start with a’s and end with b’s.
There is no limitation of number of occurrence of a’s and b’s and no relation of repetition of a’s and b’s.
Thus we can write the language of the grammar L(G) = {an bm : n > 0 ; m > 0}
Types of Grammar-
On the basis of number of derivation trees, grammars are classified as-
1. Ambiguous Grammar
2. Unambiguous Grammar
1. Ambiguous Grammar-
A grammar is said to be ambiguous if it produces more than one parse tree or derivation tree or syntax tree for at
least one string generated by it.
OR
A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost
derivation for at least one string generated by it.
Example-
Ambiguous Grammar
Reason-01:
Since two parse trees exist for the string w, therefore the grammar is ambiguous.
Reason-02:
Let us consider a string w generated by the grammar-
w = id + id x id
Now, let us draw the syntax trees for this string w.
Since two syntax trees exist for the string w, therefore the grammar is ambiguous.
Reason-03:
Since two leftmost derivations exist for the string w, therefore the grammar is ambiguous.
Reason-04:
Since two rightmost derivations exist for the string w, therefore the grammar is ambiguous.
2. Unambiguous Grammar-
A grammar is said to be unambiguous if it produces exactly one parse tree or derivation tree or syntax tree for all
the strings generated by it.
OR
A grammar is said to be unambiguous if there exists exactly one leftmost derivation or exactly one rightmost
derivation for all the strings generated by it.
Example-
Unambiguous Grammar
For all the strings generated by this grammar, there exists exactly one parse tree or syntax tree or leftmost
derivation or rightmost derivation.
Therefore, the grammar is unambiguous.