7

There are two important theorems about LR(k) grammars and DCFL. Mentioned here.

  1. A language has an LR(1) grammar iff it is DCFL.
  2. A language has an LR(0) grammar iff it is DCFL and has prefix property.

I have counter example for 2nd theorem, plz see if it is valid counterexample. (Ofcourse its not valid and I am wrong, But what wrong I am doing here ?)

DCFL without prefix property: $\{b(ab)^n \mid n\geq0\}$

2nd theorem says it should not have $LR(0)$ grammar, but here it is-

$ S \rightarrow Sab \\S \rightarrow b$

PS: This is my first question here, sorry if I violated any protocol.

user3699192
  • 167
  • 2
  • 17
  • Why do you think your grammar is LR(0)? – Yuval Filmus Jan 09 '17 at 14:22
  • @YuvalFilmus I took this example from "LR Parsing: Theory and Practice By Nigel P. Chapman" page 69 (https://books.google.co.in/books?id=nEA9AAAAIAAJ&lpg=PP1&pg=PA69#v=onepage&q&f=false) – user3699192 Jan 09 '17 at 14:35
  • 1
    Hm. In Sipser's book, he says that every DCFL has an LR(0) grammar... – xavierm02 Jan 10 '17 at 00:22
  • I have another book that says that LR(0) and finite implies the prefix property. (In fact, they prove that if for two non-empty words $u$ and $v$, both $u$ and $uv$ are in $L$, then $uv^n$ is in $L$ for every $n$) – xavierm02 Jan 10 '17 at 00:31
  • I also found ((recognized by a DPDA by empty stack) <-> (recognized by a DPDA by final state and prefix) in several references. – xavierm02 Jan 10 '17 at 00:51
  • I found the reference given by Wikipedia. What they do is prove that LR(0) <-> DPDA by empty stack <-> DPDA by accepting state and prefix. The second equivalence is in many references and convincing. The first one not so much. – xavierm02 Jan 10 '17 at 01:02
  • Yes, that's true. Now, whats conclusion for this question ? :P – user3699192 Jan 10 '17 at 07:14
  • @user3699192 : Well I now think that there's an error on Wikipedia. But I'm not confident enough to do the modification (yet, I may be if I read the proof in the reference and find an error). I sent an email to a teacher that wrote a book on the subject. – xavierm02 Jan 11 '17 at 13:02
  • @xavierm02, Great Job :) let me also know what you conclude. – user3699192 Jan 11 '17 at 14:31
  • @xavierm02 Have u found something? – user3699192 Jan 17 '17 at 23:12
  • @user3699192 I've talked about it to a few people, and several said that it wasn't that surprising because the definition of $LR(0)$ is not the same everywhere. I haven't read the proof in the reference yet though. For the time being, you can just assume that the property is false. I'll let you know if I get a clear authoritative answer, or if I read the reference and understand exactly why the proof doesn't work / the definition is different. – xavierm02 Jan 18 '17 at 08:25
  • I suppose both are TRUE. The fact that the given grammar is considered as LR(0) is due to the fact that by adding "$" at end of the string this can be accepted by a LR(0) parser. Basically by adding $, we are ensuring prefix property - isn't it? – Arjun Suresh Jan 31 '17 at 03:08
  • This is not answering this question but is still a useful link: http://stackoverflow.com/questions/14674912/why-are-there-lr0-parsers-but-not-ll0-parsers – Arjun Suresh Jan 31 '17 at 03:12
  • *$ \text{ at end of the string this can be accepted by a LR(0) parser. Basically by adding }$

    @ArjunSuresh sir, I got ur point, U mean when we add "$" as end marker to the grammar then this ensures prefix property, right ? But doing so. it will change the language itself.

    – user3699192 Feb 01 '17 at 07:09
  • 1
    yes, we are. We are giving the language prefix property and using LR(0) parser. As per definition the language should not have LR(0) grammar, but with a slight modification we can get an LR(0) grammar. I do not think a question like "is this language LR(0)" will come in any standard examination for this or similar languages as that depends on the definition being used. In literature there are many things like this - a good examination avoids them whereas a bad examination concentrates on them :) – Arjun Suresh Feb 02 '17 at 21:16

2 Answers2

3

Your grammar is $\mathrm{LR}(0)$ by adding a $\mathrm{\$}$ symbol to the alphabet and a starting deduction rule $S'\rightarrow S\$$ when constructing the $\mathrm{LR}(0)$ automaton (and it is a standard construction).

Omitting syntactical purposes, every DCFL $\mathrm{L}$ can be parsed by an $\mathrm{LR}(0)$ grammar for $\mathrm{L}\$$. What makes $\mathrm{LR}(k)$ ($k>0$) more practical is that it may include more syntactical information which is very important in compiler design. Otherwise, all these techniques would never be used in practice. In that case, $\mathrm{LL}(k)$ with local manipulation for expressions would be used instead, even with prohibitive cost of time and space.

Thinh D. Nguyen
  • 2,275
  • 3
  • 23
  • 71
0

As an conceptual understanding we can think of it as the grammars which satisfies prefix property can be reduced without even using look ahead as those can be accepted by DPDA which accepts through empty stack, but if language is not satisfying the prefix property we will have to check whether the look ahead is $ or any character, if its $ then we can reduce it otherwise we will have to scan input further. DPDA(empty stack acceptance) is less powerful than DPDA(final state acceptance), thats why LR(0) is less powerful than LR(1) Hope this helps.