edited by
2,373 views
3 votes
3 votes
The dangling else problem in the construct If (E) sales S | is (E) S | a can be resolved IN SDTS by

(A) Using the associative & precedence of operating & the 'exe' munch principle
(B) By change the grammar to an unambiguous one
(C) Cannot be removed as it Is undecidable
(D) None of the above
edited by

3 Answers

1 votes
1 votes

The most appropriate answer is (A) [It can also be B, if its possible to reduce into unambiguous].

Let me explain...

'Dangling Else' problem is like deciding with WHICH 'if' some 'else' can go with. Say for following...

ex1) X --> S | b

        S --> if E X else a | if E X

ex2) S --> if E S else a | if E S | b

Both of the above cases suffers through Dangling Else problems. Why? Both can derive, " if E if E b else a" . (E stand for some expression in Code.) Now, with which 'if' should we configure our 'else'. Compilers (LALR(1) in our day to day life) uses the approach of choosing 'nearest if' .  But this is just ONE of the way to resolve this so called ambiguity. Some other methods are...

1) Associate dangling else with nearest if by using some matching/unmatching statemnets

2) By using something like 'endif' for marking the end of of conditional structure.

3) By using opening ( { ) and closing ( } ) curly braces around statements.

4) By using different precedence rules to associate the dangling else with nearest if.  (By using lower precedence for THEN and higher precedence for ELSE. Try it.) And by doing this and other techniques you are HANDLING ambiguity in grammer.

5) By insisting space indentation.

 A nice article on this by "Efstathios Chatzikyriakidis" (What a name :) ) Check it out @ http://efxa.org/2014/05/17/techniques-for-resolving-common-grammar-conflicts-in-parsers/

edited by
0 votes
0 votes
The dangling else grammer is inherently ambigous.Even the java has inherited the same from C.The compiler matches the else with the nearest if.And also we have brace brackets for compilers to interpret it in proper way.

I think the answer should be C.

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/PPT/The%20dangling-else%20ambiguity.ppt

Related questions