when i was studying compilers i came across this in Wikipedia every DCFL has a corresponding unambiguous grammar and hence can be parsed by an LR(1) parser which is the most powerful parser then what is the significance of LR(k) parsers are they more powerful than LR(1) when every DCFL can be parsed by LR(1) why do we need LR(k)?
-
Duplicate? https://cs.stackexchange.com/questions/68499/every-dcfl-has-an-lr1-an-lalr1-and-even-an-slr1-grammar – MSalters Jan 10 '18 at 15:31
1 Answers
Remember that the point of parsing (in almost all practical applications) is not simply to recognize whether or not a sentence is in a language, but rather to find the parse tree corresponding to the sentence in order to do further processing (such as compilation). Or, to put it another way, few people would purchase a "compiler" which took some input and whose output was limited to one of two sentences:
That input is a syntactically valid C program
That input is not a syntactically valid C program.
By a large, the desired product is an executable, not just an assurance that the program could be compiled if you had a compiler.
So, it is true that if there is an LR(k) grammar for a language, then there is also an LR(1) grammar. But it is not the same grammar (unless k is 1) and it will therefore not produce the same parse tree.
It is possible to mechanically transform the LR(k) grammar into an LR(1) grammar in a way that the original parse tree can be recovered, but it is not a procedure you want to try by hand. (Trust me on this.)
So if you have an LR(k) grammar and you want to build a parser, you could:
Find an LR(k) parser generator
Find a program which mechanically tranforms an LR(k) grammar into the corresponding LR(1) grammar and the code necessary to transform a parse tree produced by the LR(1) grammar into the corresponding parse tree which would have been produced by an LR(k) parse. Then feed the LR(1) grammar into an LR(1) parser generator and add the parse tree recovery code to the semantic actions.
These two tasks are not particularly different. The mechanical transformation of an LR(k) grammar into an LR(1) grammar blows up the size of the grammar much in the way that the LR(k) parse tables are much larger than the LR(1) parse tables. From a practical viewpoint, it is roughly equally difficult to find an LR(k) parser generator as it is to find an automatic LR(k)→LR(1) transformer.
- 12,020
- 21
- 38
-
sir look at this link https://stackoverflow.com/questions/6379937/what-about-theses-grammars-and-the-minimal-parser-to-recognize-it .. sir in this image it shows LR(K) contains LR(1) in it but shouldnt they both be the same as if there is an LR(K) there will surely be an LR(1) grammer for the language right ? is this image wrong – venkat Jan 11 '18 at 05:25
-
1@venkat: that chart is for grammars, not languages. An LR(1) grammar is LR(k) but an LR(k) grammar is not necessarily LR(1). The language is LR(1) because we know that there must be a different grammar for the language which is LR(1). – rici Jan 11 '18 at 06:16
-
so every there is one to one correspondence between LR(k) and LR(1) languages means if a language has LR(k) it will have an LR(1) grammar for sure but there no one to one correspondence between LR(K) and LR(1) grammars but LR(1) grammars are equally powerful because they can parse any language which LR(k) grammars parse please correct me if i am wrong – venkat Jan 11 '18 at 06:36
-
the point which is confusing me is if every LR(K) has a corresponding LR(1) is this not one to one correspondence ? please clarify it – venkat Jan 11 '18 at 06:40
-
It's not a 1-1 correspondence. There are an infinite number of grammars for any given language. Some are not LR parseable at all. Some are not deterministic. Etc. If there is at least one LR(k) grammar, then there will also be (many) LR(1) grammars. – rici Jan 11 '18 at 14:08
-
1You must try to understand the difference between a language and a grammar. A language is just a possibly-infinite set of sentences. A parser works on a grammar; the language it parses is the set of sentences recognised by that grammar. It s is undecidable (in general) whether two grammars produce the same set (language). – rici Jan 11 '18 at 14:11
-
possibly stupid question: is there any significant meaning attached to "mechanically" in "...mechanically transform...". Also, will love to know if you have come across any concrete example of converting LR(k) grammar to LR(1) grammar and recovering LR(k) parse tree from LR(1) parse tree (though from other your related answers, I feel there isnt any such example). – RajS Jun 29 '19 at 12:56
-
1@anir: "mechanically" means that there is an algorithm which can do it. No inspiration is necessary. The proof of the proposition is constructive; it shows that the algorithm works. You should be able to find the proof in a good text on formal language theory, probably with an example of the construction. The text I usually recommend is Parsing Theory by Sippu&Soisalon-Soininen, but it's not an easy read; there are probably other more accessible textbooks... – rici Jun 29 '19 at 14:58
-
1As far as I know, there's no readily available software which implements the algorithm, for the same reason that LR(k) parser generators don't exist for values of $k>1$: the generated parsers are just too big. The LR(k)->LR(1) algorithm effectively starts with the LR(k) automaton; it does not in any way simplify it. It's really just a different presentation of the same algorithm. So in practical terms, it doesn't add much value. But understanding how it works is a great exercise in understanding the KR(k) algorithm in general. – rici Jun 29 '19 at 15:04