Open In App

GATE | Gate IT 2005 | Question 86

Like Article
Like
Save
Share
Report

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


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads