in Compiler Design edited by
4,670 views
24 votes
24 votes

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?"

in Compiler Design edited by
4.7k views

1 Answer

38 votes
38 votes
Best answer

Given CFG is "ambiguous" as you told. Is it deterministic? If so there must be a LR(k) grammar for it. This is not the case here - See this

And answer to your question is NO - because that is the definition of inherent ambiguity. If a language is inherently ambiguous it can have no unambiguous grammar possible. Now, no DCFL is inherently ambiguous - any DCFL must have some unambiguous grammar.

Some facts:

  1. DCFL has a one-one correspondence with LR(1) grammar and since LR(k) grammar is unambiguous this means no DCFL can be inherently ambiguous. 
  2. A DCFL can have an ambiguous grammar - just that some unambiguous grammar is also guaranteed to exist.
  3. If A CFL is not deterministic it may or may not have an unambiguous grammar. i.e., If the language of the grammar is not deterministic, still the grammar can be either ambiguous or not-ambiguous. i.e., Not-deterministic does not imply inherent ambiguity but inherent ambiguity does imply not deterministic. 

Quoting from Wikipedia:

Unambiguous grammars do not always generate a DCFL. For example, the language of even-length palindromes on the alphabet of 0 and 1 has the unambiguous context-free grammar S → 0S0 | 1S1 | ε. An arbitrary string of this language cannot be parsed without reading all its letters first which means that a pushdown automaton has to try alternative state transitions to accommodate for the different possible lengths of a semi-parsed string

selected by
by

4 Comments

Summary of the solution:
1. Each DCFL can be generated by a corresponding LR(k) grammar and each LR(k) grammar generates one DCFL (one-to-one correspondence).
2. A language can be generated by more than one grammar, so for each DCFL an unambiguous LR(k) grammar do exists ( means, no DCFL is inherently ambiguous).
3. Each LR(k) grammar is unambiguous grammar, but each unambiguous grammar need not to be LR(k).
4. A language can only be inherently ambiguous while there is no such concept of "inherently ambiguous grammar".
5. An inherently ambiguous language can only be non-determinstic while an non-deterministic language may or may not be inherently ambiguous. For example there are many CFLs which are not inherently ambiguous, while there are such CFLs also which are inherently ambiguous. 

Cheers!!
 

27
27
Do NCFL have LR(k) grammar ..if they are not inherently ambigous ..Or LR(k) parsers deals with only Determisnistic CFLs ??
1
1
@Arjun sir,

LR(1) parsers cannot parse non deterministic CFL which are not inherently ambiguous?

we say that  Language of LR(1) (set of all LR(1) grammars) is same as DCFL. But then what about non deterministic CFL without inherent ambiguity????
0
0

Related questions