in Compiler Design edited by
7,294 views
24 votes
24 votes
Show that grammar $G_1$ is ambiguous using parse trees:

$G_{1}:   S \rightarrow$ if $S$ then $S$ else $S$

         $S \rightarrow$ if $S$ then $S$
in Compiler Design edited by
7.3k views

2 Comments

but how to minimize s? no epsilon ?
1
1
the inherently ambiguous problem for a language not grammer
2
2

1 Answer

23 votes
23 votes
Best answer

The given grammar is well known as "Dangling Else" problem.  The given grammar is ambiguous and ambiguity can be resolved.

$\textbf{stmt} \to$ $ \textbf{if} \ $expr$ \ \textbf{then}$ stmt
                           $\mid $ $\textbf{if}$ expr $ \textbf{ then }$ stmt$ \textbf{ else}$ stmt
                           $\mid \textbf{other} $

Consider the compound conditional statement for the above grammar

if $E_1$ then $S_1$ else if $E_2$ then $S_2$ else $S_3$ has the following parse tree

Well this is ambiguous due to the statement
if $E_1$ then if $E_2$ then $S_1$ else $S_2$

The two parse trees are

Ambiguity can be resolved by parsers  as follows

In all programming languages with conditional statements of this form, the first parse tree is preferred.
The general rule is, "Match each else with the  closest unmatched then"

In practice it is rarely built into the productions . 

$\textbf{stmt} \to$ $ \textbf{matched_stmt}$
                          $\mid $ $\textbf{open_stmt}$
$\textbf{matched_stmt} \to$ $ \textbf{if } $expr$ \textbf{ then }$ matched_stmt $ \textbf{ else}$ matched_stmt
                          $\mid $ $\textbf{other}$
$\textbf{open_stmt} \to$ $ \textbf{if } $expr$ \textbf{ then }$ stmt
                           $\mid $ $\textbf{if }$ expr $ \textbf{ then }$ matched_stmt$ \textbf{ else}$ open_stmt

However this grammar is also ambiguous 

Consider the following exercise question from Dragon book.

$\textbf{Exercise 4.3.3:}$ The following grammar is proposed to remove the “dangling-else ambiguity”:

  • $stmt \to \textbf{if}\; expr\;\textbf{then}\;stmt$
    $\qquad \quad \mid matchedStmt$
  • $matchedStmt \to \textbf{if}\;expr\;\textbf{then}\;matchedStmt\;\textbf{else}\;stmt$
    $\qquad \qquad  \qquad \quad \mid \textbf{ other}$

Show that this grammar is still ambiguous.

Solution is given here. But we can make an equivalent unambiguous grammar for the dangling-else problem and that makes the language not inherently ambiguous. 

Try running this code in any compilers . No doubt that all compilers will successfully parse and produce output

#include <stdio.h>

int main(void) {
	if(1<3)
	if(1<2)
	printf("1 is the smallest");
	else
	printf("2 is the smallest");
	return 0;
}

Thus we can say that ambiguity in Dangling Else Problem can be resolved and we can have an unambiguous grammar for it (making the language $\textbf{NOT}$ inherently ambiguous. But many of the grammars given for Dangling Else problem is ambiguous. 

Source

Good Read 

edited by
by

31 Comments

very informative answer pc
1
1
The question is different ?
1
1
@pavan What do you mean by different ?
0
0
@pC So the language of the grammar is inherently ambiguous? This info too can be added in answer.
2
2
but it says in first line that ambiguity can be removed?
0
0
i think arjun sir asked a question here that whether the language will be inherently ambiguous or not...
it will not be inherently ambiguous as we can remove the ambiguity here
0
0
It says "resolved", meaning the parser choses one of the parse trees possible. Not that ambiguity is removed from the grammar.
11
11
@Arjun sir...u mean language generated by this grammar will be inherently amibguous?
0
0
That is my question. If not there should be a grammar which is not ambigous for it.
0
0
edited by
yes sir..we can have one such unambiguous grammar for it.....and since we can get atleast one such unambiguous grammar..language is not inherently ambiguous...isnt sir?
0
0
No it is not. Whatever grammar you give- there will always be multiple parse trees possible for some words. So, language is inherently ambiguous.
1
1
edited by

@Arjun sir here we are getting problem because we have 2 "if" and else will get associated with the closest one....so removing left factoring wouldnt help here...but sir in wiki its mentioned that we can remove ambiguity by changing the syntax..https://en.wikipedia.org/wiki/Dangling_else.....
and if its true that by changing syntax of the language we can somehow remove ambiguity then inherent property of the langauage is also lost..and then language will not be inherently ambiguous.isnt it sir?

0
0
No, that changes the language - some strings not possible in the new language. In the first paragraph it is mentioned that the language of dangling else is inherently ambiguous.
0
0
edited by
@arjun sir In Aho and Ullman they have shown equivalent unambiguous grammar . which I have shown in the 2nd paragraph but actually that grammar is also ambiguous . Moreover No grammar is there that could resolve dangling else problem unambiguously . Hence it should be inherently ambiguous . Should I need to add this proof in the answer ?

If I add this part it will contracdict with  " ambiguity can be resolved " :D though it is also true
0
0
No, no need to prove it. Issue is by reading your answer many would think language of "dangling-else" problem is not inherently ambiguous. And that might be a GATE question :) So, just need to make this point clear. "Resolve" here is used in the sense that parser can continue working -- not that ambiguity is removed from the grammar.
6
6

@Arjun sir....if question is asked whether dangling else problem can be resolved?...then our answer should be yes since confusion in parse trees can be resolved...

if question is asked can ambiguity be removed??..then also it means same here because ambiguity means confusion among parse trees...right sir?
but we cant remove amibuity from the language.because not a single unambiguous grammar exists for this...so language will be inherently ambiguous...

0
0
For GATE no such questions will be asked. Every question (at least >95%) in GATE will be meaningful. As told, the only thing that really matters is "language of dangling else" is inherently ambiguous. This can be asked in GATE. Also, questions can be asked regarding ways of resolving. But there won't be questions like "is dangling else problem ambigous if we look from left side/right side" etc, but unfortunately these can happen in other exams.
8
8
okk sir...got it :)
0
0
@arjun sir , check now . Updated
0
0
@arjun sir , can u pls check this grammer ?
I think it is unambiguous and ambiguity in completely eleminated here . So I think resolved dangling else grammer is NOT  inherently ambiguous

$\textbf{stmt} \to$ $ \textbf{matched_stmt}$
                          $\mid $ $\textbf{open_stmt}$
$\textbf{matched_stmt} \to$ $ \textbf{if} $expr$ \textbf{then}$ matched_stmt $ \textbf{else}$ matched_stmt
                          $\mid $ $\textbf{other}$
$\textbf{open_stmt} \to$ $ \textbf{if} $expr$ \textbf{then}$ stmt
                           $\mid $ $\textbf{if}$ expr $ \textbf{then}$ matched_stmt$ \textbf{else}$ open_stmt
0
0

@PC
So grammar for Dangling Else is still ambiguous or not?

0
0

I don't know if the dangling else language is inherently ambiguous or not, but following are the two Screenshots taken from "Compilers - Principles, Techniques, and Tools, By Aho ullman" second edition

7
7

Thnx @manu bro for giving valid references.

I think we should consider it as Unambiguous as at least one unambiguous grammar exist and also this is an authentic source 

0
0
Source:https://www.cse.iitk.ac.in/users/karkare/cs335/lectures/05SyntaxAnalysis.pdf

The above screenshot is taken from the lecture slides of iit kanpur. They clearly mentioned that if we rewrite the ambiguous grammar to unambiguous grammar than we won't get the grammar which will accept the same language. And this is the case with dangling else problem. Thus the dangling else problem is inherently ambiguous.

0
0

Manu Thakur @pC @ Arjun  Sir please explain if some unambiguous grammar does exist for this dangling-else problem (as shown above) than how is this language inherently ambiguous?

0
0
edited by
@ Manu Thakur @pC @ Arjun  Sir please explain if some unambiguous grammar does exist for this dangling-else problem (as shown above) than how is this language inherently ambiguous?

Yes please someone clear this doubt

also check this answe by @Habibkhan sir

https://gateoverflow.in/84451/dangling-else-problem-and-ambiguity-elimination
0
0
The language is not inherently ambiguous.
5
5
Sir, can you make it more clear why the dangling else problem is not inherently ambiguous? (unless the language gets modified I am not able to generate any ambiguous grammar)
0
0
this grammar matches each else with the nearest if and is unambiguous.

Hence this language is not inherently ambiguous
1
1

Related questions