I know that DCFL are unambiguous languages and DCFL languages have one-to-one correspondence with LR grammars.
But I wanted to know if there can be an instance that deterministic context free grammar is ambiguous.
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.
Sign up to join this communityNo, there is no deterministic context-free grammar that is ambiguous since any deterministic context free grammar (DCFG) must be unambiguous.
Here is definition 2.47 of the book introduction to the theory of computation, third edition, by Michael Sipser.
A deterministic context-free grammar is a context-free grammar such that every valid string has a forced handle.
Furthermore, here is definition 2.62.
An LR($k$) grammar is a context-free grammar such that the handle of every valid string is forced by lookahead $k$.
A DCFG is the same as an LR(0) grammar. All LR($k$) grammars are unambiguous, by definition.
I was also struggling with this problem. I think I might have found an ambiguous DCFG G = ({S,T,A},{a,u,v},R,S), where R is defined as
S -> ATa | aTa
T -> uv
A -> a
G is ambiguous because there are two different leftmost derivations for string auva.
S => ATa => aTa => auva
S => aTa => auva
However, G is also a DCFG because there is only one leftmost reduction
auva >-> Auva >-> ATa >-> S
which means that there are a total of three valid strings: auva, Auva, ATa. And we can show that each of them has a forced handle by enumerating all possibilities.