GATE | Gate IT 2005 | Question 86

Q83 Part_A

Consider the context-free grammar

E → E + E
E → (E * E)
E → id

where E is the starting symbol, the set of terminals is {id, (,+,),*}, and the set of nonterminals is {E}.
Which of the following terminal strings has more than one parse tree when parsed according to the above grammar?

(A) id + id + id + id
(B) id + (id* (id * id))
(C) (id* (id * id)) + id
(D) ((id * id + id) * id)


Answer: (A)

Explanation:
According to leftmost derivation :

E -> E + E (using E -> E + E)
E -> E + E + E (using E -> E + E)
E -> E + E + E + E (using E -> E + id)
E -> id + E + E + E (using E -> id)
E -> id + id + E + E (using E -> id)
E -> id + id + id + E (using E -> id)
E -> id + id + id + id

 

According to rightmost derivation :

E -> E + E (using E -> E + E)
E -> E + E + E (using E -> E + E)
E -> E + E + E + E (using E -> E + id)
E -> E + E + E + id (using E -> id)
E -> E + E + id + id (using E -> id)
E -> E + id + id + E (using E -> id)
E -> id + id + id + id

 
Thus, option (A) is correct.

 
Please comment below if you find anything wrong in the above post.


Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.