1

Till now I have believed that a LL language is a language generated by a LL grammar. Same goes for LR.

Now I know some of the hierarchies such as $LL(1)\subset LR(1)$. I used to think that the hierarchy is valid for language as well as grammar i,e if a grammar is LL1 then it must be LR(1); If a language is LL(1) then it must be LR(1).

Language theoretic comparison of LL and LR grammars

In the above link there is mention about two type of hierarchy. One for grammar, another for language. The two hierarchies are different at some points.

For e,g for grammar hierarchy $LALR(1)\subset LR(1)$ but for language hierarchy $LALR(1)=LR(1)$. I accept the grammar hierarchy. But the language hierarchy confuses me. Basically this hierarchy says that LALR(1) language set is equal to LR(1) language set.

How could this be true?

There are some grammars which are LR(1) but not LALR(1). So the language produced by these grammars should be LR(1) but not LALR(1) thus violating the language hierarchy.

I must be making some kind of mistake. please someone point that out?

Kishan Kumar
  • 717
  • 1
  • 8
  • 24

2 Answers2

2

A language is in a class if there exist a grammar of that class which generates the language. Stating that for languages $LALR(1)=LR(1)$, means that when you have a $LR(1)$ grammar you can build a $LALR(1)$ grammar for the same language. The grammar may be more complex than the $LR(1)$ grammar (for instance, it may use equivalent but different non-terminals to provide the finer distinction of contexts that $LR(1)$ gives for free).

AProgrammer
  • 3,099
  • 18
  • 20
1

The notation $LR(1)$ denotes the set of languages that can be accepted by a LR(1) parser. So when you see something like $LALR(1)=LR(1)$, that is fundamentally about languages. It is incorrect to write "$LALR(1) \subset LR(1)$ for the grammar hierarchy"; that is meaningless. You haven't defined what you mean by a "grammar hierarchy", so we're forced to guess what that might mean. That's a non-standard concept -- we usually focus on the language hierarchy.

I don't know what the correct relationship between $LALR(1)$ and $LR(1)$ is, but here's how it could be true (if it is true): every LALR(1) grammar is a LR(1) grammar, but not every LR(1) grammar is a LALR(1) grammar; yet perhaps every LR(1) grammar can be rewritten into an equivalent LALR(1) grammar (one that accepts the same language). That would be consistent with (a natural interpretation) of what you wrote.

Keep in mind that there might be multiple grammars that accept the same language.

D.W.
  • 167,959
  • 22
  • 232
  • 500