-4

DANGLING ELSE PROBLEM:

S->iEtSS' / a

S'->∊/ eS

E->b

is a Deterministic context free grammar, and is ambiguous for "iEtiEtSeS"

but ALL DCFG ARE UNAMBIGUOUS . so " how can this DCFG be ambiguous?"

  • Welcome to Stack Overflow. This is a place to post your code and ask for help in fixing something that isn't working right. – Seano666 Jun 30 '16 at 15:09
  • SO is about specific languages and/or problems. This is neither. I'm not sure Programmers.SE would be specific enough, either, since the question is probably more generalised to grammar theory. Maybe there's a more suited SE site out there that I don't know about yet. Also, why are you SOMETIMES SHOUTING? – underscore_d Jun 30 '16 at 15:11
  • The language is not inherently ambiguous because it is possible to construct a deterministic grammar. The grammar you present is ambiguous, but you can build a deterministic parsing automaton from it by resolving the ambiguity in favour of shifting. This is all explained better in any standard textbook on parsing theory. – rici Jun 30 '16 at 20:17

1 Answers1

0

is a Deterministic context free grammar, and is ambiguous for "iEtiEtSeS"

This is the problem. You assume that your grammar is a deterministic context free grammar. However, you also state that it is ambiguous. Both cannot be true and indeed it is not: Your grammar is not a deterministic context free grammar but only a context free grammar that is ambiguous.

Indeed, the dangling else problem is known to be an issue for compiler design, because it cannot be solved with only a context free grammar. It is often disambiguated by using a syntactic predicate. This tells the compiler to prefer the shift over the reduce operation, which leads to a parse tree where the else part is attached to the inner most if. However, while this resolves the problem, it is not part of a pure context free grammar.

Finally, to give a clear and simple answer to the question: No, it is not possible to specify a deterministic context free grammar for an inherently ambiguous language.

Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51