Sie sind auf Seite 1von 16

Derivation Trees

Parse Tree-

 The process of deriving a string is called as a derivation.


 The geometrical representation of a derivation is called as a parse tree or derivation 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-

Consider the following grammar-


S → aB / bA
S → aS / bAA / a
B → bS / aBB / b
(Unambiguous Grammar)
Let us consider a string w = aaabbabbba

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-

Consider the following grammar-


S → aB / bA
S → aS / bAA / a
B → bS / aBB / b
(Unambiguous Grammar)
Let us consider a string w = aaabbabbba
Now, let us derive the string w using rightmost derivation.
Rightmost derivation-

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,

Leftmost Derivation Tree = Rightmost Derivation Tree

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)

Yield of a parse tree-


 Concatenating the leaves of a parse tree from the left produces a string of terminals.
 This string of terminals is called as yield of a parse tree.

PRACTICE PROBLEMS BASED ON DERIVATIONS OF


STRINGS AND PARSE TREE-
Problem-01:

Consider the grammar-


S → bB / aA
A → b / bS / aAA
B → a / aS / bBB
For the string w = bbaababa, find-
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree

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:

Consider the grammar-


S → A1B
A → 0A / ∈
B → 0B / 1B / ∈
For the string w = 00101, find-
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree

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.

Types of Derivation Tree:


There are three types of Derivation trees;

 Leftmost Derivation tree


 Rightmost derivation tree
 Mixed derivation tree

Leftmost derivation: A leftmost derivation is obtained by applying production to the leftmost


variable in each successive step.

Example:

Consider the grammar G with production:

S → aSS (Rule: 1)
A→b (Rule: 2)

Compute the string w = ‘aababbb’ with left most derivation.


S ⇒ aSS (Rule: 1)
⇒ aaSSS (Rule: 1)
⇒ aabSS (Rule: 2)
⇒ aabaSSS (Rule: 1)
⇒ aababSS (Rule: 2)
⇒ aababbS (Rule: 2)
⇒ aababbb (Rule: 2)

To obtain the string ‘w’ the sequence followed is “left most derivation”, following “1121222”.

Rightmost derivation: A rightmost derivation is obtained by applying production to the rightmost


variable in each step.

Example:

Consider the grammar G with production:

S → aSS (Rule: 1)
A→b (Rule: 2)

Compute the string w = ‘aababbb’ with right most derivation.

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”.

Example 1: A grammar G which is context-free has the productions


S → aAB (Rule: 1)
A → Bba (Rule: 2)
B → bB (Rule: 3)
B→c (Rule: 4)

Compute the string w = ‘acbabc’ with left most derivation.

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;

Derivation Tree solved example


Example 2: A grammar G which is context-free has the productions
S→a (Rule: 1)
S → aAS (Rule: 2)
A → bS (Rule: 3)

Compute the string w = ‘abaabaa’ with left most derivation.

S ⇒ aAS (Rule: 2)
⇒ abS (Rule: 3)
⇒ abaAS (Rule: 1)
⇒ abaabSS (Rule: 2)
⇒ abaabaS (Rule: 3)
⇒ abaabaa (Rule: 3)

Left most derivation Tree to obtain the string ‘w’ as follows;

Derivation Tree solved example


Example 3: A grammar G = (N, T, P, S) with N = {E}, S = E, T = {id, +, *, c}

E→E+E (Rule: 1)
E→E*E (Rule: 2)
E→(E) (Rule: 3)
E → id (Rule: 4)

Obtain the derivation tree of expressions:


(i) id * id + id
(ii) id + id * id
Solution:

(i)

E⇒E+E (Rule: 1)
⇒E*E+E (Rule: 2 and left derivation)
⇒ id * id + id (Rule: 4 and left derivation)

Derivation Tree solved example 3.1


(ii)

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)

Obtain the derivation tree and L(G).

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}

And the derivation tree of the grammar G is:

Derivation Tree solved example 4

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-

Consider the following grammar-


E → E + E / E x E / id

Ambiguous Grammar

 This grammar is an example of an Ambiguous Grammar.


 As per the definition, we can state any one of the following reasons to prove that the grammar is ambiguous-

Reason-01:

Let us consider a string w generated by the grammar-


w = id + id x id
Now, let us draw the parse trees for this string w.

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:

Let us consider a string w generated by the grammar-


w = id + id x id
Now, let us write the leftmost derivations for this string w.

Since two leftmost derivations exist for the string w, therefore the grammar is ambiguous.

Reason-04:

Let us consider a string w generated by the grammar-


w = id + id x id
Now, let us write the rightmost derivations for this string w.

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-

Consider the following grammar-


E→E+T/T
T→TxF/F
F → id

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.

Das könnte Ihnen auch gefallen