edited by
5,544 views
28 votes
28 votes

The grammar whose productions are

  • $\langle\text{stmt}\rangle \to\text{ if id then } \langle\text{stmt}\rangle$
  • $\langle\text{stmt}\rangle\to\text{ if id then } \langle\text{stmt}\rangle\text{ else } \langle\text{stmt}\rangle$
  • $\langle\text{stmt}\rangle \to\text{ id }:=\text{ id}$

is ambiguous because

(a) the sentence

if a then if b then c:= d

has more than two parse trees

(b) the left most and right most derivations of the sentence

if a then if b then c:= d

give rise to different parse trees

(c) the sentence 

if a then if b then c:= d else c:= f

has more than two parse trees

(d) the sentence

if a then if b then c:= d else c:= f

has two parse trees

edited by

1 Answer

Best answer
39 votes
39 votes

(d) the sentence

  •  if a then if b then c: = d else c:= f  has two parse trees as follows:
  1. if a then (if b then c:= d) else c:= f
  2. and if a then (if b then c:=d else c:= f)

$$\begin{align*} \pmb{\text{Ambiguity - “dangling else"}} \end{align*}$$ 

stmt -> if expr then stmt |
        if expr then stmt else stmt | other stmts

$$\mathtt{\text{if $E_1$ then  if $E_2$  then  $S_1$  else  $S_2$ }}$$

       

edited by
Answer:

Related questions

38 votes
38 votes
2 answers
4
Kathleen asked Oct 9, 2014
12,817 views
The pass numbers for each of the following activitiesobject code generationliterals added to literal tablelisting printedaddress resolution of local symbols that occur in...